Mercurial > repos > rakesh4osdd > asist
comparison asist_dynamic.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 | 734777d3c253 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:c1a77856070c |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # coding: utf-8 | |
| 3 | |
| 4 # In[1309]: | |
| 5 | |
| 6 | |
| 7 #ASIST program for phenotype based on Antibiotics profile | |
| 8 # create a profile based on selected antibiotics only | |
| 9 # rakesh4osdd@gmail.com, 14-June-2021 | |
| 10 | |
| 11 | |
| 12 # In[1403]: | |
| 13 | |
| 14 | |
| 15 import pandas as pd | |
| 16 import sys | |
| 17 import os | |
| 18 from collections import Counter | |
| 19 | |
| 20 | |
| 21 # In[ ]: | |
| 22 | |
| 23 | |
| 24 input_file=sys.argv[1] | |
| 25 output_file=sys.argv[2] | |
| 26 | |
| 27 | |
| 28 # In[1362]: | |
| 29 | |
| 30 | |
| 31 # strain_profile to phenotype condition | |
| 32 def s_phen(sus,res,na,pb_sus): | |
| 33 if (sus>0 and res==0 and na>=0): | |
| 34 #print('Possible Susceptible') | |
| 35 phen='Possible Susceptible' | |
| 36 elif (sus>=0 and 3<=res<7 and na>=0 and pb_sus==0): | |
| 37 #print('Possible MDR') | |
| 38 phen='Possible MDR' | |
| 39 elif (sus>=0 and 7<=res<9 and na>=0 and pb_sus==0): | |
| 40 #print('Possible XDR') | |
| 41 phen='Possible XDR' | |
| 42 #special cases | |
| 43 elif (sus>=1 and res>0 and na>=0 and pb_sus==1): | |
| 44 #print('Possible XDR') | |
| 45 phen='Possible XDR' | |
| 46 #special cases | |
| 47 elif (sus>0 and res==9 and na>=0): | |
| 48 #print('Possible XDR') | |
| 49 phen='Possible XDR' | |
| 50 elif (sus==0 and res==9 and na>=0): | |
| 51 #print('Possible TDR') | |
| 52 phen='Possible TDR' | |
| 53 else: | |
| 54 #print('Strain could not be classified') | |
| 55 phen='Strain could not be classified' | |
| 56 return(phen) | |
| 57 | |
| 58 #print(s_phen(1,9,0,0)) | |
| 59 | |
| 60 | |
| 61 # In[1363]: | |
| 62 | |
| 63 | |
| 64 # define Antibiotic groups as per antibiotic of CLSI breakpoints MIC | |
| 65 #Aminoglycoside | |
| 66 cat1=['Amikacin','Tobramycin','Gentamycin','Netilmicin'] | |
| 67 #Beta-lactams- Carbapenems | |
| 68 cat2=['Imipenem','Meropenam','Doripenem'] | |
| 69 #Fluoroquinolone | |
| 70 cat3=['Ciprofloxacin','Levofloxacin'] | |
| 71 #Beta-lactam inhibitor | |
| 72 cat4=['Piperacillin/tazobactam','Ticarcillin/clavulanicacid'] | |
| 73 #Cephalosporin | |
| 74 cat5=['Cefotaxime','Ceftriaxone','Ceftazidime','Cefepime'] | |
| 75 #Sulfonamides | |
| 76 cat6=['Trimethoprim/sulfamethoxazole'] | |
| 77 #Penicillins/beta-lactamase | |
| 78 cat7=['Ampicillin/sulbactam'] | |
| 79 #Polymyxins | |
| 80 cat8=['Colistin','Polymyxinb'] | |
| 81 #Tetracycline | |
| 82 cat9=['Tetracycline','Doxicycline','Minocycline'] | |
| 83 | |
| 84 def s_profiler(pd_series): | |
| 85 #print(type(pd_series),'\n', pd_series) | |
| 86 #create a dictionary of dataframe series | |
| 87 cats={'s1':cat1,'s2':cat2,'s3':cat3,'s4':cat4,'s5':cat5,'s6':cat6,'s7':cat7,'s8':cat8,'s9':cat9} | |
| 88 # find the antibiotics name in input series | |
| 89 for cat in cats: | |
| 90 #print(cats[cat]) | |
| 91 cats[cat]=pd_series.filter(cats[cat]) | |
| 92 #print(cats[cat]) | |
| 93 #define res,sus,na,pb_sus | |
| 94 res=0 | |
| 95 sus=0 | |
| 96 na=0 | |
| 97 pb_sus=0 | |
| 98 # special case of 'Polymyxin b' for its value | |
| 99 if 'Polymyxinb' in pd_series: | |
| 100 ctp=cats['s8']['Polymyxinb'].strip().lower() | |
| 101 if ctp == 'susceptible': | |
| 102 pb_sus=1 | |
| 103 #print((ctp,p_sus)) | |
| 104 # check all categories | |
| 105 for cat in cats: | |
| 106 #ctp=cats['s8'].iloc[i:i+1].stack().value_counts().to_dict() | |
| 107 #print(ctp) | |
| 108 # Pandas series | |
| 109 ct=cats[cat].value_counts().to_dict() | |
| 110 #print(ct) | |
| 111 # remove whitespace and convert to lowercase words | |
| 112 ct = {k.strip().lower(): v for k, v in ct.items()} | |
| 113 #print(ct) | |
| 114 k=Counter(ct) | |
| 115 #j=Counter(ct)+Counter(j) | |
| 116 #print(j) | |
| 117 # category wise marking | |
| 118 if k['resistant']>=1: | |
| 119 res=res+1 | |
| 120 if k['susceptible']>=1: | |
| 121 sus=sus+1 | |
| 122 if k['na']>=1: | |
| 123 na=na+1 | |
| 124 #print(s_phen(sus,res,na,pb_sus)) | |
| 125 return(s_phen(sus,res,na,pb_sus)) | |
| 126 | |
| 127 | |
| 128 # In[1397]: | |
| 129 | |
| 130 | |
| 131 #input_file='input2.csv_table.csv' | |
| 132 #output_file=input_file+'_output.txt' | |
| 133 strain_profile=pd.read_csv(input_file, sep=',',na_filter=False,skipinitialspace = True) | |
| 134 | |
| 135 | |
| 136 # In[1387]: | |
| 137 | |
| 138 | |
| 139 old_strain_name=strain_profile.columns[0] | |
| 140 new_strain_name=old_strain_name.capitalize().strip().replace(' ', '') | |
| 141 | |
| 142 | |
| 143 # In[1388]: | |
| 144 | |
| 145 | |
| 146 # 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) | |
| 148 #print(strain_profile.columns) | |
| 149 #strain_profile.head() | |
| 150 #strain_profile.columns | |
| 151 | |
| 152 | |
| 153 # In[1389]: | |
| 154 | |
| 155 | |
| 156 # add new column in dataframe on second position | |
| 157 strain_profile.insert(1, 'Strain phenotype','') | |
| 158 #strain_profile.head() | |
| 159 | |
| 160 | |
| 161 # In[1390]: | |
| 162 | |
| 163 | |
| 164 strain_profile['Strain phenotype'] = strain_profile.apply(lambda x: (s_profiler(x)), axis=1) | |
| 165 | |
| 166 | |
| 167 # In[1391]: | |
| 168 | |
| 169 | |
| 170 #strain_profile.head() | |
| 171 | |
| 172 | |
| 173 # In[1392]: | |
| 174 | |
| 175 | |
| 176 #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'} ) | |
| 178 | |
| 179 | |
| 180 # In[1404]: | |
| 181 | |
| 182 | |
| 183 #strain_profile | |
| 184 | |
| 185 | |
| 186 # In[1394]: | |
| 187 | |
| 188 | |
| 189 strain_profile.to_csv(output_file,na_rep='NA',index=False) | |
| 190 |
