Mercurial > repos > rakesh4osdd > asist
comparison clsi_profile_type2.py @ 0:c1a77856070c draft
"planemo upload for repository https://github.com/rakesh4osdd/asist/tree/master commit f5b374bef15145c893ffdd3a7d2f2978d8052184-dirty"
author | rakesh4osdd |
---|---|
date | Sat, 26 Jun 2021 07:27:53 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c1a77856070c |
---|---|
1 #!/usr/bin/env python | |
2 # coding: utf-8 | |
3 | |
4 # In[1245]: | |
5 | |
6 | |
7 # ASIST module2 | map AST result to the CLSI breakporints with combination antibiotics | |
8 # By rakesh4osdd@gmail.com, 06-Jun-2021 | |
9 import pandas as pd | |
10 import re | |
11 import sys | |
12 | |
13 | |
14 # In[1246]: | |
15 | |
16 | |
17 # cross check MIC values | |
18 def sus_res(mic): | |
19 #print(mic) | |
20 #print ((mic,pd.isna(mic[1]),pd.isna(mic[0]),'\n') | |
21 if not (pd.isna(mic[0]) or pd.isna(mic[1]) or pd.isna(mic[2])):#check for NaN value | |
22 #remove unwanted whitespace | |
23 o_mic = mic[0].replace(' ', '') | |
24 s_mic = mic[1].replace(' ', '') | |
25 r_mic = mic[2].replace(' ', '') | |
26 #print (o_mic,s_mic,r_mic) | |
27 #print (type(o_mic),type(s_mic),type(r_mic)) | |
28 if '/' in s_mic: #check for combination antibiotics | |
29 #print ('combination antibiotics') | |
30 try: | |
31 if '/' in o_mic: | |
32 #print ('input combination') | |
33 if (float(o_mic.split('/')[0]) <= float(s_mic.split('/')[0]) and float(o_mic.split('/')[1]) <= float(s_mic.split('/')[1])): | |
34 strain_type='Susceptible' | |
35 elif (float(o_mic.split('/')[0]) >= float(r_mic.split('/')[0]) and float(o_mic.split('/')[1]) >= float(r_mic.split('/')[1])): | |
36 strain_type='Resistant' | |
37 else: | |
38 strain_type='Intermediate' | |
39 else: | |
40 #print ('single') | |
41 if float(o_mic)==0: | |
42 strain_type='Strain could not classified' | |
43 elif (float(o_mic.split('/')[0]) <= float(s_mic.split('/')[0]) and float(1) <= float(s_mic.split('/')[1])): | |
44 strain_type='Susceptible' | |
45 elif (float(o_mic.split('/')[0]) >= float(r_mic.split('/')[0]) and float(1) >= float(r_mic.split('/')[1])): | |
46 strain_type='Resistant' | |
47 else: | |
48 strain_type='Intermediate' | |
49 except ValueError: | |
50 strain_type='Strain could not classified' | |
51 else: #single antibiotics | |
52 #print('single antibiotics') | |
53 if o_mic: | |
54 if float(o_mic)==0: | |
55 strain_type='Strain could not classified' | |
56 elif (float(o_mic) <= float(s_mic)): | |
57 strain_type='Susceptible' | |
58 elif (float(o_mic) >= float(r_mic)): | |
59 strain_type='Resistant' | |
60 else: | |
61 strain_type='Intermediate' | |
62 else: | |
63 strain_type='Strain could not classified' | |
64 else: | |
65 strain_type='Strain could not classified' | |
66 return(strain_type) | |
67 | |
68 | |
69 # In[ ]: | |
70 | |
71 # for input argument | |
72 input_user = sys.argv[1] | |
73 input_clsi = sys.argv[2] | |
74 output_table = sys.argv[3] | |
75 | |
76 # In[1247]: | |
77 | |
78 | |
79 """ | |
80 input_user='input_ast_comb2.csv' | |
81 input_clsi='clsi_profile_comb.csv' | |
82 output_profile=input_user+'_profile.csv' | |
83 output_table=input_user+'_table.csv' | |
84 | |
85 """ | |
86 | |
87 # In[1248]: | |
88 | |
89 | |
90 # read user AST data with selected 3 columns | |
91 strain_mic=pd.read_csv(input_user, sep=',', usecols =['Strain name', 'Antibiotics', 'MIC'],na_filter=False) | |
92 | |
93 | |
94 # In[1249]: | |
95 | |
96 | |
97 clsi_bp=pd.read_csv(input_clsi,sep=',') | |
98 | |
99 | |
100 # In[1290]: | |
101 | |
102 | |
103 #clsi_bp.head(2) | |
104 #strain_mic | |
105 | |
106 | |
107 # In[1251]: | |
108 | |
109 | |
110 # convert MIC to numbers sMIC, rMIC | |
111 clsi_bp['s_mic'] =clsi_bp[['Susceptible']].applymap(lambda x: (re.sub(r'[^0-9.\/]', '', x))) | |
112 clsi_bp['r_mic'] =clsi_bp[['Resistant']].applymap(lambda x: (re.sub(r'[^0-9.\/]', '', x))) | |
113 | |
114 | |
115 # In[1252]: | |
116 | |
117 | |
118 # Read only numbers in MIC values | |
119 #try: | |
120 strain_mic['o_mic']=strain_mic[['MIC']].applymap(lambda x: (re.sub(r'[^0-9.\/]','', x))) | |
121 #except TypeError: | |
122 # print('Waring: Error in MIC value') | |
123 | |
124 | |
125 # In[1289]: | |
126 | |
127 | |
128 #strain_mic.head() | |
129 | |
130 | |
131 # In[1254]: | |
132 | |
133 | |
134 # capitalize each Antibiotic Name for comparision with removing whitespace | |
135 strain_mic['Antibiotics']=strain_mic['Antibiotics'].str.capitalize().str.replace(" ","") | |
136 clsi_bp['Antibiotics']=clsi_bp['Antibiotics'].str.capitalize().str.replace(" ","") | |
137 | |
138 | |
139 # In[1255]: | |
140 | |
141 | |
142 #compare CLSI Antibiotics only | |
143 #result=pd.merge(strain_mic, clsi_bp, on='Antibiotics',how='inner', indicator=True)[['Strain name','Antibiotics', 'MIC', 'o_mic', 's_mic', 'r_mic','_merge']] | |
144 try: | |
145 result=pd.merge(strain_mic, clsi_bp, on='Antibiotics',how='inner')[['Strain name','Antibiotics', 'MIC', 'o_mic', 's_mic', 'r_mic']] | |
146 except KeyError: | |
147 print('Waring: Error in input Values') | |
148 | |
149 | |
150 # In[1256]: | |
151 | |
152 | |
153 #compare MIC values and assign Susceptible and Resistant to Strain | |
154 #try: | |
155 result[['CLSI_profile']] = result[['o_mic','s_mic','r_mic']].apply(sus_res,axis = 1) | |
156 #except ValueError: | |
157 # print('Waring: Error in input MIC value') | |
158 | |
159 | |
160 # In[1288]: | |
161 | |
162 | |
163 #result | |
164 | |
165 | |
166 # In[1258]: | |
167 | |
168 | |
169 #result[['Strain name', 'Antibiotics', 'MIC','s_mic','r_mic','CLSI_profile']].to_csv(output_profile,sep=',', index=False, encoding='utf-8-sig') | |
170 | |
171 | |
172 # In[1259]: | |
173 | |
174 | |
175 #create a pivot table for ASIST | |
176 table=result[['Strain name', 'Antibiotics','CLSI_profile']].drop_duplicates() | |
177 result_table=pd.pivot_table(table, values ='CLSI_profile', index =['Strain name'],columns =['Antibiotics'], aggfunc = lambda x: ' '.join(x)) | |
178 | |
179 | |
180 # In[1261]: | |
181 | |
182 | |
183 result_table | |
184 | |
185 | |
186 # In[1264]: | |
187 | |
188 | |
189 #result_table.to_csv(output_table,na_rep='NA') | |
190 | |
191 | |
192 # In[1282]: | |
193 | |
194 | |
195 # reorder the Antibiotics for ASIST | |
196 clsi_ab=['Amikacin','Tobramycin','Gentamycin','Imipenem','Meropenem','Doripenem','Ciprofloxacin','Levofloxacin', | |
197 'Piperacillin/tazobactam','Ticarcillin/clavulanicacid','Cefotaxime','Ceftriaxone','Ceftazidime','Cefepime', | |
198 'Trimethoprim/sulfamethoxazole','Ampicillin/sulbactam','Colistin','Polymyxinb','Tetracycline','Doxicycline ', | |
199 'Minocycline'] | |
200 result_selected=result_table.filter(clsi_ab) | |
201 | |
202 | |
203 # In[1283]: | |
204 | |
205 | |
206 result_selected.shape | |
207 | |
208 | |
209 # In[1284]: | |
210 | |
211 | |
212 result_table.shape | |
213 | |
214 | |
215 # In[1285]: | |
216 | |
217 | |
218 result_selected.insert(0,'Resistance_phenotype','') | |
219 | |
220 | |
221 # In[1286]: | |
222 | |
223 | |
224 result_selected.to_csv(output_table,na_rep='NA') | |
225 | |
226 | |
227 # In[1287]: | |
228 | |
229 | |
230 #rename headers | |
231 result_selected.rename(columns = {'Ticarcillin/clavulanicacid':'Ticarcillin/clavulanic acid','Piperacillin/tazobactam':'Piperacillin/ tazobactam','Trimethoprim/sulfamethoxazole': 'Trimethoprim/ sulfamethoxazole','Ampicillin/sulbactam':'Ampicillin/ sulbactam', 'Polymyxinb': 'Polymyxin B'} ) | |
232 | |
233 | |
234 # In[ ]: | |
235 | |
236 | |
237 | |
238 | |
239 | |
240 # In[ ]: | |
241 | |
242 | |
243 | |
244 |