comparison asist_dynamic.ipynb @ 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 3ea72fb2eaac
comparison
equal deleted inserted replaced
-1:000000000000 0:c1a77856070c
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1309,
6 "id": "27cfc66f",
7 "metadata": {},
8 "outputs": [],
9 "source": [
10 "#ASIST program for phenotype based on Antibiotics profile\n",
11 "# create a profile based on selected antibiotics only\n",
12 "# rakesh4osdd@gmail.com, 14-June-2021"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 1,
18 "id": "75a352b7",
19 "metadata": {},
20 "outputs": [],
21 "source": [
22 "import pandas as pd\n",
23 "import sys\n",
24 "import os\n",
25 "from collections import Counter"
26 ]
27 },
28 {
29 "cell_type": "code",
30 "execution_count": 176,
31 "id": "d66ec0d2",
32 "metadata": {},
33 "outputs": [],
34 "source": [
35 "input_file=sys.argv[1]\n",
36 "output_file=sys.argv[2]\n",
37 "#input_file='test-data/asist_input.csv'\n",
38 "#output_file='test-data/asist_output.csv'"
39 ]
40 },
41 {
42 "cell_type": "code",
43 "execution_count": 177,
44 "id": "bf24c946",
45 "metadata": {},
46 "outputs": [],
47 "source": [
48 "# strain_profile to phenotype condition\n",
49 "def s_phen(sus,res,intm,na,pb_sus):\n",
50 " if (sus>0 and res==0 and na>=0):\n",
51 " #print('Possible Susceptible')\n",
52 " phen='Possible Susceptible'\n",
53 " elif (sus>=0 and 3<=res<7 and na>=0 and pb_sus==0):\n",
54 " #print('Possible MDR')\n",
55 " phen='Possible MDR'\n",
56 " elif (sus>=0 and 7<=res<9 and na>=0 and pb_sus==0):\n",
57 " #print('Possible XDR')\n",
58 " phen='Possible XDR'\n",
59 " #special cases\n",
60 " elif (sus>=1 and res>0 and na>=0 and pb_sus==1):\n",
61 " #print('Possible XDR')\n",
62 " phen='Possible XDR'\n",
63 " #special cases\n",
64 " elif (sus>0 and res==9 and na>=0):\n",
65 " #print('Possible XDR')\n",
66 " phen='Possible XDR'\n",
67 " elif (sus==0 and res==9 and na>=0):\n",
68 " #print('Possible TDR')\n",
69 " phen='Possible TDR'\n",
70 " else:\n",
71 " #print('Strain could not be classified')\n",
72 " phen='Strain could not be classified ('+ str(intm)+' | ' + str(na) +')'\n",
73 " return(phen)\n",
74 "\n",
75 "#print(s_phen(1,9,0,0))"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 178,
81 "id": "8bad7d9d",
82 "metadata": {},
83 "outputs": [],
84 "source": [
85 "# define Antibiotic groups as per antibiotic of CLSI breakpoints MIC\n",
86 "#Aminoglycoside\n",
87 "cat1=['Amikacin','Tobramycin','Gentamycin','Netilmicin']\n",
88 "#Beta-lactams- Carbapenems\n",
89 "cat2=['Imipenem','Meropenam','Doripenem']\n",
90 "#Fluoroquinolone\n",
91 "cat3=['Ciprofloxacin','Levofloxacin']\n",
92 "#Beta-lactam inhibitor\n",
93 "cat4=['Piperacillin/tazobactam','Ticarcillin/clavulanicacid']\n",
94 "#Cephalosporin\n",
95 "cat5=['Cefotaxime','Ceftriaxone','Ceftazidime','Cefepime']\n",
96 "#Sulfonamides\n",
97 "cat6=['Trimethoprim/sulfamethoxazole']\n",
98 "#Penicillins/beta-lactamase\n",
99 "cat7=['Ampicillin/sulbactam']\n",
100 "#Polymyxins\n",
101 "cat8=['Colistin','Polymyxinb']\n",
102 "#Tetracycline\n",
103 "cat9=['Tetracycline','Doxicycline','Minocycline']\n",
104 "\n",
105 "def s_profiler(pd_series):\n",
106 " #print(type(pd_series),'\\n', pd_series)\n",
107 " #create a dictionary of dataframe series\n",
108 " cats={'s1':cat1,'s2':cat2,'s3':cat3,'s4':cat4,'s5':cat5,'s6':cat6,'s7':cat7,'s8':cat8,'s9':cat9}\n",
109 " # find the antibiotics name in input series\n",
110 " for cat in cats:\n",
111 " #print(cats[cat])\n",
112 " cats[cat]=pd_series.filter(cats[cat])\n",
113 " #print(cats[cat])\n",
114 " #define res,sus,intm,na,pb_sus\n",
115 " res=0\n",
116 " sus=0\n",
117 " intm=0\n",
118 " na=0\n",
119 " pb_sus=0\n",
120 " # special case of 'Polymyxin b' for its value\n",
121 " if 'Polymyxinb' in pd_series:\n",
122 " ctp=cats['s8']['Polymyxinb'].strip().lower()\n",
123 " if ctp == 'susceptible':\n",
124 " pb_sus=1\n",
125 " #print((ctp,p_sus))\n",
126 " # check all categories\n",
127 " for cat in cats:\n",
128 " #ctp=cats['s8'].iloc[i:i+1].stack().value_counts().to_dict()\n",
129 " #print(ctp)\n",
130 " # Pandas series\n",
131 " ct=cats[cat].value_counts().to_dict()\n",
132 " #print(ct)\n",
133 " # remove whitespace and convert to lowercase words\n",
134 " ct = {k.strip().lower(): v for k, v in ct.items()}\n",
135 " #print(ct)\n",
136 " k=Counter(ct)\n",
137 " #j=Counter(ct)+Counter(j)\n",
138 " #print(j)\n",
139 " # category wise marking\n",
140 " if k['resistant']>=1:\n",
141 " res=res+1\n",
142 " if k['susceptible']>=1:\n",
143 " sus=sus+1\n",
144 " if k['intermediate']>=1:\n",
145 " intm=intm+1\n",
146 " if k['na']>=1:\n",
147 " na=na+1\n",
148 " #print(sus,res,intm,na,pb_sus)\n",
149 " #print(s_phen(sus,res,intm,na,pb_sus))\n",
150 " return(s_phen(sus,res,intm,na,pb_sus))"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": 179,
156 "id": "7629fc10",
157 "metadata": {},
158 "outputs": [],
159 "source": [
160 "#input_file='input2.csv_table.csv'\n",
161 "#output_file=input_file+'_output.txt'\n",
162 "strain_profile=pd.read_csv(input_file, sep=',',na_filter=False,skipinitialspace = True)"
163 ]
164 },
165 {
166 "cell_type": "code",
167 "execution_count": 180,
168 "id": "bed1abba",
169 "metadata": {},
170 "outputs": [],
171 "source": [
172 "old_strain_name=strain_profile.columns[0]\n",
173 "new_strain_name=old_strain_name.capitalize().strip().replace(' ', '')"
174 ]
175 },
176 {
177 "cell_type": "code",
178 "execution_count": 181,
179 "id": "a64b5022",
180 "metadata": {},
181 "outputs": [],
182 "source": [
183 "# make header capitalization, remove leading,lagging, and multiple whitespace for comparision\n",
184 "strain_profile.columns=strain_profile.columns.str.capitalize().str.strip().str.replace('\\s+', '', regex=True)\n",
185 "#print(strain_profile.columns)\n",
186 "#strain_profile.head()\n",
187 "#strain_profile.columns"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": 182,
193 "id": "caac57d7",
194 "metadata": {},
195 "outputs": [],
196 "source": [
197 "# add new column in dataframe on second position\n",
198 "strain_profile.insert(1, 'Strain phenotype','')\n",
199 "#strain_profile.head()"
200 ]
201 },
202 {
203 "cell_type": "code",
204 "execution_count": 183,
205 "id": "eb4b0c4d",
206 "metadata": {
207 "scrolled": true
208 },
209 "outputs": [],
210 "source": [
211 "strain_profile['Strain phenotype'] = strain_profile.apply(lambda x: (s_profiler(x)), axis=1)"
212 ]
213 },
214 {
215 "cell_type": "code",
216 "execution_count": 184,
217 "id": "86441c0f",
218 "metadata": {},
219 "outputs": [],
220 "source": [
221 "#strain_profile.head()"
222 ]
223 },
224 {
225 "cell_type": "code",
226 "execution_count": 185,
227 "id": "75698be5",
228 "metadata": {},
229 "outputs": [],
230 "source": [
231 "#rename headers for old name\n",
232 "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'} )"
233 ]
234 },
235 {
236 "cell_type": "code",
237 "execution_count": 186,
238 "id": "c14a13eb",
239 "metadata": {
240 "scrolled": true
241 },
242 "outputs": [],
243 "source": [
244 "#strain_profile.columns"
245 ]
246 },
247 {
248 "cell_type": "code",
249 "execution_count": 187,
250 "id": "ff484767",
251 "metadata": {},
252 "outputs": [],
253 "source": [
254 "#strain_profile"
255 ]
256 },
257 {
258 "cell_type": "code",
259 "execution_count": 188,
260 "id": "5ab72211",
261 "metadata": {},
262 "outputs": [],
263 "source": [
264 "strain_profile.to_csv(output_file,na_rep='NA',index=False)"
265 ]
266 },
267 {
268 "cell_type": "code",
269 "execution_count": 189,
270 "id": "020dbe85",
271 "metadata": {},
272 "outputs": [],
273 "source": [
274 "# Open a file with access mode 'a'\n",
275 "with open(output_file, \"a\") as file_object:\n",
276 " # Append 'hello' at the end of file\n",
277 " 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')\")"
278 ]
279 },
280 {
281 "cell_type": "code",
282 "execution_count": null,
283 "id": "9c17e66a",
284 "metadata": {},
285 "outputs": [],
286 "source": []
287 }
288 ],
289 "metadata": {
290 "kernelspec": {
291 "display_name": "Python 3",
292 "language": "python",
293 "name": "python3"
294 },
295 "language_info": {
296 "codemirror_mode": {
297 "name": "ipython",
298 "version": 3
299 },
300 "file_extension": ".py",
301 "mimetype": "text/x-python",
302 "name": "python",
303 "nbconvert_exporter": "python",
304 "pygments_lexer": "ipython3",
305 "version": "3.7.10"
306 }
307 },
308 "nbformat": 4,
309 "nbformat_minor": 5
310 }