minor changes to variable names in .R & .py

This commit is contained in:
Tanushree Tunstall 2020-03-24 10:36:51 +00:00
parent dd91692673
commit c184841951
2 changed files with 96 additions and 75 deletions

View file

@ -14,7 +14,7 @@ Created on Tue Aug 6 12:56:03 2019
# load libraries
import os, sys
import pandas as pd
import numpy as np
#import numpy as np
#from pandas.api.types import is_string_dtype
#from pandas.api.types import is_numeric_dtype
@ -61,21 +61,21 @@ gene_match = gene + '_p.'
# input dir
#=======
#indir = 'git/Data/pyrazinamide/input/original'
indir = 'git/Data' + '/' + drug + '/' + 'input/original'
indir = homedir + '/' + 'git/Data'
#=========
# output dir
#=========
# several output files
# output filenames in respective sections at the time of outputting files
#outdir = 'git/Data/pyrazinamide/output'
outdir = 'git/Data' + '/' + drug + '/' + 'output'
outdir = homedir + '/' + 'git/Data' + '/' + drug + '/' + 'output'
#%%end of variable assignment for input and output files
#==============================================================================
#%% Read files
in_filename = 'original_tanushree_data_v2.csv'
infile = homedir + '/' + indir + '/' + in_filename
infile = indir + '/' + in_filename
print('Reading input master file:', infile)
master_data = pd.read_csv(infile, sep = ',')
@ -325,12 +325,12 @@ print('======================================================================')
#%% write file
#print(outdir)
out_filename0 = gene.lower() + '_' + 'common_ids.csv'
outfile0 = homedir + '/' + outdir + '/' + out_filename0
outfile0 = outdir + '/' + out_filename0
#FIXME: CHECK line len(common_ids)
print('Writing file: common ids:',
'\nFilename:', out_filename0,
'\nPath:', homedir +'/'+ outdir,
'\nPath:', outdir,
'\nExpected no. of rows:', len(common_ids) )
common_ids.to_csv(outfile0)
@ -690,7 +690,7 @@ print('Counting no. of ambiguous muts...')
if dr_muts[dr_muts.isin(other_muts)].nunique() == other_muts[other_muts.isin(dr_muts)].nunique():
common_muts = dr_muts[dr_muts.isin(other_muts)].value_counts().keys().tolist()
print('No. of ambigiuous muts detected:'+ str(len(common_muts)),
print('Distinct no. of ambigiuous muts detected:'+ str(len(common_muts)),
'list of ambiguous mutations (see below):', *common_muts, sep = '\n')
else:
print('Error: ambiguous muts detected, but extraction failed. Debug!',
@ -711,21 +711,27 @@ del(c1, c2, col_to_split1, col_to_split2, comp_pnca_samples, dr_WF0, dr_df, dr_m
#other_muts.to_csv('other_muts.csv', header = True)
out_filename1 = gene.lower() + '_' + 'ambiguous_muts.csv'
outfile1 = homedir + '/' + outdir + '/' + out_filename1
outfile1 = outdir + '/' + out_filename1
print('Writing file: ambiguous muts',
'\nFilename:', out_filename1,
'\nPath:', homedir +'/'+ outdir)
'\nPath:', outdir)
#common_muts = ['pncA_p.Val180Phe','pncA_p.Gln10Pro'] # test
inspect = pnca_LF1[pnca_LF1['mutation'].isin(common_muts)]
inspect.to_csv(outfile1)
print('Finished writing:', out_filename1, '\nExpected no. of rows (no. of samples with the ambiguous muts present):', dr_muts.isin(other_muts).sum() + other_muts.isin(dr_muts).sum())
print('Finished writing:', out_filename1,
'\nNo. of rows:', len(inspect),
'\nNo. of cols:', len(inspect.columns),
'\nNo. of rows = no. of samples with the ambiguous muts present:', dr_muts.isin(other_muts).sum() + other_muts.isin(dr_muts).sum())
print('======================================================================')
del(out_filename1)
#%%
#%% read aa dict and pull relevant info
print('Reading aa dict and fetching1 letter aa code',
'\nFormatting mutation in mCSM style format: {WT}<POS>{MUT}',
'\nAdding aa properties')
#===========
# Split 'mutation' column into three: wild_type, position and
# mutant_type separately. Then map three letter code to one using
@ -733,7 +739,6 @@ del(out_filename1)
# First: Import reference dict
# Second: convert to mutation to lowercase for compatibility with dict
#===========
from reference_dict import my_aa_dict # CHECK DIR STRUC THERE!
pnca_LF1['mutation'] = pnca_LF1.loc[:, 'mutation'].str.lower()
#=======
# Iterate through the dict, create a lookup dict i.e
@ -756,7 +761,7 @@ pnca_LF1['position'] = pnca_LF1['mutation'].str.extract(r'(\d+)')
# clear variables
del(k, v, wt, mut, lookup_dict)
print('======================================================================')
#print('======================================================================')
#=========
# iterate through the dict, create a lookup dict that i.e
# lookup_dict = {three_letter_code: aa_prop_water}
@ -777,7 +782,7 @@ for k, v in my_aa_dict.items():
# clear variables
del(k, v, wt, mut, lookup_dict)
print('======================================================================')
#print('======================================================================')
#========
# iterate through the dict, create a lookup dict that i.e
# lookup_dict = {three_letter_code: aa_prop_polarity}
@ -798,7 +803,7 @@ for k, v in my_aa_dict.items():
# clear variables
del(k, v, wt, mut, lookup_dict)
print('======================================================================')
#print('======================================================================')
#========
# iterate through the dict, create a lookup dict that i.e
@ -866,12 +871,12 @@ else:
print('======================================================================')
out_filename2 = gene.lower() + '_' + 'mcsm_snps.csv'
outfile2 = homedir + '/' + outdir + '/' + out_filename2
outfile2 = outdir + '/' + out_filename2
print('Writing file: mCSM style muts',
'\nFilename:', out_filename2,
'\nPath:', homedir +'/'+ outdir,
'\nmutation format (SNP): {Wt}<POS>{Mut}',
'\nPath:', outdir,
'\nmutation format (SNP): {WT}<POS>{MUT}',
'\nNo. of distinct muts:', len(snps_only),
'\nNo. of distinct positions:', len(pos_only))
@ -887,10 +892,10 @@ del(out_filename2)
#%% Write file: pnca_metadata (i.e pnca_LF1)
# where each row has UNIQUE mutations NOT unique sample ids
out_filename3 = gene.lower() + '_' + 'metadata.csv'
outfile3 = homedir + '/' + outdir + '/' + out_filename3
outfile3 = outdir + '/' + out_filename3
print('Writing file: LF formatted data',
'\nFilename:', out_filename3,
'\nPath:', homedir +'/'+ outdir)
'\nPath:', outdir)
pnca_LF1.to_csv(outfile3, header = True, index = False)
print('Finished writing:', out_filename3,
@ -929,12 +934,12 @@ else:
print('======================================================================')
out_filename4 = gene.lower() + '_' + 'all_muts_msa.csv'
outfile4 = homedir + '/' + outdir + '/' + out_filename4
outfile4 = outdir + '/' + out_filename4
print('Writing file: mCSM style muts for msa',
'\nFilename:', out_filename4,
'\nPath:', homedir +'/'+ outdir,
'\nmutation format (SNP): {Wt}<POS>{Mut}',
'\nPath:', outdir,
'\nmutation format (SNP): {WT}<POS>{MUT}',
'\nNo.of lines of msa:', len(all_muts_msa),
)
@ -961,12 +966,12 @@ pos_only.position.dtype
pos_only_sorted = pos_only.sort_values(by = 'position', ascending = True)
out_filename5 = gene.lower() + '_' + 'mutational_positons.csv'
outfile5 = homedir + '/' + outdir + '/' + out_filename5
outfile5 = outdir + '/' + out_filename5
print('Writing file: mutational positions',
'\nNo. of distinct positions:', len(pos_only_sorted),
'\nFilename:', out_filename5,
'\nPath:', homedir +'/'+ outdir)
'\nPath:', outdir)
pos_only_sorted.to_csv(outfile5, header = True, index = False)