-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_bact.py
More file actions
160 lines (128 loc) · 4.33 KB
/
find_bact.py
File metadata and controls
160 lines (128 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pandas as pd
import numpy as np
#Input file, database name and blast output xml
queryID="salmonella-fake.fa"
dbID="operons.100.fa"
outputID="out.xml"
#Biopython runs blast through the command line
from Bio.Blast.Applications import NcbiblastnCommandline
blastn_cline = NcbiblastnCommandline(query=queryID,db =dbID,
outfmt=5, out=outputID)
stdout, stderr = blastn_cline()
#This section processes the information from the blast xml output into something useful.
#Using element tree package to retrieve relevent xml information
import xml.etree.ElementTree as ET
tree = ET.parse(outputID)
root = tree.getroot()
df = pd.DataFrame(columns=['hit_id','e_value', 'Hsp_score', 'Hit_len'])
#Builds xml data into dataframe
j=0
iterator=root.iter('Hit_id')
for i in iterator:
df.loc[j] = i.text
j+=1
k = 0
iterator=root.iter('Hsp_evalue')
for i in iterator:
df['e_value'].loc[k] = i.text
k+=1
l = 0
iterator=root.iter('Hsp_score')
for i in iterator:
df['Hsp_score'].loc[l] = i.text
l+=1
m = 0
iterator=root.iter('Hit_len')
for i in iterator:
df['Hit_len'].loc[m] = i.text
m+=1
#Calculates percentage read match and filters result
df['Hsp_score'] = pd.to_numeric(df['Hsp_score'])#converts string to number
df['Hit_len'] = pd.to_numeric(df['Hit_len'])#converts string to number
df['Result'] = df['Hsp_score']/df['Hit_len']*100
blast_res = df
blast_res['species'] = ''
#df[(df['Result']>=30)] #filters results to >= 30% match
df = pd.read_csv('species_annotation.csv', sep = '\t',header=-1,names = ['species','operons','GenBank','operon_length'])
df['species'] = df['species'].str.replace('_',' ')
my_file = open('operons.txt','r')
data = []
for line in my_file:
data.append(line)
df2 = pd.DataFrame(columns=['operons','seq'],index=list(range(len(data))))
df2['data'] = data
def GetOperand(x):
if '>' in x:
return x
def GetSeq(x):
if '>' not in x:
return x
df2['operons'] = df2['data'].apply(GetOperand)
df2['seq'] = df2['data'].apply(GetSeq)
df2['operons'] = df2['operons'].ffill()
df2 = df2.dropna()
df2 = df2.drop('data',1)
df3 = df2.groupby('operons').agg(lambda x:x.tolist())
df3 = df3.reset_index()
df3['seq'] = df3['seq'].apply(lambda x:''.join(x))
df3['operons'] = df3['operons'].str.replace('>','').str.replace('\n','')
final = df3
diseases = ['amoebiasis',
'entamoeba histolytica',
'cryptosporidiosis',
'cryptosporidium parvum',
'cyclosporiasis',
'cyclospora cayetanensis',
'giardiasis',
'giardia lamblia',
'microsporidiosis',
'microsporidia',
'botulism',
'clostridium botulinum',
'campylobacteriosis',
'camploybacter jejuni',
'cholera',
'vibrio cholerae',
'e.eoli infection',
'escherichia coli',
'm.marinum infection',
'mycobacterium marinum',
'dysentery',
'shigella dysenterae',
'legionellosis',
'legionella pneumophilia',
'leptospirosis',
'leptospira',
'otitis externa',
'salmonellosis',
'salmonella',
'typhoid fever',
'salmonella typhi',
'vibrio illness',
'vibrio vulnificus',
'vibrio alginolyticus',
'vibrio parahaemolyticus',
'sars',
'coronavirus',
'hepatitis A',
'poliomyelitis',
'polio',
'poliovirus',
'polyomavirus infection',
'polyomavirus',
'jc virus',
'bk virus',
'desmodesmus',
'desmodesmus armatus'
]
full_join = pd.merge(df,final)
water_dis = full_join[full_join['species'].str.lower().isin(diseases)]
def GetInd(x):
idx = full_join.index[full_join['operons']==x]
return full_join['species'][idx[0]]
blast_res['species'] = blast_res['hit_id'].apply(GetInd)
blast_res_qual = blast_res[blast_res['Result']>=90]
results = (blast_res_qual.groupby('species')['hit_id'].count())
pathogens = []
for i in range(len(reuslts)):
pathogens.append(results.index[i])