comparison DPOGALAXY.py @ 15:0940c145e15a draft

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