apug-umich commited on
Commit
ecc15cc
·
verified ·
1 Parent(s): b3fb483

Upload 3 files

Browse files
Files changed (3) hide show
  1. fingerprints.py +23 -0
  2. sanitize_edc.py +38 -0
  3. train_classifier.py +63 -0
fingerprints.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from rdkit import Chem
3
+ from rdkit.Chem import AllChem
4
+
5
+ def smiles_to_fingerprint(smiles):
6
+ mol = Chem.MolFromSmiles(smiles)
7
+ if mol is None:
8
+ return None
9
+ fp = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=2048)
10
+ return ",".join(map(str, list(fp)))
11
+
12
+ # Load the original CSV
13
+ df = pd.read_csv("EDC_data_sanitized.csv")
14
+
15
+ # Use the correct column: 'standardized_smiles'
16
+ df["fingerprint"] = df["standardized_smiles"].apply(smiles_to_fingerprint)
17
+
18
+ # Drop rows where fingerprint generation failed
19
+ df = df.dropna(subset=["fingerprint"])
20
+
21
+ # Save to new file
22
+ df.to_csv("edc_sanitized_with_fingerprints.csv", index=False)
23
+
sanitize_edc.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from molvs import Standardizer
3
+ from rdkit import Chem
4
+
5
+ def standardize_smiles(smiles):
6
+ mol = Chem.MolFromSmiles(smiles)
7
+ if mol is None:
8
+ return None
9
+ std = Standardizer()
10
+ try:
11
+ std_mol = std.standardize(mol)
12
+ return Chem.MolToSmiles(std_mol)
13
+ except:
14
+ return None
15
+
16
+ # Load and manually parse .txt file
17
+ data = []
18
+ with open("EDC_data.txt", "r") as f:
19
+ for line in f:
20
+ parts = line.strip().split()
21
+ if len(parts) < 6:
22
+ continue # skip malformed lines
23
+ sample_id = parts[0]
24
+ smiles = parts[1]
25
+ name = parts[3]
26
+ label = parts[-2] # sometimes it's merged, so this may need refining
27
+ source = parts[-1]
28
+ data.append((sample_id, smiles, name, label, source))
29
+
30
+ df = pd.DataFrame(data, columns=["id", "smiles", "name", "label", "source"])
31
+
32
+ # Sanitize
33
+ df["standardized_smiles"] = df["smiles"].apply(standardize_smiles)
34
+ df.dropna(subset=["standardized_smiles"], inplace=True)
35
+
36
+ # Save
37
+ df.to_csv("EDC_data_sanitized.csv", index=False)
38
+
train_classifier.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from sklearn.ensemble import RandomForestClassifier
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.metrics import (
7
+ classification_report,
8
+ confusion_matrix,
9
+ ConfusionMatrixDisplay,
10
+ roc_curve,
11
+ auc
12
+ )
13
+
14
+ # Load the dataset (adjust path if needed)
15
+ df = pd.read_csv("edc_sanitized_with_fingerprints.csv")
16
+
17
+ # Convert fingerprint column (stored as strings) back into lists
18
+ df["fingerprint"] = df["fingerprint"].apply(eval)
19
+
20
+ # Features and labels
21
+ X = np.array(df["fingerprint"].tolist())
22
+ y = df["label"].values
23
+
24
+ # Split into training and testing sets (80/20 split with stratification)
25
+ X_train, X_test, y_train, y_test = train_test_split(
26
+ X, y, test_size=0.2, stratify=y, random_state=42
27
+ )
28
+
29
+ # Train the classifier
30
+ clf = RandomForestClassifier(random_state=42)
31
+ clf.fit(X_train, y_train)
32
+
33
+ # Predictions and probabilities
34
+ y_pred = clf.predict(X_test)
35
+ y_proba = clf.predict_proba(X_test)[:, 1] # Probabilities for class 1
36
+
37
+ # Print classification report
38
+ print(classification_report(y_test, y_pred))
39
+
40
+ # --- Confusion Matrix ---
41
+ cm = confusion_matrix(y_test, y_pred)
42
+ disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=clf.classes_)
43
+ disp.plot(cmap="Blues")
44
+ plt.title("Confusion Matrix")
45
+ plt.savefig("confusion_matrix.png")
46
+ plt.show()
47
+
48
+ # --- ROC Curve ---
49
+ fpr, tpr, _ = roc_curve(y_test, y_proba)
50
+ roc_auc = auc(fpr, tpr)
51
+
52
+ plt.figure()
53
+ plt.plot(fpr, tpr, color="darkorange", lw=2, label=f"ROC curve (AUC = {roc_auc:.2f})")
54
+ plt.plot([0, 1], [0, 1], color="navy", lw=2, linestyle="--")
55
+ plt.xlim([0.0, 1.0])
56
+ plt.ylim([0.0, 1.05])
57
+ plt.xlabel("False Positive Rate")
58
+ plt.ylabel("True Positive Rate")
59
+ plt.title("Receiver Operating Characteristic (ROC) Curve")
60
+ plt.legend(loc="lower right")
61
+ plt.savefig("roc_curve.png")
62
+ plt.show()
63
+