view clsi_profile_type2.py @ 2:62afd73a0987 draft

"planemo upload for repository https://github.com/rakesh4osdd/asist/tree/master commit 58779a9edf592cff8cc63877d29e83888e403748-dirty"
author rakesh4osdd
date Mon, 28 Jun 2021 15:05:17 +0000
parents c1a77856070c
children
line wrap: on
line source

#!/usr/bin/env python
# coding: utf-8

# In[1245]:


# ASIST module2 | map AST result to the CLSI breakporints with combination antibiotics
# By rakesh4osdd@gmail.com, 06-Jun-2021
import pandas as pd
import re
import sys


# In[1246]:


# cross check MIC values
def sus_res(mic):
    #print(mic)
    #print ((mic,pd.isna(mic[1]),pd.isna(mic[0]),'\n')
    if not (pd.isna(mic[0]) or pd.isna(mic[1]) or pd.isna(mic[2])):#check for NaN value
        #remove unwanted whitespace
        o_mic = mic[0].replace(' ', '')
        s_mic = mic[1].replace(' ', '')
        r_mic = mic[2].replace(' ', '')
        #print (o_mic,s_mic,r_mic)
        #print (type(o_mic),type(s_mic),type(r_mic))
        if '/' in s_mic: #check for combination antibiotics
            #print ('combination antibiotics')
            try:
                if '/' in o_mic:
                    #print ('input combination')
                    if (float(o_mic.split('/')[0]) <= float(s_mic.split('/')[0]) and float(o_mic.split('/')[1]) <= float(s_mic.split('/')[1])):
                        strain_type='Susceptible'
                    elif (float(o_mic.split('/')[0]) >= float(r_mic.split('/')[0]) and float(o_mic.split('/')[1]) >= float(r_mic.split('/')[1])):
                        strain_type='Resistant'
                    else:
                        strain_type='Intermediate'
                else: 
                    #print ('single')
                    if float(o_mic)==0:
                        strain_type='Strain could not classified'
                    elif (float(o_mic.split('/')[0]) <= float(s_mic.split('/')[0]) and float(1) <= float(s_mic.split('/')[1])):
                        strain_type='Susceptible'
                    elif (float(o_mic.split('/')[0]) >= float(r_mic.split('/')[0]) and float(1) >= float(r_mic.split('/')[1])):
                        strain_type='Resistant'
                    else:
                        strain_type='Intermediate'
            except ValueError:
                strain_type='Strain could not classified'
        else: #single antibiotics
            #print('single antibiotics')
            if o_mic:
                if float(o_mic)==0:
                    strain_type='Strain could not classified'
                elif (float(o_mic) <= float(s_mic)):
                    strain_type='Susceptible'
                elif (float(o_mic) >= float(r_mic)):
                    strain_type='Resistant'
                else:
                    strain_type='Intermediate'
            else:
                strain_type='Strain could not classified'
    else:
        strain_type='Strain could not classified'
    return(strain_type)


# In[ ]:

# for input argument
input_user = sys.argv[1]
input_clsi = sys.argv[2]
output_table = sys.argv[3]

# In[1247]:


"""
input_user='input_ast_comb2.csv'
input_clsi='clsi_profile_comb.csv'
output_profile=input_user+'_profile.csv'
output_table=input_user+'_table.csv'

"""

# In[1248]:


# read user AST data with selected 3 columns
strain_mic=pd.read_csv(input_user, sep=',', usecols =['Strain name', 'Antibiotics', 'MIC'],na_filter=False)


# In[1249]:


clsi_bp=pd.read_csv(input_clsi,sep=',')


# In[1290]:


#clsi_bp.head(2)
#strain_mic


# In[1251]:


# convert MIC to numbers sMIC, rMIC
clsi_bp['s_mic'] =clsi_bp[['Susceptible']].applymap(lambda x: (re.sub(r'[^0-9.\/]', '', x)))
clsi_bp['r_mic'] =clsi_bp[['Resistant']].applymap(lambda x: (re.sub(r'[^0-9.\/]', '', x)))


# In[1252]:


# Read only numbers in MIC values
#try:
strain_mic['o_mic']=strain_mic[['MIC']].applymap(lambda x: (re.sub(r'[^0-9.\/]','', x)))
#except TypeError:
#    print('Waring: Error in MIC value')


# In[1289]:


#strain_mic.head()


# In[1254]:


# capitalize each Antibiotic Name for comparision with removing whitespace
strain_mic['Antibiotics']=strain_mic['Antibiotics'].str.capitalize().str.replace(" ","")
clsi_bp['Antibiotics']=clsi_bp['Antibiotics'].str.capitalize().str.replace(" ","")


# In[1255]:


#compare CLSI Antibiotics only
#result=pd.merge(strain_mic, clsi_bp, on='Antibiotics',how='inner',  indicator=True)[['Strain name','Antibiotics', 'MIC', 'o_mic', 's_mic', 'r_mic','_merge']]
try:
    result=pd.merge(strain_mic, clsi_bp, on='Antibiotics',how='inner')[['Strain name','Antibiotics', 'MIC', 'o_mic', 's_mic', 'r_mic']]
except KeyError:
    print('Waring: Error in input Values')


# In[1256]:


#compare MIC values and assign Susceptible and Resistant to Strain
#try:
result[['CLSI_profile']] = result[['o_mic','s_mic','r_mic']].apply(sus_res,axis = 1)
#except ValueError:
#    print('Waring: Error in input MIC value')


# In[1288]:


#result


# In[1258]:


#result[['Strain name', 'Antibiotics', 'MIC','s_mic','r_mic','CLSI_profile']].to_csv(output_profile,sep=',', index=False, encoding='utf-8-sig')


# In[1259]:


#create a pivot table for ASIST
table=result[['Strain name', 'Antibiotics','CLSI_profile']].drop_duplicates()
result_table=pd.pivot_table(table, values ='CLSI_profile', index =['Strain name'],columns =['Antibiotics'], aggfunc = lambda x: ' '.join(x))


# In[1261]:


result_table


# In[1264]:


#result_table.to_csv(output_table,na_rep='NA')


# In[1282]:


# reorder the Antibiotics for ASIST
clsi_ab=['Amikacin','Tobramycin','Gentamycin','Imipenem','Meropenem','Doripenem','Ciprofloxacin','Levofloxacin',
         'Piperacillin/tazobactam','Ticarcillin/clavulanicacid','Cefotaxime','Ceftriaxone','Ceftazidime','Cefepime',
         'Trimethoprim/sulfamethoxazole','Ampicillin/sulbactam','Colistin','Polymyxinb','Tetracycline','Doxicycline ',
         'Minocycline']
result_selected=result_table.filter(clsi_ab)


# In[1283]:


result_selected.shape


# In[1284]:


result_table.shape


# In[1285]:


result_selected.insert(0,'Resistance_phenotype','')


# In[1286]:


result_selected.to_csv(output_table,na_rep='NA')


# In[1287]:


#rename headers
result_selected.rename(columns = {'Ticarcillin/clavulanicacid':'Ticarcillin/clavulanic acid','Piperacillin/tazobactam':'Piperacillin/ tazobactam','Trimethoprim/sulfamethoxazole': 'Trimethoprim/ sulfamethoxazole','Ampicillin/sulbactam':'Ampicillin/ sulbactam', 'Polymyxinb': 'Polymyxin B'} )


# In[ ]:





# In[ ]: