added aa_prop.py and add_aa_prop.py to add aa properties for wt and mutant in a given file containing one letter code wt and mut cols as csv

This commit is contained in:
Tanushree Tunstall 2021-06-14 13:24:00 +01:00
parent 58b5b63595
commit 0881181f4b
2 changed files with 304 additions and 0 deletions

113
scripts/aa_prop.py Normal file
View file

@ -0,0 +1,113 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Created on Mon June 14 2021
@author: tanu
'''
# FIXME: import dirs.py to get the basic dir paths available
#=======================================================================
# TASK
# Input:
# Output:
#=======================================================================
#%% load libraries
import os, sys
import pandas as pd
#import numpy as np
#from varname import nameof
import argparse
DEBUG = False
#=======================================================================
#%% specify input and curr dir
homedir = os.path.expanduser('~')
# set working dir
os.getcwd()
os.chdir(homedir + '/git/LSHTM_analysis/scripts')
os.getcwd()
from reference_dict import oneletter_aa_dict
#=======================================================================
#%%
def get_aa_prop(df, col1 = 'aap1', col2 = 'aap2', col3 = 'aap_taylor', col4 = 'aap_kd', col5 = 'aap_polarity', col6 = 'aap_calcprop'):
"""Add amino acid properties for wt and mutant residues
@df: df containing one letter aa code for wt and mutant respectively
@type: pandas df
@col1: column adding 7 aa categories (no overlap; acidic, basic, amidic, hydrophobic, hydroxylic, aromatic, sulphur)
@type: str
@col2: column adding 9 aa categories (overlap; acidic, basic, polar, hydrophobic, hydrophilic, small, aromatic, aliphatic, special)
@type: str
@col3: column adding 8 aa categories (overlap; acidic, basic, polar, hydrophobic, small, aromatic, aliphatic, special)
@type: str
@col4: column adding 3 aa categories (no overlap, hydrophobic, neutral and hydrophilic according to KD scale)
@type: str
@col5: column adding 4 aa categories (no overlap, acidic, basic, neutral, non-polar)
@type: str
@col6: column adding 4 aa categories (neg, pos, polar, non-polar)
@type: str
returns df: with 6 added columns. If column names clash, the function column
name will override original column
@rtype: pandas df
"""
lookup_dict_p1 = dict()
lookup_dict_p2 = dict()
lookup_dict_taylor = dict()
lookup_dict_kd = dict()
lookup_dict_polarity = dict()
lookup_dict_calcprop = dict()
for k, v in oneletter_aa_dict.items():
lookup_dict_p1[k] = v['aa_prop1']
lookup_dict_p2[k] = v['aa_prop2']
lookup_dict_taylor[k] = v['aa_taylor']
lookup_dict_kd[k] = v['aa_prop_water']
lookup_dict_polarity[k] = v['aa_prop_polarity']
lookup_dict_calcprop[k] = v['aa_calcprop']
#if DEBUG:
# print('Key:', k, 'value:', v
# , '\n============================================================'
# , '\nlook up dict:\n')
df['wt_aap1'] = df['wild_type'].map(lookup_dict_p1)
df['mut_aap1'] = df['mutant_type'].map(lookup_dict_p1)
df['wt_aap2'] = df['wild_type'].map(lookup_dict_p2)
df['mut_aap2'] = df['mutant_type'].map(lookup_dict_p2)
df['wt_aap_taylor'] = df['wild_type'].map(lookup_dict_taylor)
df['mut_aap_taylor'] = df['mutant_type'].map(lookup_dict_taylor)
df['wt_aap_kd'] = df['wild_type'].map(lookup_dict_kd)
df['mut_aap_kd'] = df['mutant_type'].map(lookup_dict_kd)
df['wt_aap_polarity'] = df['wild_type'].map(lookup_dict_polarity)
df['mut_aap_polarity'] = df['mutant_type'].map(lookup_dict_polarity)
df['wt_aa_calcprop'] = df['wild_type'].map(lookup_dict_calcprop)
df['mut_aa_calcprop'] = df['mutant_type'].map(lookup_dict_calcprop)
return df
#========================================

191
scripts/add_aa_prop.py Normal file
View file

@ -0,0 +1,191 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Created on Tue Aug 6 12:56:03 2019
@author: tanu
'''
# FIXME: import dirs.py to get the basic dir paths available
#=======================================================================
# TASK:
#
# Input:
# Output:
#=======================================================================
#%% load libraries
import os, sys
import pandas as pd
#import numpy as np
#from varname import nameof
import argparse
#=======================================================================
#%% specify input and curr dir
homedir = os.path.expanduser('~')
# set working dir
os.getcwd()
os.chdir(homedir + '/git/LSHTM_analysis/scripts')
os.getcwd()
from aa_prop import get_aa_prop
#=======================================================================
#%% command line args
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-d', '--drug', help='drug name', default = None)
arg_parser.add_argument('-g', '--gene', help='gene name', default = None)
arg_parser.add_argument('--datadir', help = 'Data Directory. By default, it assmumes homedir + git/Data')
arg_parser.add_argument('-i', '--input_dir', help = 'Input dir containing pdb files. By default, it assmumes homedir + <drug> + input')
arg_parser.add_argument('-o', '--output_dir', help = 'Output dir for results. By default, it assmes homedir + <drug> + output')
arg_parser.add_argument('--debug', action ='store_true', help = 'Debug Mode') # not used atm
args = arg_parser.parse_args()
#%% variable assignment: input and output
drug = args.drug
gene = args.gene
datadir = args.datadir
indir = args.input_dir
outdir = args.output_dir
#%%=======================================================================
#==============
# directories
#==============
if not datadir:
datadir = homedir + '/' + 'git/Data'
if not indir:
indir = datadir + '/' + drug + '/input'
if not outdir:
outdir = datadir + '/' + drug + '/output'
#=======
# input
#=======
#in_filename = 'merged_df3.csv'
#in_filename = gene.lower() + '_complex_mcsm_norm.csv'
in_filename_mcsm = gene.lower() + '_complex_mcsm_norm_SRY.csv' # gid
infile_mcsm = outdir + '/' + in_filename_mcsm
print('Input file: ', infile_mcsm
, '\n============================================================')
#=======
# output
#=======
out_filename_aaps = gene.lower() + '_mcsm_aaps.txt'
outfile_aaps = outdir + '/' + out_filename_aaps
print('Output file: ', outfile_aaps
, '\n============================================================')
#%% end of variable assignment for input and output files
#=======================================================================
#%% Read input files
print('Reading input file (merged file):', infile_mcsm)
comb_df = pd.read_csv(infile_mcsm, sep = ',')
print('Input filename: ', in_filename_mcsm
, '\nPath :', outdir
, '\nNo. of rows: ', len(comb_df)
, '\nNo. of cols: ', len(comb_df.columns)
, '\n============================================================')
# column names
list(comb_df.columns)
#%% sanity check
nrows_df = len(comb_df)
ncols_add = 12
expected_cols = len(comb_df.columns) + ncols_add
print('\nAdding aa properties for wt and mutant: ', ncols_add, ' columns in total'
, '\n===============================================================')
#%% call get_aa_prop():
get_aa_prop(df = comb_df)
#%% check dim of df
if len(comb_df) == nrows_df and len(comb_df.columns) == expected_cols:
print('Checking dim of df: '
, '\nPASS: df dim match'
, '\nno.of rows: ', len(comb_df)
, '\nno. of cols: ', len(comb_df.columns))
else:
print('\FAIL: dim mismatch'
, 'Expected rows: ', nrows_df
, '\Got: ', len(comb_df)
, '\nExpected cols: ', expected_cols
, '\nGot: ', len(comb_df.columns))
sys.exit('Aborting')
#%% summary output
sys.stdout = open(outfile_aaps, 'w')
print('################################################'
, '\n aa properties: ', gene
, '\n################################################')
print('\n-------------------'
, '\nWild-type: aap 1'
, '\n-------------------\n'
, comb_df['wt_aap1'].value_counts()
, '\n-------------------'
, '\nmutant-type: aap 1'
, '\n-------------------\n'
, comb_df['mut_aap1'].value_counts()
, '\n===================================================')
print('\n-------------------'
, '\nWild-type: aap 2'
, '\n-------------------\n'
, comb_df['wt_aap2'].value_counts()
, '\n-------------------'
, '\nmutant-type: aap 2'
, '\n-------------------\n'
, comb_df['mut_aap2'].value_counts()
, '\n===================================================')
print('\n-------------------'
, '\nWild-type: aap taylor'
, '\n-------------------\n'
, comb_df['wt_aap_taylor'].value_counts()
, '\n-------------------'
, '\nmutant-type: taylor'
, '\n-------------------\n'
, comb_df['mut_aap_taylor'].value_counts()
, '\n===================================================')
print('\n-------------------'
, '\nWild-type: aap water/kd'
, '\n-------------------\n'
, comb_df['wt_aap_kd'].value_counts()
, '\n-------------------'
, '\nmutant-type: water/kd'
, '\n-------------------\n'
, comb_df['mut_aap_kd'].value_counts()
, '\n===================================================')
print('\n-------------------'
, '\nWild-type: aap polarity'
, '\n-------------------\n'
, comb_df['wt_aap_polarity'].value_counts()
, '\n-------------------'
, '\nmutant-type: polarity'
, '\n-------------------\n'
, comb_df['mut_aap_polarity'].value_counts()
, '\n===================================================')
print('\n-------------------'
, '\nWild-type: aa calcprop'
, '\n-------------------\n'
, comb_df['wt_aa_calcprop'].value_counts()
, '\n-------------------'
, '\nmutant-type: aa calcprop'
, '\n-------------------\n'
, comb_df['mut_aa_calcprop'].value_counts()
, '\n===================================================')
#%% end of script
#=======================================================================