|
| 1 | +import pandas as pd |
| 2 | +import synapseclient |
| 3 | +import argparse |
| 4 | +import math |
| 5 | + |
| 6 | + |
| 7 | +def get_copy_call(a): |
| 8 | + """ |
| 9 | + Helper Function - Determine copy call for a value. |
| 10 | + """ |
| 11 | + |
| 12 | + if a is None: |
| 13 | + return float('nan') |
| 14 | + |
| 15 | + if math.isnan(a): |
| 16 | + return float('nan') |
| 17 | + |
| 18 | + a_val = a##math.log2(float(a)+0.000001) ###this should not be exponent, should be log!!! 2**float(a) |
| 19 | + if a_val < 0.0: #0.5210507: |
| 20 | + return 'deep del' |
| 21 | + elif a_val < 0.7311832: |
| 22 | + return 'het loss' |
| 23 | + elif a_val < 1.214125: |
| 24 | + return 'diploid' |
| 25 | + elif a_val < 1.731183: |
| 26 | + return 'gain' |
| 27 | + else: |
| 28 | + return 'amp' |
| 29 | + |
| 30 | + return pl.Series([get_copy_call(a) for a in arr]) |
| 31 | + |
| 32 | +def parseCNVFile(fpath, sampid, genes): |
| 33 | + log2data = pd.read_csv(fpath, sep='\t', header=None) |
| 34 | + log2data.columns = ['gene_symbol','copy_number','Region','Type','Pos'] |
| 35 | + log2data['improve_sample_id']=sampid |
| 36 | + newdat = pd.merge(log2data,genes)[['improve_sample_id','entrez_id','copy_number']].drop_duplicates() |
| 37 | + newdat['study']='pancpdo' |
| 38 | + newdat['source']='TiriacEtal' |
| 39 | + newdat = newdat[['improve_sample_id','entrez_id','copy_number','source','study']] |
| 40 | + newdat['copy_call'] = [get_copy_call(a) for a in newdat['copy_number']] |
| 41 | + return newdat |
| 42 | + |
| 43 | + |
| 44 | +mutmap = {'CODON_CHANGE_PLUS_CODON_DELETION':'In_Frame_Del', ##this isn't a great mapping |
| 45 | + 'CODON_CHANGE_PLUS_CODON_INSERTION':'In_Frame_Ins', ##this isn't a great mapping |
| 46 | + 'CODON_DELETION':'In_Frame_Del', |
| 47 | + 'CODON_INSERTION':'In_Frame_Ins', |
| 48 | + 'DOWNSTREAM':"3'Flank", |
| 49 | + 'FRAME_SHIFT':'Frameshift_Variant', |
| 50 | + 'FRAME_SHIFT+SPLICE_SITE_ACCEPTOR+SPLICE_SITE_REGION+INTRON':'Frameshift_Variant', |
| 51 | + 'FRAME_SHIFT+SPLICE_SITE_REGION':'Frameshift_Variant', |
| 52 | + 'INTERGENIC':'IGR', |
| 53 | + 'INTRON':'Intron', |
| 54 | + 'NON_SYNONYMOUS_CODING':'Missense_Mutation', |
| 55 | + 'NON_SYNONYMOUS_CODING+SPLICE_SITE_REGION':'Missense_Mutation', |
| 56 | + 'SPLICE_SITE_ACCEPTOR+INTRON':'Splice_Site', |
| 57 | + 'SPLICE_SITE_DONOR+INTRON':'Splice_Site', |
| 58 | + 'SPLICE_SITE_REGION+INTRON':'Splice_Site', |
| 59 | + 'SPLICE_SITE_REGION+NON_CODING_EXON_VARIANT':'Splice_Site', |
| 60 | + 'SPLICE_SITE_REGION+SYNONYMOUS_CODING':'Silent', |
| 61 | + 'START_GAINED+UTR_5_PRIME':'Start_Codon_Ins', |
| 62 | + 'STOP_GAINED':'Stop_Codon_Ins', |
| 63 | + 'STOP_GAINED+CODON_CHANGE_PLUS_CODON_INSERTION':'Stop_Codon_Ins', |
| 64 | + 'SYNONYMOUS_CODING':'Silent', |
| 65 | + 'UPSTREAM':"5'Flank", |
| 66 | + 'UTR_3_PRIME':"3'UTR", |
| 67 | + 'UTR_5_PRIME':"5'UTR" |
| 68 | + } |
| 69 | + |
| 70 | +def parseMutFile(fpath, sampid,genes): |
| 71 | + ''' |
| 72 | + move mutations to following headers: |
| 73 | + entrez_id, improve_sample_id, source, study, mutation, variant_classification |
| 74 | + ''' |
| 75 | + mutfile = pd.read_csv(fpath,sep='\t')[['SNPEFF_GENE_NAME','SNPEFF_EFFECT','SNPEFF_CDS_CHANGE']] |
| 76 | + mutfile = mutfile.dropna(subset='SNPEFF_CDS_CHANGE') |
| 77 | + mutfile.columns = ['gene_symbol','SNPEFF_EFFECT','mutation'] |
| 78 | + fullfile = pd.merge(mutfile,pd.DataFrame({'SNPEFF_EFFECT':mutmap.keys(),'variant_classification':mutmap.values()})) |
| 79 | + fullfile = pd.merge(fullfile,genes) |
| 80 | + fullfile['improve_sample_id'] = sampid |
| 81 | + fullfile['source']='TiriacEtAl' |
| 82 | + fullfile['study']='pancpdo' |
| 83 | + fullfile = fullfile[['improve_sample_id','entrez_id','source','study','mutation','variant_classification']] |
| 84 | + fullfile = fullfile.dropna().drop_duplicates() |
| 85 | + return fullfile |
| 86 | + |
| 87 | +def main(): |
| 88 | + parser = argparse.ArgumentParser(description = 'Script that collects WES and CNV data from Synapse for Coderdata') |
| 89 | + parser.add_argument('-s', '--samples', help='Path to sample file',default=None) |
| 90 | + parser.add_argument('-g', '--genes', help='Path to genes file', default = None) |
| 91 | + parser.add_argument('-c', '--copy', help='Flag to capture copy number data', action='store_true', default=False) |
| 92 | + parser.add_argument('-m', '--mutation', help='Flag to capture mutation data', action='store_true', default=False) |
| 93 | + parser.add_argument('-t', '--token', help='Synapse token') |
| 94 | + |
| 95 | + args = parser.parse_args() |
| 96 | + if args.samples is None or args.genes is None: |
| 97 | + print('We need at least a genes and samples file to continue') |
| 98 | + exit() |
| 99 | + samps = pd.read_csv(args.samples) |
| 100 | + genes = pd.read_csv(args.genes) |
| 101 | + |
| 102 | + print("Logging into synapse") |
| 103 | + sc = synapseclient.Synapse() |
| 104 | + sc.login(authToken=args.token) |
| 105 | + |
| 106 | + ##to double check identifiers, we use transcriptomics data since that determines what samples were sequenced |
| 107 | + #update this step isn't needed anymore |
| 108 | + trans = pd.read_csv('/tmp/pancpdo_transcriptomics.csv.gz') |
| 109 | + tsamps = samps[samps.improve_sample_id.isin(trans.improve_sample_id)] |
| 110 | + print(samps.shape) |
| 111 | + print(tsamps.shape) |
| 112 | + |
| 113 | + |
| 114 | + missingsamples = [] |
| 115 | + if args.copy: |
| 116 | + ##query synapse view for files |
| 117 | + cnvs = sc.tableQuery("select * from syn64608378 where parentId='syn64608163'").asDataFrame() |
| 118 | + alldats = [] |
| 119 | + ##go through table and get every file |
| 120 | + for index,row in cnvs.iterrows(): |
| 121 | + sid = row.id |
| 122 | + sname = row['name'].split('--')[0] |
| 123 | + print(sid,sname) |
| 124 | + path = sc.get(sid).path |
| 125 | + if sname in set(tsamps.other_id): |
| 126 | + print(sname+' in transcriptomics, using that id') |
| 127 | + sampid = tsamps.loc[tsamps.other_id==sname]['improve_sample_id'].values[0] |
| 128 | + missingsamples.append('copy,trans,'+sname) |
| 129 | + elif sname in set(samps.other_id): |
| 130 | + print(sname+' in samples but not transcriptomics, using other id') |
| 131 | + sampid = samps.loc[samps.other_id==sname]['improve_sample_id'].values[0] |
| 132 | + missingsamples.append("copy,notrans,"+sname) |
| 133 | + else: |
| 134 | + print('Missing sample id for '+sname,' skipping for now') |
| 135 | + missingsamples.append('copy,missed,'+sname) |
| 136 | + continue |
| 137 | + sampid = samps.loc[samps.other_id==sname]['improve_sample_id'].values[0] |
| 138 | + res = parseCNVFile(path,sampid, genes) |
| 139 | + alldats.append(res) |
| 140 | + newcnv = pd.concat(alldats) |
| 141 | + newcnv.to_csv('/tmp/pancpdo_copy_number.csv.gz',compression='gzip',index=False) |
| 142 | + |
| 143 | + if args.mutation: |
| 144 | + wes = sc.tableQuery("select * from syn64608378 where parentId='syn64608263'").asDataFrame() |
| 145 | + alldats = [] |
| 146 | + ##go through and get every mutation file |
| 147 | + for index,row in wes.iterrows(): |
| 148 | + sname = row['name'].split('--')[0] |
| 149 | + sid = row.id |
| 150 | + print(sid,sname) |
| 151 | + if sname in set(tsamps.other_id): |
| 152 | + print(sname+' in transcriptomics, using that id') |
| 153 | + sampid = tsamps.loc[tsamps.other_id==sname]['improve_sample_id'].values[0] |
| 154 | + missingsamples.append('mutation,trans,'+sname) |
| 155 | + elif sname in set(samps.other_id): |
| 156 | + print(sname+' in samples but not transcriptomics, using other id') |
| 157 | + sampid = samps.loc[samps.other_id==sname]['improve_sample_id'].values[0] |
| 158 | + missingsamples.append('mutation,notrans,'+sname) |
| 159 | + else: |
| 160 | + print('Missing sample id for '+sname) |
| 161 | + missingsamples.append('mutation,'+sname) |
| 162 | + continue |
| 163 | + path = sc.get(sid).path |
| 164 | + sampid = samps.loc[samps.other_id==sname]['improve_sample_id'].values[0] |
| 165 | + res = parseMutFile(path,sampid, genes) |
| 166 | + alldats.append(res) |
| 167 | + newmut = pd.concat(alldats) |
| 168 | + newmut.to_csv("/tmp/pancpdo_mutations.csv.gz",compression='gzip',index=False) |
| 169 | + #pd.DataFrame(missingsamples).to_csv('missing.csv',index=False,quoting=None,header=False) |
| 170 | +if __name__=='__main__': |
| 171 | + main() |
0 commit comments