-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisolationsource.py
executable file
·286 lines (205 loc) · 8.16 KB
/
isolationsource.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 2 17:40:52 2020
@author: jha
"""
#%%
import csv
import mysql.connector as mariadb
from tabulate import tabulate
import pandas as pd
amrdb= mariadb.connect(
host="localhost",
user="root",
passwd="Sukhoi@90",
database ="myamr"
)
cursor = amrdb.cursor(buffered=True) # else it fetches one row for everytime it is executed
print("We are at line 31 we have connection, lets begin")
#%%
#get the data from the ast table to make the dataframe
#%%
import pandas as pd
numrows= cursor.execute("SELECT isolation_source FROM Escherichia_MergedF")
print("Selected %s rows" %numrows)
print("Selected %s rows " %cursor.rowcount)
rows =cursor.fetchall()#fetch all rows at once
#print(tabulate(rows, headers=['AST_phenotype'], tablefmt='psql'))
#%%
#splitting the collection_date column values into separate columns to get a separate column for date
#%%
#df = pd.DataFrame(rows)
list_rows=[item for t in rows for item in t]
list_epi=[]
for index,item in enumerate(list_rows):
#print(item)
if 'beef'in item:
print ("it was beef")
list_rows[index]= "beef/beefproduct"
elif 'farm' in item :
print("it was farm")
else:
list_rows[index]= "clinical"
print("it was clinical")
#%%
#%%
df = pd.DataFrame(list_rows,columns=['isolate_epi_type'])
df['isolationsource']= pd.DataFrame(rows)
from sqlalchemy import create_engine
cnx = create_engine('mysql+pymysql://root:Sukhoi@90@localhost/myamr')
df.to_sql(name='isolationsource_es', con=cnx, if_exists = 'replace', index=False)
print("we are at line 186 in code")
#%%
#best way to read table from sql
#%%
import pandas as pd
numrows= cursor.execute("Describe Escherichia_isolateepi")
print("Selected %s rows" %numrows)
print("Selected %s rows " %cursor.rowcount)
rows =cursor.fetchall()#fetch all rows at once
print(rows)
dfz= pd.DataFrame(rows)
listz=list(dfz[0])
numrows_k= cursor.execute("Select * from Escherichia_isolateepi")
print("Selected %s rows" %numrows_k)
print("Selected %s rows " %cursor.rowcount)
rows_k =cursor.fetchall()#fetch all rows at once
df_kleb= pd.DataFrame(rows_k)
df_kleb.columns= listz
#%%
#%%
import pandas as pd
numrows= cursor.execute("Describe Escherichia_nov_astt")
print("Selected %s rows" %numrows)
print("Selected %s rows " %cursor.rowcount)
rows =cursor.fetchall()#fetch all rows at once
print(rows)
df_col_name= pd.DataFrame(rows)
list_col_name=list(df_col_name[0])
del list_col_name[0]
del list_col_name[0]
#%%
#df_crosstab=pd.crosstab(df_kleb.isolate_epi_type, df_kleb.cefoxitin, margins=True, margins_name="total samples" , normalize= 'all').round(4)*100
#df_crosstab=df_crosstab.T
#df_crosstab.to_excel('escherichia_isolation_astphenotypes_crosstab.xlsx')
df_crosstab=pd.crosstab(df_kleb.isolate_epi_type, df_kleb.cefoxitin,df_kleb.Collection_year, margins=True, margins_name="total samples")
#%%
#for getting all the antimicrobials
#%%
#def crosstab_all(df_kleb,list_col_name):
appended_data=[]
for k in list_col_name:
try:
xdf=pd.crosstab(df_kleb["isolate_epi_type"],df_kleb[k], margins=True, margins_name="Out of total samples")
xdf["Antimicrobial"] = k
print('xdf',xdf)
print('') # for spacing
appended_data.append(xdf)
except:
print("not found")
continue
appended_data = pd.concat(appended_data, sort= False)
appended_data.loc[appended_data['Antimicrobial'].duplicated(), 'Antimicrobial'] = np.nan
#%%
#for only those antimicrobials which were tested for more than one epitypes
#%%
#def crosstab_all(df_kleb,list_col_name):
appended_data=[]
for k in list_col_name:
try:
xdf=pd.crosstab(df_kleb["isolate_epi_type"],df_kleb[k], margins=True, margins_name="All Samples Combined")
xdf["Antimicrobial"] = k
print('xdf',xdf)
print('') # for spacing
if len(xdf.index) >2:
appended_data.append(xdf)
else:
print("only one epitype tested")
except:
print("not found")
continue
appended_data= pd.concat(appended_data, sort= False)
#%%
#%%
df_family = pd.read_excel('/Users/jha/Documents/spring2020/ecoli_antimicrobial_class.xlsx', sheet_name='Sheet1')
df_family_mod= df_family
#df_family=df_family.drop(["Intrinsicly_resistant","Antimicrobial_class","Antimicrobial_mod"], axis=1)
list_family=list(df_family.itertuples(index=False, name=None))
#list_family=list(df_family)
family_dict=dict(list_family)
appended_data['Antimicrobial_class']=appended_data['Antimicrobial'].map(family_dict)
appended_data = appended_data.reset_index()
appended_data= (appended_data.groupby(['Antimicrobial_class', 'Antimicrobial','isolate_epi_type']).sum())
appended_data = appended_data.reset_index()
appended_data.loc[appended_data['Antimicrobial'].duplicated(), 'Antimicrobial'] = np.nan
appended_data.loc[appended_data['Antimicrobial_class'].duplicated(), 'Antimicrobial_class'] = np.nan
appended_data = appended_data.set_index(['Antimicrobial_class', 'Antimicrobial'])
#cols=["isolate_epi_type","S","R","I"]
#appended_data =appended_data[cols]
#%%
#for only those antimicrobials which were tested for more than one epitypes
#%%
#def crosstab_all(df_kleb,list_col_name):
appended_data_P=[]
for k in list_col_name:
try:
xdf=pd.crosstab(df_kleb["isolate_epi_type"],df_kleb[k], margins=True, margins_name="All Samples Combined", normalize="index").round(4)*100
xdf["Antimicrobial"] = k
print('xdf',xdf)
print('') # for spacing
if len(xdf.index) >2:
appended_data_P.append(xdf)
else:
print("only one epitype tested")
except:
print("not found")
continue
appended_data_P= pd.concat(appended_data_P, sort= False)
#%%
#to merge the appended tables to get frequency and percentage in the same table
#%%
appended_data_P=appended_data_P.rename(columns = {"S": "%Susceptible",
"R":"%Resistant",
"I": "%Intermediate"})
appended_data=appended_data.reset_index()
appended_data=appended_data.reset_index()
appended_data_P=appended_data_P.reset_index()
appended_data_P=appended_data_P.reset_index()
appended_dict = dict(zip(appended_data.index, appended_data.S))
appended_data_P["Susceptible"]=appended_data_P['index'].map(appended_dict)
appended_dict = dict(zip(appended_data.index, appended_data.R))
appended_data_P["Resistant"]=appended_data_P['index'].map(appended_dict)
appended_dict = dict(zip(appended_data.index, appended_data.I))
appended_data_P["Intermediate"]=appended_data_P['index'].map(appended_dict)
appended_data_P=appended_data_P.drop("index", axis=1)
appended_data=appended_data.drop("index", axis=1)
#appended_data = appended_data.set_index(['Collection_year'])
#%%
#%%
df_family = pd.read_excel('/Users/jha/Documents/spring2020/ecoli_antimicrobial_class.xlsx', sheet_name='Sheet1')
df_family_mod= df_family
#df_family=df_family.drop(["Intrinsicly_resistant","Antimicrobial_class","Antimicrobial_mod"], axis=1)
list_family=list(df_family.itertuples(index=False, name=None))
#list_family=list(df_family)
family_dict=dict(list_family)
appended_data_P['Antimicrobial_class']=appended_data_P['Antimicrobial'].map(family_dict)
appended_data_P= (appended_data_P.groupby(['Antimicrobial_class', 'Antimicrobial','isolate_epi_type']).sum())
appended_data_P = appended_data_P.reset_index()
appended_data_P.loc[appended_data_P['Antimicrobial'].duplicated(), 'Antimicrobial'] = np.nan
appended_data_P.loc[appended_data_P['Antimicrobial_class'].duplicated(), 'Antimicrobial_class'] = np.nan
appended_data_P = appended_data_P.set_index(['Antimicrobial_class', 'Antimicrobial'])
cols=["isolate_epi_type","%Susceptible","Susceptible","%Resistant","Resistant","%Intermediate","Intermediate"]
appended_data_P =appended_data_P[cols]
#%%
#%%
import pandas as pd
from openpyxl import load_workbook
path= "/Users/jha/escherichia_isolation_antimicrobial_crosstab.xlsx"
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine = 'openpyxl')
writer.book = book
appended_data_P.to_excel(writer, sheet_name="iso-byindex_freq_Per")
writer.save()
writer.close()
#%%