annotate DPOGALAXY.py @ 18:bb7cbcd20bf2 draft

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