annotate phage_host_prediction/run_galaxy.py @ 2:3e1e8be4e65c draft default tip

Uploaded
author pedro_araujo
date Fri, 02 Apr 2021 10:11:13 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
2
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
3 class GalaxyPrediction:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
4
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
5 def __init__(self, phage_input_type='ID', bact_input_type='ID', phage='', bacteria='', ml_model='RandomForests', run_interpro=False):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
6 import pickle
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
7 import os
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
8 import re
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
9 with open('files/feature_dataset', 'rb') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
10 dataset = pickle.load(f)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
11 self.all_phages = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
12 self.all_bacteria = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
13 for ID in dataset.index:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
14 temp_phage = ID[:ID.find('--')]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
15 temp_bacteria = ID[ID.find('--')+2:]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
16 if temp_phage not in self.all_phages:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
17 self.all_phages.append(temp_phage)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
18 if temp_bacteria not in self.all_bacteria:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
19 self.all_bacteria.append(temp_bacteria)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
20 if phage_input_type == 'ID':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
21 phage = re.split('\W', phage.replace(' ', ''))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
22 len_phage_id = len(phage)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
23 phage_seqs = self._retrieve_from_phage_id(phage)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
24 elif phage_input_type == 'seq_file':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
25 phage_seqs = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
26 phage_seqs['PhageFasta'] = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
27 with open(phage, 'r') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
28 temp = f.readlines()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
29 count_prot = 0
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
30 prot = ''
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
31 i=0
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
32 while i < len(temp):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
33 if '>' in temp[i]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
34 if prot:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
35 phage_seqs['PhageFasta']['Protein' + str(count_prot)] = ['Unknown', prot]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
36 count_prot += 1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
37 prot = ''
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
38 i+=1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
39 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
40 prot += temp[i].strip()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
41 i+=1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
42 if bact_input_type == 'ID':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
43 bacteria = re.split('\W', bacteria.replace(' ', ''))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
44 if len(bacteria) > 1 and len_phage_id == 1 or len(bacteria) == 1:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
45 bact_seqs = self._retrieve_from_bact_id(bacteria)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
46 elif bact_input_type == 'seq_file':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
47 bact_seqs = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
48 bact_seqs['BacteriaFasta'] = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
49 with open(bacteria, 'r') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
50 temp = f.readlines()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
51 count_prot = 0
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
52 prot = ''
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
53 i=0
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
54 while i < len(temp):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
55 if '>' in temp[i]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
56 if prot:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
57 bact_seqs['BacteriaFasta']['Protein' + str(count_prot)] = ['Unknown', prot]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
58 count_prot += 1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
59 prot = ''
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
60 i+=1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
61 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
62 prot += temp[i].strip()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
63 i+=1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
64 phage_seqs = self._find_phage_functions(phage_seqs, run_interpro)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
65 phage_seqs = self._find_phage_tails(phage_seqs)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
66
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
67 list_remove = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
68 for org in phage_seqs:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
69 if not phage_seqs[org]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
70 print('Could not find tails for phage ' + org + '. Deleting entry...')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
71 list_remove.append(org)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
72 for org in list_remove:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
73 del phage_seqs[org]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
74
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
75 if phage_seqs:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
76 output = self.run_prediction(phage_seqs, bact_seqs, ml_model)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
77 self.create_output(output, phage_seqs, bact_seqs)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
78 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
79 with open(or_location + '/output.tsv', 'w') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
80 f.write('No phage tails found in query')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
81 for file in os.listdir('files'):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
82 if file.startswith('temp'):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
83 os.remove('files/' + file)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
84
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
85 def _retrieve_from_phage_id(self, phage):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
86 temp_phage = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
87 for ID in phage:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
88 temp_phage[ID] = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
89 if ID in self.all_phages:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
90 import json
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
91 with open('files/phageTails.json', encoding='utf-8') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
92 phage_tails = json.loads(f.read())
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
93 temp_phage[ID] = phage_tails[ID]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
94 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
95 from Bio import Entrez
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
96 from Bio import SeqIO
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
97 phage = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
98 Entrez.email = 'insert@email.com'
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
99 try:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
100 with Entrez.efetch(db="nucleotide", rettype="gb", retmode="text", id=ID) as handle:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
101 genome = SeqIO.read(handle, "gb")
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
102 for feat in genome.features:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
103 if feat.type == 'CDS':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
104 try: temp_phage[ID][feat.qualifiers['protein_id'][0]] = [feat.qualifiers['product'][0], feat.qualifiers['translation'][0]]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
105 except: pass
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
106 except:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
107 print(ID, 'not found in GenBank')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
108 return temp_phage
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
109
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
110 def _retrieve_from_bact_id(self, bacteria):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
111 temp_bacteria = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
112 for ID in bacteria:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
113 temp_bacteria[ID] = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
114 if '.' in ID:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
115 ID = ID[:ID.find('.')]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
116 #if ID in self.all_bacteria:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
117 # import json
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
118 # with open('files/bacteria/' + ID + '.json', encoding='utf-8') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
119 # temp_bacteria[ID] = json.loads(f.read())
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
120 #else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
121 from Bio import Entrez
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
122 from Bio import SeqIO
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
123 bacteria = {}
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
124 Entrez.email = 'insert@email.com'
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
125 try:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
126 with Entrez.efetch(db="nucleotide", rettype="gb", retmode="text", id=ID+'.1') as handle:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
127 genome = SeqIO.read(handle, "gb")
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
128 for feat in genome.features:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
129 if feat.type == 'CDS':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
130 try: temp_bacteria[ID][feat.qualifiers['protein_id'][0]] = [feat.qualifiers['product'][0], feat.qualifiers['translation'][0]]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
131 except: pass
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
132 if len(genome.features) <= 5:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
133 with Entrez.efetch(db="nucleotide", rettype="gbwithparts", retmode="text", id=ID) as handle:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
134 genome = handle.readlines()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
135 for i in range(len(genome)):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
136 if ' CDS ' in genome[i]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
137 j = i
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
138 protDone = False
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
139 while j < len(genome):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
140 if protDone:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
141 break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
142 if '/product=' in genome[j]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
143 product = genome[j].strip()[10:]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
144 j += 1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
145 elif '_id=' in genome[j]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
146 protKey = genome[j].strip()[13:-1]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
147 j += 1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
148 elif '/translation=' in genome[j]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
149 protSeq = genome[j].strip()[14:]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
150 j += 1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
151 for k in range(j, len(genome)):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
152 if genome[k].islower():
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
153 j = k
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
154 protDone = True
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
155 break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
156 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
157 protSeq += genome[k].strip()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
158 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
159 j += 1
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
160 temp_bacteria[ID][protKey] = [product, protSeq[:protSeq.find('"')]]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
161 except:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
162 print(ID, 'not found in GenBank')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
163 return temp_bacteria
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
164
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
165 def _find_phage_functions(self, phage_dict, run_interpro):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
166 import os
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
167 import json
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
168 with open('files/known_function.json', encoding='utf-8') as F:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
169 known_function = json.loads(F.read())
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
170 with open('files/temp_database.fasta', 'w') as F:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
171 for phage in known_function:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
172 for prot in known_function[phage]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
173 F.write('>' + phage + '-' + prot + '\n' + known_function[phage][prot][1] + '\n')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
174 os.system('makeblastdb -in files/temp_database.fasta -dbtype prot -title PhageProts -parse_seqids -out files/temp_database -logfile files/temp_log')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
175 for org in phage_dict:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
176 with open('files/temp.fasta', 'w') as F:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
177 for prot in phage_dict[org]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
178 F.write('>' + prot + '\n' + phage_dict[org][prot][1] + '\n')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
179 os.system('blastp -db files/temp_database -query files/temp.fasta -out files/temp_blast -num_threads 2 -outfmt 6')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
180 phage_dict[org] = self.process_blast(phage_dict[org], known_function)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
181 if run_interpro: phage_dict[org] = self.interpro(phage_dict[org])
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
182 return phage_dict
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
183
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
184 def process_blast(self, phage_dict, known_function):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
185 import pandas as pd
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
186 import re
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
187 blast_domains = pd.read_csv('files/temp_blast', sep='\t', header=None)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
188 for prot in phage_dict:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
189 func = phage_dict[prot][0]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
190 known = False
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
191 if (not any(i in func.lower() for i in ['hypothetical', 'unknown', 'kda', 'uncharacterized', 'hyphothetical']) and len(func) > 3) and not ('gp' in func.lower() and len(func.split(' ')) < 2) and not (len(func.split(' ')) == 1 and len(func) < 5):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
192 known = True
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
193 if not known:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
194 evalue = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
195 bitscore = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
196 pred = blast_domains[blast_domains[0] == prot]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
197 if pred.shape[0] == 0: break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
198 for i in pred[10]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
199 evalue.append(float(i))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
200 for i in pred[11]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
201 bitscore.append(float(i))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
202 if min(evalue) < 1.0 and max(bitscore) > 30.0:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
203 ind = evalue.index(min(evalue))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
204 if ind != bitscore.index(max(bitscore)):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
205 ind = bitscore.index(max(bitscore))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
206 temp = pred.iloc[ind, 1]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
207 known_phage = temp[:temp.find('-')]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
208 known_prot = temp[temp.find('-') + 1:]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
209 if known_function[known_phage][known_prot]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
210 new_func = known_function[known_phage][known_prot][0]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
211 # for j in known_function.keys():
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
212 # if pred.iloc[ind, 1] in known_function[j].keys():
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
213 # new_func = known_function[j][pred.iloc[ind, 1]][0]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
214 # break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
215 x = re.findall('(Gp\d{2,}[^,\d -]|Gp\d{1}[^,\d -])', temp) # se tiver hits, remover
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
216 if not any(z in new_func.lower() for z in ['unknown', 'ucp', 'uncharacterized', 'consensus']) and len(new_func) > 3 and not x:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
217 phage_dict[prot][0] = new_func
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
218 return phage_dict
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
219
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
220 def interpro(self, phage_dict):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
221 import os
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
222 import pandas as pd
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
223 import re
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
224 os.system('interproscan.sh -b ' + 'files/temp_interpro -i ' + 'files/temp.fasta -f tsv > files/temp_interpro_log')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
225 domains = pd.read_csv('files/temp_interpro.tsv', sep='\t', index_col=0, header=None, names=list(range(13)))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
226 domains = domains.fillna('-')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
227 domains = domains[domains.loc[:, 3] != 'Coils']
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
228 domains = domains[domains.loc[:, 3] != 'MobiDBLite']
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
229 for prot in phage_dict:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
230 func = phage_dict[prot][0]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
231 known = False
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
232 if (not any(i in func.lower() for i in ['hypothetical', 'unknown', 'kda', 'uncharacterized', 'hyphothetical']) and len(func) > 3) and not ('gp' in func.lower() and len(func.split(' ')) < 2) and not (len(func.split(' ')) == 1 and len(func) < 5):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
233 known = True
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
234 if prot in domains.index and not known:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
235 temp = '-'
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
236 try:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
237 for i in range(domains.loc[prot, :].shape[0]):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
238 if '-' not in domains.loc[prot, 12].iloc[i].lower():
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
239 if float(domains.loc[prot, 8].iloc[i]) < 1.0:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
240 temp = domains.loc[prot, 12].iloc[i]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
241 break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
242 except:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
243 if float(domains.loc[prot, 8]) < 1.0:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
244 temp = domains.loc[prot, 12]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
245 x = re.findall('(Gp\d{2,}[^,\d -]|Gp\d{1}[^,\d -])', temp) # se tiver hits, remover
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
246 if temp != '-' and not any(z in temp.lower() for z in ['unknown', 'ucp', 'uncharacterized', 'consensus']) and len(temp) > 3 and not x:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
247 phage_dict[prot][0] = temp
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
248 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
249 try:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
250 for i in range(domains.loc[prot, :].shape[0]):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
251 if '-' not in domains.loc[prot, 5].iloc[i].lower():
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
252 temp = domains.loc[prot, 5].iloc[i]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
253 break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
254 except:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
255 temp = domains.loc[prot, 5]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
256 x = re.findall('(Gp\d{2,}[^,\d -]|Gp\d{1}[^,\d -])', temp)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
257 if temp != '-' and not any(z in temp.lower() for z in ['unknown', 'ucp', 'uncharacterized', 'consensus']) and len(temp) > 3 and not x:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
258 phage_dict[prot][0] = temp
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
259 return phage_dict
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
260
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
261 def _find_phage_tails(self, phage_dict):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
262 for org in phage_dict:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
263 list_remove = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
264 for protein in phage_dict[org]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
265 if any(z in phage_dict[org][protein][0].lower() for z in ['fiber', 'fibre', 'spike', 'hydrolase', 'bind', 'depolymerase', 'peptidase', 'lyase', 'sialidase', 'dextranase', 'lipase', 'adhesin', 'baseplate', 'protein h', 'recognizing', 'protein j', 'protein g', 'gpe', 'duf4035', 'host specifity', 'cor protein', 'specificity', 'baseplate component', 'gp38', 'gp12 tail', 'receptor', 'recognition', 'tail']) \
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
266 and not any(z in phage_dict[org][protein][0].lower() for z in ['nucle', 'dna', 'rna', 'ligase', 'transferase', 'inhibitor', 'assembly', 'connect', 'nudix', 'atp', 'nad', 'transpos', 'ntp', 'molybdenum', 'hns', 'gtp', 'riib', 'inhibitor', 'replicat', 'codon', 'pyruvate', 'catalyst', 'hinge', 'sheath completion', 'head', 'capsid', 'tape', 'tip', 'strand', 'matur', 'portal', 'terminase', 'nucl', 'promot', 'block', 'olfact', 'wedge', 'lysozyme', 'mur', 'sheat']):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
267 pass
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
268 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
269 list_remove.append(protein)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
270 for protein in list_remove:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
271 del phage_dict[org][protein]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
272 return phage_dict
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
273
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
274 def run_prediction(self, phage_dict, bact_dict, ml_model):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
275 from feature_construction import FeatureConstruction
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
276 import pickle
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
277 from sklearn.preprocessing import LabelEncoder
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
278 from sklearn.preprocessing import StandardScaler
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
279 import numpy as np
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
280
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
281 if ml_model == 'RandomForests':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
282 with open('files/dataset_reduced', 'rb') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
283 dataset = pickle.load(f)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
284 columns_remove = [3, 7, 9, 11, 24, 28, 32, 34, 38, 42, 45, 52, 53, 61, 65, 73, 75, 79, 104, 122, 141, 151, 154, 155, 157, 159, 160, 161, 163, 165, 169, 170, 173, 176, 178, 180, 182, 183, 185, 186, 187, 190, 193, 194, 196, 197, 201, 202, 203, 206, 207, 209, 210, 212, 216, 217, 221, 223, 225, 226, 230, 233, 235, 236, 245, 251]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
285 elif ml_model == 'SVM':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
286 with open('files/feature_dataset', 'rb') as f:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
287 dataset = pickle.load(f)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
288 columns_remove = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
289
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
290 dataset = dataset.dropna()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
291 le = LabelEncoder()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
292 le.fit(['Yes', 'No'])
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
293 output = le.transform(dataset['Infects'].values)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
294 dataset = dataset.drop('Infects', 1)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
295 scaler = StandardScaler()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
296 scaler.fit(dataset)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
297 data_z = scaler.transform(dataset)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
298
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
299 fc = FeatureConstruction()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
300 solution = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
301 for phage in phage_dict:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
302 for bacteria in bact_dict:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
303 temp_solution = np.array([])
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
304 temp_solution = np.append(temp_solution, fc.get_grouping(phage_dict[phage], bact_dict[bacteria]))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
305 temp_solution = np.append(temp_solution, fc.get_composition(phage_dict[phage], bact_dict[bacteria]))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
306 temp_solution = np.append(temp_solution, fc.get_kmers(phage_dict[phage], bact_dict[bacteria]))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
307 temp_solution = temp_solution.reshape(1, -1)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
308 if columns_remove:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
309 temp_solution = np.delete(temp_solution, columns_remove, 1)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
310 if phage in self.all_phages:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
311 for ID in dataset.index:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
312 if phage in ID:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
313 for i in range(len(dataset.loc[ID].index)):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
314 if 'phage' in dataset.loc[ID].index[i]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
315 temp_solution[0][i] = dataset.loc[ID, dataset.loc[ID].index[i]]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
316 break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
317 if bacteria in self.all_bacteria:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
318 for ID in dataset.index:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
319 if bacteria in ID:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
320 for i in range(len(dataset.loc[ID].index)):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
321 if 'bact' in dataset.loc[ID].index[i]:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
322 temp_solution[0][i] = dataset.loc[ID, dataset.loc[ID].index[i]]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
323 break
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
324 if type(solution) != np.ndarray:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
325 solution = temp_solution
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
326 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
327 solution = np.append(solution, temp_solution, axis=0)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
328 # solution = solution.reshape(1, -1)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
329 solution = scaler.transform(solution)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
330
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
331 if ml_model == 'RandomForests':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
332 from sklearn.ensemble import RandomForestClassifier
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
333 clf = RandomForestClassifier(n_estimators=200, bootstrap=False, criterion='gini', min_samples_leaf=2, min_samples_split=4, oob_score=False)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
334 clf = clf.fit(data_z, output)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
335 elif ml_model == 'SVM':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
336 from sklearn.svm import SVC
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
337 clf = SVC(C=10, degree=2, gamma='auto', kernel='rbf')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
338 clf = clf.fit(data_z, output)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
339 pred = clf.predict(solution)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
340 pred = list(le.inverse_transform(pred))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
341 return pred
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
342
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
343 def create_output(self, output, phage_seqs, bact_seqs):
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
344 import pandas as pd
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
345 list_orgs = []
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
346 for phage in phage_seqs:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
347 for bact in bact_seqs:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
348 list_orgs.append(phage + ' - ' + bact)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
349 file = pd.DataFrame({'Phage - Bacteria': list_orgs, 'Infects': output})
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
350 file.to_csv('files/output.tsv', sep='\t', index=False, header=True)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
351 file.to_csv(or_location + '/output.tsv', sep='\t', index=False, header=True)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
352
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
353
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
354 if __name__ == '__main__':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
355 import sys
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
356 import os
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
357 global or_location
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
358 or_location = os.getcwd()
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
359 os.chdir(os.path.dirname(__file__))
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
360
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
361 phage_input_type = sys.argv[1]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
362 Phages = sys.argv[2]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
363 bact_input_type = sys.argv[3]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
364 Bacts = sys.argv[4]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
365 run_interpro = sys.argv[5]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
366 if run_interpro == 'True':
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
367 run_interpro = True
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
368 else:
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
369 run_interpro = False
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
370 model = sys.argv[6]
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
371 GalaxyPrediction(phage_input_type=phage_input_type, bact_input_type=bact_input_type, phage=Phages, bacteria=Bacts, ml_model=model, run_interpro=run_interpro)
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
372 #rg = GalaxyPrediction(phage_input_type='ID', bact_input_type='ID', phage='NC_050154', bacteria='NC_007414,NZ_MK033499,NZ_CP031214')
3e1e8be4e65c Uploaded
pedro_araujo
parents:
diff changeset
373 # GalaxyPrediction(phage_input_type='ID', bact_input_type='ID', phage='NC_031087,NC_049833,NC_049838,NC_049444', bacteria='LR133964', ml_model='SVM')