comparison asist_dynamic.py @ 3:734777d3c253 draft

Deleted selected files test
author rakesh4osdd
date Wed, 30 Jun 2021 05:35:56 +0000
parents c1a77856070c
children
comparison
equal deleted inserted replaced
2:62afd73a0987 3:734777d3c253
7 #ASIST program for phenotype based on Antibiotics profile 7 #ASIST program for phenotype based on Antibiotics profile
8 # create a profile based on selected antibiotics only 8 # create a profile based on selected antibiotics only
9 # rakesh4osdd@gmail.com, 14-June-2021 9 # rakesh4osdd@gmail.com, 14-June-2021
10 10
11 11
12 # In[1403]: 12 # In[1]:
13 13
14 14
15 import pandas as pd 15 import pandas as pd
16 import sys 16 import sys
17 import os 17 import os
18 from collections import Counter 18 from collections import Counter
19 19
20 20
21 # In[ ]: 21 # In[176]:
22 22
23 23
24 input_file=sys.argv[1] 24 input_file=sys.argv[1]
25 output_file=sys.argv[2] 25 output_file=sys.argv[2]
26 26 #input_file='test-data/asist_input.csv'
27 27 #output_file='test-data/asist_output.csv'
28 # In[1362]: 28
29
30 # In[177]:
29 31
30 32
31 # strain_profile to phenotype condition 33 # strain_profile to phenotype condition
32 def s_phen(sus,res,na,pb_sus): 34 def s_phen(sus,res,intm,na,pb_sus):
33 if (sus>0 and res==0 and na>=0): 35 if (sus>0 and res==0 and na>=0):
34 #print('Possible Susceptible') 36 #print('Possible Susceptible')
35 phen='Possible Susceptible' 37 phen='Possible Susceptible'
36 elif (sus>=0 and 3<=res<7 and na>=0 and pb_sus==0): 38 elif (sus>=0 and 3<=res<7 and na>=0 and pb_sus==0):
37 #print('Possible MDR') 39 #print('Possible MDR')
50 elif (sus==0 and res==9 and na>=0): 52 elif (sus==0 and res==9 and na>=0):
51 #print('Possible TDR') 53 #print('Possible TDR')
52 phen='Possible TDR' 54 phen='Possible TDR'
53 else: 55 else:
54 #print('Strain could not be classified') 56 #print('Strain could not be classified')
55 phen='Strain could not be classified' 57 phen='Strain could not be classified ('+ str(intm)+' | ' + str(na) +')'
56 return(phen) 58 return(phen)
57 59
58 #print(s_phen(1,9,0,0)) 60 #print(s_phen(1,9,0,0))
59 61
60 62
61 # In[1363]: 63 # In[178]:
62 64
63 65
64 # define Antibiotic groups as per antibiotic of CLSI breakpoints MIC 66 # define Antibiotic groups as per antibiotic of CLSI breakpoints MIC
65 #Aminoglycoside 67 #Aminoglycoside
66 cat1=['Amikacin','Tobramycin','Gentamycin','Netilmicin'] 68 cat1=['Amikacin','Tobramycin','Gentamycin','Netilmicin']
88 # find the antibiotics name in input series 90 # find the antibiotics name in input series
89 for cat in cats: 91 for cat in cats:
90 #print(cats[cat]) 92 #print(cats[cat])
91 cats[cat]=pd_series.filter(cats[cat]) 93 cats[cat]=pd_series.filter(cats[cat])
92 #print(cats[cat]) 94 #print(cats[cat])
93 #define res,sus,na,pb_sus 95 #define res,sus,intm,na,pb_sus
94 res=0 96 res=0
95 sus=0 97 sus=0
98 intm=0
96 na=0 99 na=0
97 pb_sus=0 100 pb_sus=0
98 # special case of 'Polymyxin b' for its value 101 # special case of 'Polymyxin b' for its value
99 if 'Polymyxinb' in pd_series: 102 if 'Polymyxinb' in pd_series:
100 ctp=cats['s8']['Polymyxinb'].strip().lower() 103 ctp=cats['s8']['Polymyxinb'].strip().lower()
117 # category wise marking 120 # category wise marking
118 if k['resistant']>=1: 121 if k['resistant']>=1:
119 res=res+1 122 res=res+1
120 if k['susceptible']>=1: 123 if k['susceptible']>=1:
121 sus=sus+1 124 sus=sus+1
125 if k['intermediate']>=1:
126 intm=intm+1
122 if k['na']>=1: 127 if k['na']>=1:
123 na=na+1 128 na=na+1
124 #print(s_phen(sus,res,na,pb_sus)) 129 #print(sus,res,intm,na,pb_sus)
125 return(s_phen(sus,res,na,pb_sus)) 130 #print(s_phen(sus,res,intm,na,pb_sus))
126 131 return(s_phen(sus,res,intm,na,pb_sus))
127 132
128 # In[1397]: 133
134 # In[179]:
129 135
130 136
131 #input_file='input2.csv_table.csv' 137 #input_file='input2.csv_table.csv'
132 #output_file=input_file+'_output.txt' 138 #output_file=input_file+'_output.txt'
133 strain_profile=pd.read_csv(input_file, sep=',',na_filter=False,skipinitialspace = True) 139 strain_profile=pd.read_csv(input_file, sep=',',na_filter=False,skipinitialspace = True)
134 140
135 141
136 # In[1387]: 142 # In[180]:
137 143
138 144
139 old_strain_name=strain_profile.columns[0] 145 old_strain_name=strain_profile.columns[0]
140 new_strain_name=old_strain_name.capitalize().strip().replace(' ', '') 146 new_strain_name=old_strain_name.capitalize().strip().replace(' ', '')
141 147
142 148
143 # In[1388]: 149 # In[181]:
144 150
145 151
146 # make header capitalization, remove leading,lagging, and multiple whitespace for comparision 152 # make header capitalization, remove leading,lagging, and multiple whitespace for comparision
147 strain_profile.columns=strain_profile.columns.str.capitalize().str.strip().str.replace('\s+', '', regex=True) 153 strain_profile.columns=strain_profile.columns.str.capitalize().str.strip().str.replace('\s+', '', regex=True)
148 #print(strain_profile.columns) 154 #print(strain_profile.columns)
149 #strain_profile.head() 155 #strain_profile.head()
150 #strain_profile.columns 156 #strain_profile.columns
151 157
152 158
153 # In[1389]: 159 # In[182]:
154 160
155 161
156 # add new column in dataframe on second position 162 # add new column in dataframe on second position
157 strain_profile.insert(1, 'Strain phenotype','') 163 strain_profile.insert(1, 'Strain phenotype','')
158 #strain_profile.head() 164 #strain_profile.head()
159 165
160 166
161 # In[1390]: 167 # In[183]:
162 168
163 169
164 strain_profile['Strain phenotype'] = strain_profile.apply(lambda x: (s_profiler(x)), axis=1) 170 strain_profile['Strain phenotype'] = strain_profile.apply(lambda x: (s_profiler(x)), axis=1)
165 171
166 172
167 # In[1391]: 173 # In[184]:
168 174
169 175
170 #strain_profile.head() 176 #strain_profile.head()
171 177
172 178
173 # In[1392]: 179 # In[185]:
174 180
175 181
176 #rename headers for old name 182 #rename headers for old name
177 strain_profile=strain_profile.rename(columns = {new_strain_name:old_strain_name, 'Ticarcillin/clavulanicacid':'Ticarcillin/ clavulanic acid','Piperacillin/tazobactam':'Piperacillin/ tazobactam','Trimethoprim/sulfamethoxazole': 'Trimethoprim/ sulfamethoxazole','Ampicillin/sulbactam':'Ampicillin/ sulbactam', 'Polymyxinb': 'Polymyxin B'} ) 183 strain_profile=strain_profile.rename(columns = {new_strain_name:old_strain_name, 'Ticarcillin/clavulanicacid':'Ticarcillin/ clavulanic acid','Piperacillin/tazobactam':'Piperacillin/ tazobactam','Trimethoprim/sulfamethoxazole': 'Trimethoprim/ sulfamethoxazole','Ampicillin/sulbactam':'Ampicillin/ sulbactam', 'Polymyxinb': 'Polymyxin B'} )
178 184
179 185
180 # In[1404]: 186 # In[186]:
187
188
189 #strain_profile.columns
190
191
192 # In[187]:
181 193
182 194
183 #strain_profile 195 #strain_profile
184 196
185 197
186 # In[1394]: 198 # In[188]:
187 199
188 200
189 strain_profile.to_csv(output_file,na_rep='NA',index=False) 201 strain_profile.to_csv(output_file,na_rep='NA',index=False)
190 202
203
204 # In[189]:
205
206
207 # Open a file with access mode 'a'
208 with open(output_file, "a") as file_object:
209 # Append 'hello' at the end of file
210 file_object.write("Note: \n1. 'MDR': Multidrug-resistant, 'XDR': Extensively drug-resistant, 'TDR':totally drug resistant, NA': Data Not Available.\n2. 'Strain could not be classified' numbers follow the format as ('Number of antibiotics categories count as Intermediate' | 'Number of antibiotics categories count as NA')")
211
212
213 # In[ ]:
214
215
216
217