moved scripts to /ind_scripts & added add col to formatting script
This commit is contained in:
parent
368496733a
commit
8b1a7fc71c
6 changed files with 1129 additions and 62 deletions
160
mcsm/mcsm.py
160
mcsm/mcsm.py
|
@ -158,7 +158,7 @@ def build_result_dict(web_result_raw):
|
|||
fields = line.split(':')
|
||||
#print(fields)
|
||||
if len(fields) > 1: # since Mutaton information is empty
|
||||
dict_entry = dict([(x, y) for x, y in zip(fields[::2], fields[1::2])])
|
||||
dict_entry = dict([(x, y) for x, y in zip(fields[::2], fields[1::2])])
|
||||
result_dict.update(dict_entry)
|
||||
return result_dict
|
||||
#%%
|
||||
|
@ -174,17 +174,17 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
@type pandas df
|
||||
|
||||
"""
|
||||
#############
|
||||
# Read file
|
||||
#############
|
||||
#############
|
||||
# Read file
|
||||
#############
|
||||
mcsm_data = pd.read_csv(mcsm_outputcsv, sep = ',')
|
||||
dforig_shape = mcsm_data.shape
|
||||
print('dimensions of input file:', dforig_shape)
|
||||
|
||||
#############
|
||||
# rename cols
|
||||
#############
|
||||
# format colnames: all lowercase, remove spaces and use '_' to join
|
||||
#############
|
||||
# rename cols
|
||||
#############
|
||||
# format colnames: all lowercase, remove spaces and use '_' to join
|
||||
print('Assigning meaningful colnames i.e without spaces and hyphen and reflecting units'
|
||||
, '\n===================================================================')
|
||||
my_colnames_dict = {'Predicted Affinity Change': 'PredAffLog' # relevant info from this col will be extracted and the column discarded
|
||||
|
@ -199,26 +199,26 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
|
||||
mcsm_data.rename(columns = my_colnames_dict, inplace = True)
|
||||
#%%===========================================================================
|
||||
#################################
|
||||
# populate mutation_information
|
||||
# col which is currently blank
|
||||
#################################
|
||||
# populate mutation_information column:mcsm style muts {WT}<POS>{MUT}
|
||||
#################################
|
||||
# populate mutation_information
|
||||
# col which is currently blank
|
||||
#################################
|
||||
# populate mutation_information column:mcsm style muts {WT}<POS>{MUT}
|
||||
print('Populating column : mutation_information which is currently empty\n', mcsm_data['mutation_information'])
|
||||
mcsm_data['mutation_information'] = mcsm_data['wild_type'] + mcsm_data['position'].astype(str) + mcsm_data['mutant_type']
|
||||
print('checking after populating:\n', mcsm_data['mutation_information']
|
||||
, '\n===================================================================')
|
||||
|
||||
# Remove spaces b/w pasted columns
|
||||
# Remove spaces b/w pasted columns
|
||||
print('removing white space within column: \mutation_information')
|
||||
mcsm_data['mutation_information'] = mcsm_data['mutation_information'].str.replace(' ', '')
|
||||
print('Correctly formatted column: mutation_information\n', mcsm_data['mutation_information']
|
||||
, '\n===================================================================')
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# sanity check: drop dupliate muts
|
||||
#############
|
||||
# shouldn't exist as this should be eliminated at the time of running mcsm
|
||||
#############
|
||||
# sanity check: drop dupliate muts
|
||||
#############
|
||||
# shouldn't exist as this should be eliminated at the time of running mcsm
|
||||
print('Sanity check:'
|
||||
, '\nChecking duplicate mutations')
|
||||
if mcsm_data['mutation_information'].duplicated().sum() == 0:
|
||||
|
@ -233,10 +233,10 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
print('Dim of data after removing duplicate muts:', mcsm_data.shape
|
||||
, '\n===============================================================')
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# Create col: duet_outcome
|
||||
#############
|
||||
# classification based on DUET stability values
|
||||
#############
|
||||
# Create col: duet_outcome
|
||||
#############
|
||||
# classification based on DUET stability values
|
||||
print('Assigning col: duet_outcome based on DUET stability values')
|
||||
print('Sanity check:')
|
||||
# count positive values in the DUET column
|
||||
|
@ -253,25 +253,25 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
, '\nGot no. of stabilising mutations', mcsm_data['duet_outcome'].value_counts()['Stabilising']
|
||||
, '\n===============================================================')
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# Extract numeric
|
||||
# part of ligand_distance col
|
||||
#############
|
||||
# Extract only the numeric part from col: ligand_distance
|
||||
# number: '-?\d+\.?\d*'
|
||||
#############
|
||||
# Extract numeric
|
||||
# part of ligand_distance col
|
||||
#############
|
||||
# Extract only the numeric part from col: ligand_distance
|
||||
# number: '-?\d+\.?\d*'
|
||||
mcsm_data['ligand_distance']
|
||||
print('extracting numeric part of col: ligand_distance')
|
||||
mcsm_data['ligand_distance'] = mcsm_data['ligand_distance'].str.extract('(\d+\.?\d*)')
|
||||
mcsm_data['ligand_distance']
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# Create 2 columns:
|
||||
# ligand_affinity_change and ligand_outcome
|
||||
#############
|
||||
# the numerical and categorical parts need to be extracted from column: PredAffLog
|
||||
# regex used
|
||||
# numerical part: '-?\d+\.?\d*'
|
||||
# categorocal part: '\b(\w+ing)\b'
|
||||
#############
|
||||
# Create 2 columns:
|
||||
# ligand_affinity_change and ligand_outcome
|
||||
#############
|
||||
# the numerical and categorical parts need to be extracted from column: PredAffLog
|
||||
# regex used
|
||||
# numerical part: '-?\d+\.?\d*'
|
||||
# categorocal part: '\b(\w+ing)\b'
|
||||
print('Extracting numerical and categorical parts from the col: PredAffLog')
|
||||
print('to create two columns: ligand_affinity_change and ligand_outcome'
|
||||
, '\n===================================================================')
|
||||
|
@ -306,9 +306,9 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
, '\nExpected:\n', american_spl
|
||||
, '\nGot:\n', british_spl
|
||||
, '\n===============================================================')
|
||||
#%%===========================================================================
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# ensuring corrrect dtype columns
|
||||
# ensuring corrrect dtype for numeric columns
|
||||
#############
|
||||
# check dtype in cols
|
||||
print('Checking dtypes in all columns:\n', mcsm_data.dtypes
|
||||
|
@ -319,10 +319,10 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
, '\nligand_affinity_change'
|
||||
, '\n===================================================================')
|
||||
|
||||
# using apply method to change stabilty and affinity values to numeric
|
||||
# using apply method to change stabilty and affinity values to numeric
|
||||
numeric_cols = ['duet_stability_change', 'ligand_affinity_change', 'ligand_distance']
|
||||
mcsm_data[numeric_cols] = mcsm_data[numeric_cols].apply(pd.to_numeric)
|
||||
# check dtype in cols
|
||||
# check dtype in cols
|
||||
print('checking dtype after conversion')
|
||||
cols_check = mcsm_data.select_dtypes(include='float64').columns.isin(numeric_cols)
|
||||
if cols_check.all():
|
||||
|
@ -334,12 +334,11 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
, '\n===============================================================')
|
||||
print(mcsm_data.dtypes)
|
||||
#%%===========================================================================
|
||||
|
||||
#############
|
||||
# scale duet values
|
||||
#############
|
||||
# Rescale values in DUET_change col b/w -1 and 1 so negative numbers
|
||||
# stay neg and pos numbers stay positive
|
||||
#############
|
||||
# scale duet values
|
||||
#############
|
||||
# Rescale values in DUET_change col b/w -1 and 1 so negative numbers
|
||||
# stay neg and pos numbers stay positive
|
||||
duet_min = mcsm_data['duet_stability_change'].min()
|
||||
duet_max = mcsm_data['duet_stability_change'].max()
|
||||
|
||||
|
@ -351,11 +350,11 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
, '\nScaled duet scores:\n', mcsm_data['duet_scaled'])
|
||||
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# scale affinity values
|
||||
#############
|
||||
# rescale values in affinity change col b/w -1 and 1 so negative numbers
|
||||
# stay neg and pos numbers stay positive
|
||||
#############
|
||||
# scale affinity values
|
||||
#############
|
||||
# rescale values in affinity change col b/w -1 and 1 so negative numbers
|
||||
# stay neg and pos numbers stay positive
|
||||
aff_min = mcsm_data['ligand_affinity_change'].min()
|
||||
aff_max = mcsm_data['ligand_affinity_change'].max()
|
||||
|
||||
|
@ -365,17 +364,52 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
print('Raw affinity scores:\n', mcsm_data['ligand_affinity_change']
|
||||
, '\n---------------------------------------------------------------'
|
||||
, '\nScaled affinity scores:\n', mcsm_data['affinity_scaled'])
|
||||
#=============================================================================
|
||||
# Removing PredAff log column as it is not needed?
|
||||
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# adding column: wild_position
|
||||
# useful for plots and db
|
||||
#############
|
||||
print('Creating column: wild_position')
|
||||
mcsm_data['wild_position'] = mcsm_data['wild_type'] + mcsm_data['position'].astype(str)
|
||||
print(mcsm_data['wild_position'].head())
|
||||
# Remove spaces b/w pasted columns
|
||||
print('removing white space within column: wild_position')
|
||||
mcsm_data['wild_position'] = mcsm_data['wild_position'].str.replace(' ', '')
|
||||
print('Correctly formatted column: wild_position\n', mcsm_data['wild_position'].head()
|
||||
, '\n===================================================================')
|
||||
|
||||
#%%===========================================================================
|
||||
|
||||
#############
|
||||
# ensuring corrrect dtype in non-numeric cols
|
||||
#############
|
||||
|
||||
#) char cols
|
||||
char_cols = ['PredAffLog', 'mutation_information', 'wild_type', 'mutant_type', 'chain', 'ligand_id', 'duet_outcome', 'ligand_outcome', 'wild_position']
|
||||
|
||||
#mcsm_data[char_cols] = mcsm_data[char_cols].astype(str)
|
||||
cols_check_char = mcsm_data.select_dtypes(include = 'object').columns.isin(char_cols)
|
||||
|
||||
if cols_check_char.all():
|
||||
print('PASS: dtypes for char cols:', char_cols, 'are indeed string'
|
||||
, '\n===============================================================')
|
||||
else:
|
||||
print('FAIL:dtype change to numeric for selected cols unsuccessful'
|
||||
, '\n===============================================================')
|
||||
#mcsm_data['ligand_distance', 'ligand_affinity_change'].apply(is_numeric_dtype(mcsm_data['ligand_distance', 'ligand_affinity_change']))
|
||||
print(mcsm_data.dtypes)
|
||||
#%%=============================================================================
|
||||
# Removing PredAff log column as it is not needed?
|
||||
print('Removing col: PredAffLog since relevant info has been extracted from it')
|
||||
mcsm_dataf = mcsm_data.drop(columns = ['PredAffLog'])
|
||||
#%%===========================================================================
|
||||
#############
|
||||
# sanity check before writing file
|
||||
#############
|
||||
expected_cols_toadd = 4
|
||||
#############
|
||||
# sanity check before writing file
|
||||
#############
|
||||
expected_ncols_toadd = 5
|
||||
dforig_len = dforig_shape[1]
|
||||
expected_cols = dforig_len + expected_cols_toadd
|
||||
expected_cols = dforig_len + expected_ncols_toadd
|
||||
if len(mcsm_dataf.columns) == expected_cols:
|
||||
print('PASS: formatting successful'
|
||||
, '\nformatted df has expected no. of cols:', expected_cols
|
||||
|
@ -389,9 +423,15 @@ def format_mcsm_output(mcsm_outputcsv):
|
|||
, '\n===============================================================')
|
||||
else:
|
||||
print('FAIL: something went wrong in formatting df'
|
||||
, '\nExpected no. of cols:', expected_cols
|
||||
, '\nLen of orig df:', dforig_len
|
||||
, '\nExpected number of cols to add:', expected_ncols_toadd
|
||||
, '\nExpected no. of cols:', expected_cols, '(', dforig_len, '+', expected_ncols_toadd, ')'
|
||||
, '\nGot no. of cols:', len(mcsm_dataf.columns)
|
||||
, '\nCheck formatting'
|
||||
, '\nCheck formatting:'
|
||||
, '\ncheck hardcoded value:', expected_ncols_toadd
|
||||
, '\nis', expected_ncols_toadd, 'the no. of expected cols to add?'
|
||||
, '\n===============================================================')
|
||||
|
||||
|
||||
return mcsm_dataf
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue