annotate DPOGALAXY.py @ 31:3d94608aea7a draft

Uploaded
author jose_duarte
date Mon, 13 Dec 2021 11:19:23 +0000
parents ce0de724097a
children 5a0afb1578ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
1
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
2 class PDPOPrediction:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
3 def __init__(self, Folder = 'location', mdl='',seq_file = 'fasta_file.fasta',ttable=11):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
4 import pickle
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
5 import pandas as pd
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
6 from Bio import SeqIO
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
7 import os
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
8 from pathlib import Path
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
9 self.data = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
10 self.df_output = None
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
11 self.seqfile = seq_file
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
12 self.__location__ = os.path.realpath(os.path.join(os.getcwd(), Folder))
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
13
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
14 with open(os.path.join(self.__location__,mdl), 'rb') as m:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
15 self.model = pickle.load(m)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
16 if mdl == 'SVM4311':
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
17 with open(os.path.join(__location__,'d4311_SCALER'),'rb') as sl:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
18 self.scaler = pickle.load(sl)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
19 self.name = mdl
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
20 elif mdl == 'ANN7185':
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
21 with open(os.path.join(__location__,'d7185_SCALER'),'rb') as sc:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
22 self.scaler = pickle.load(sc)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
23 self.name = mdl
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
24
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
25 for seq in SeqIO.parse(os.path.join(self.__location__,self.seqfile), 'fasta'):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
26 DNA_seq = seq.seq
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
27 AA_seq = DNA_seq.translate(table=ttable)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
28 descr_seq = seq.description.replace(' ','')
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
29 self.data[descr_seq]=[DNA_seq._data,AA_seq._data]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
30 self.df = pd.DataFrame({'ID':list(self.data.keys()),
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
31 'DNAseq':[elem[0] for elem in self.data.values()],
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
32 'AAseq':[elem[1] for elem in self.data.values()]})
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
33 self.df = self.df.set_index('ID')
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
34
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
35 def Datastructure(self):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
36 import pandas as pd
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
37 import pickle
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
38 from Bio.SeqUtils.ProtParam import ProteinAnalysis
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
39 from local_ctd import CalculateCTD
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
40 from local_AAComposition import CalculateAAComposition, CalculateDipeptideComposition
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
41
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
42 def count_orf(orf_seq):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
43 dic = {'DNA-A': 0, 'DNA-C': 0, 'DNA-T': 0, 'DNA-G': 0, 'DNA-GC': 0}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
44 for letter in range(len(orf_seq)):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
45 for k in range(0, 4):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
46 if orf_seq[letter] in list(dic.keys())[k][-1]:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
47 dic[list(dic.keys())[k]] += 1
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
48 dic['DNA-GC'] = ((dic['DNA-C'] + dic['DNA-G']) / (
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
49 dic['DNA-A'] + dic['DNA-C'] + dic['DNA-T'] + dic['DNA-G'])) * 100
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
50 return dic
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
51
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
52 def count_aa(aa_seq):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
53 dic = {'G': 0, 'A': 0, 'L': 0, 'V': 0, 'I': 0, 'P': 0, 'F': 0, 'S': 0, 'T': 0, 'C': 0,
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
54 'Y': 0, 'N': 0, 'Q': 0, 'D': 0, 'E': 0, 'R': 0, 'K': 0, 'H': 0, 'W': 0, 'M': 0}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
55 for letter in range(len(aa_seq)):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
56 if aa_seq[letter] in dic.keys():
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
57 dic[aa_seq[letter]] += 1
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
58 return dic
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
59
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
60 def sec_st_fr(aa_seq):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
61 from Bio.SeqUtils.ProtParam import ProteinAnalysis
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
62 st_dic = {'Helix': 0, 'Turn': 0, 'Sheet': 0}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
63 stu = ProteinAnalysis(aa_seq).secondary_structure_fraction()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
64 st_dic['Helix'] = stu[0]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
65 st_dic['Turn'] = stu[1]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
66 st_dic['Sheet'] = stu[2]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
67 return st_dic
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
68
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
69 self.feat={"SVM4311": ["DNA-A", "DNA-T", "DNA-G", "DNA-GC", "AA_Len", "G", "A", "S", "T", "N", "Turn", "Sheet",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
70 "_PolarizabilityC1", "_PolarizabilityC3", "_SolventAccessibilityC1", "_SecondaryStrC1",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
71 "_SecondaryStrC2", "_SecondaryStrC3", "_ChargeC2", "_ChargeC3", "_PolarityC1", "_NormalizedVDWVC1",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
72 "_NormalizedVDWVC3", "_HydrophobicityC2", "_HydrophobicityC3", "_SecondaryStrT23",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
73 "_NormalizedVDWVT13", "_PolarizabilityD1001", "_SolventAccessibilityD1001", "_SolventAccessibilityD2001",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
74 "_SolventAccessibilityD3001", "_SecondaryStrD1025", "_ChargeD1075","_ChargeD2001", "_ChargeD2025",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
75 "_ChargeD3025", "_ChargeD3050", "_PolarityD1075", "_PolarityD3025","_NormalizedVDWVD1001",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
76 "_NormalizedVDWVD3050", "_HydrophobicityD2001", "DG", "DT", "GD"],
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
77 "ANN7185": ["DNA-GC", "AA_Len", "Aromaticity", "IsoelectricPoint", "G", "A", "L", "V", "I", "P", "F",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
78 "S", "T", "C", "Y", "N", "Q", "D", "E", "R", "K", "H", "W", "M", "Turn", "Sheet", "_PolarizabilityC1",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
79 "_PolarizabilityC2", "_PolarizabilityC3", "_SolventAccessibilityC1", "_SolventAccessibilityC2",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
80 "_SecondaryStrC1", "_SecondaryStrC3", "_ChargeC1", "_ChargeC2", "_ChargeC3", "_PolarityC2",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
81 "_NormalizedVDWVC2", "_NormalizedVDWVC3", "_HydrophobicityC1", "_HydrophobicityC2", "_SecondaryStrT13",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
82 "_SecondaryStrT23", "_ChargeT12", "_ChargeT13", "_HydrophobicityT12", "_PolarizabilityD1001",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
83 "_PolarizabilityD1025", "_PolarizabilityD1050", "_PolarizabilityD2001", "_PolarizabilityD3025",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
84 "_PolarizabilityD3050", "_PolarizabilityD3075", "_SolventAccessibilityD1050", "_SolventAccessibilityD2001",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
85 "_SolventAccessibilityD2025", "_SolventAccessibilityD2050", "_SolventAccessibilityD3025",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
86 "_SolventAccessibilityD3050", "_SolventAccessibilityD3100", "_SecondaryStrD1025", "_SecondaryStrD1050",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
87 "_SecondaryStrD1075", "_SecondaryStrD2001", "_SecondaryStrD2050", "_SecondaryStrD2075", "_ChargeD1050",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
88 "_ChargeD1075", "_ChargeD1100", "_ChargeD2025", "_ChargeD3025", "_ChargeD3050", "_PolarityD2050",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
89 "_PolarityD3050", "_NormalizedVDWVD1001", "_NormalizedVDWVD1050", "_NormalizedVDWVD2001", "_NormalizedVDWVD2025",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
90 "_HydrophobicityD3001", "_HydrophobicityD3075", "AD", "AW", "AY", "RC", "RT", "NA", "NE",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
91 "NG", "NP", "DE", "DQ", "DG", "DT", "DY", "CG", "CL", "CY", "CV", "EN", "QA", "QR", "QE",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
92 "QI", "GA", "GR", "GD", "GQ", "GG", "GH", "GL", "GF", "GP", "GT", "GY", "HA", "HC", "HI",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
93 "HK", "HP", "IC", "IG", "IS", "IT", "IW", "LA", "LR", "LH", "LI", "LK", "LP", "KQ", "KH",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
94 "KS", "KT", "MQ", "MG", "MI", "FA", "FR", "FS", "FY", "PC", "PE", "PG", "PH", "PM", "PF",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
95 "PT", "SA", "SD", "SC", "SQ", "SW", "TA", "TC", "TM", "WL", "WV", "YE", "YG", "YH", "YI",
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
96 "YL", "YK", "YM", "YS"]}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
97
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
98 self.df_output = self.df.copy()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
99 self.df_output.drop(['DNAseq','AAseq'],axis=1,inplace=True)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
100 dna_feat = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
101 aa_len = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
102 aroma_dic = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
103 iso_dic = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
104 aa_content = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
105 st_dic_master = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
106 CTD_dic = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
107 dp = {}
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
108 for i in range(len(self.df)):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
109 i_name = self.df.index[i]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
110 dna_feat[i_name] = count_orf(self.df.iloc[i]['DNAseq'])
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
111 aa_len[i_name] = len(self.df.iloc[i]['AAseq'])
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
112 aroma_dic[i_name] = ProteinAnalysis(self.df.iloc[i]['AAseq']).aromaticity()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
113 iso_dic[i_name] = ProteinAnalysis(self.df.iloc[i]['AAseq']).isoelectric_point()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
114 aa_content[i_name] = count_aa(self.df.iloc[i]['AAseq'])
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
115 st_dic_master[i_name] = sec_st_fr(self.df.iloc[i]['AAseq'])
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
116 CTD_dic[i_name] = CalculateCTD(self.df.iloc[i]['AAseq'])
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
117 dp[i_name] = CalculateDipeptideComposition(self.df.iloc[i]['AAseq'])
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
118 for j in self.df.index:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
119 self.df.loc[j, dna_feat[j].keys()] = dna_feat[j].values() #dic with multiple values
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
120 self.df.loc[j, 'AA_Len'] = int(aa_len[j]) #dic with one value
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
121 self.df.loc[j, 'Aromaticity'] = aroma_dic[j]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
122 self.df.loc[j, 'IsoelectricPoint'] = iso_dic[j]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
123 self.df.loc[j, aa_content[j].keys()] = aa_content[j].values()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
124 self.df.loc[j, st_dic_master[j].keys()] = st_dic_master[j].values()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
125 self.df.loc[j, CTD_dic[j].keys()] = CTD_dic[j].values()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
126 self.df.loc[j, dp[j].keys()] = dp[j].values()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
127 self.df.drop(['DNAseq','AAseq'],axis=1,inplace=True)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
128
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
129 def Prediction(self):
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
130 import os
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
131 import pickle
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
132 import json
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
133 import pandas as pd
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
134 import numpy as np
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
135 from pathlib import Path
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
136 ft_scaler = pd.DataFrame(self.scaler.transform(self.df.iloc[:, :]), index=self.df.index,columns=self.df.columns)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
137 ft_scaler = ft_scaler.drop(columns=[col for col in self.df if col not in self.feat[self.name]], axis=1)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
138 scores = self.model.predict_proba(ft_scaler)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
139 pos_scores = np.empty((self.df.shape[0], 0), float)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
140 for x in scores:
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
141 pos_scores = np.append(pos_scores, round(x[1]*100))
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
142 self.df_output.reset_index(inplace=True)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
143 self.df_output['{} DPO Prediction (%)'.format(self.name)]= pos_scores
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
144 #self.df_output = self.df_output.sort_values(by='{} DPO Prediction (%)'.format(self.name), ascending=False)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
145 self.df_output.to_html('output.html', index=False, justify='center')
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
146
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
147 if __name__ == '__main__':
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
148 import os
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
149 import sys
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
150 __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
151
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
152 model = sys.argv[1]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
153 fasta_file = sys.argv[2]
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
154
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
155 PDPO = PDPOPrediction(__location__,model,fasta_file)
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
156 PDPO.Datastructure()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
157 PDPO.Prediction()
ce0de724097a Uploaded
jose_duarte
parents:
diff changeset
158