Mercurial > repos > chmaramis > irprofiler
comparison clonotype_computation.py @ 0:0e37e5b73273 draft
Initial commit
author | chmaramis |
---|---|
date | Fri, 30 Mar 2018 07:22:29 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0e37e5b73273 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 """ | |
3 Created on Sat Mar 24 14:00:24 2018 | |
4 | |
5 @author: chmaramis | |
6 """ | |
7 | |
8 from __future__ import division | |
9 import numpy as np | |
10 from pandas import * | |
11 import functools as ft | |
12 import sys | |
13 import time | |
14 | |
15 frm = lambda x,y: '{r}/{l}'.format(r=x,l=y) | |
16 | |
17 clono_def = {'CDR3': ['AA JUNCTION'], | |
18 'VCDR3': ['V-GENE','AA JUNCTION'], | |
19 'JCDR3': ['J-GENE','AA JUNCTION'], | |
20 'VJCDR3': ['V-GENE','J-GENE','AA JUNCTION'], | |
21 'VDJCDR3': ['V-GENE','D-GENE','J-GENE','AA JUNCTION']} | |
22 | |
23 | |
24 def clonotypeComputation(inp_name, clono, out1, t10n, fname): | |
25 | |
26 clono_comps = clono_def[clono] | |
27 | |
28 frame = DataFrame() | |
29 tp = read_csv(inp_name, iterator=True, chunksize=5000,sep='\t', index_col=0 ) | |
30 frame = concat([chunk for chunk in tp]) | |
31 | |
32 | |
33 grouped = frame.groupby(clono_comps) | |
34 x=grouped.size() | |
35 x1=DataFrame(list(x.index), columns=clono_comps) | |
36 x1['Reads']=x.values | |
37 total = sum(x1['Reads']) | |
38 #x1['Reads/Total'] = ['{r}/{l}'.format(r=pr , l = total) for pr in x1['Reads']] | |
39 x1['Reads/Total'] = x1['Reads'].map(ft.partial(frm, y=total)) | |
40 x1['Frequency %'] = (100*x1['Reads']/total).map('{:.4f}'.format) | |
41 | |
42 final = x1.sort_values(by = ['Reads'] , ascending = False) | |
43 | |
44 final.index=range(1,len(final)+1) | |
45 final.to_csv(out1 , sep = '\t') | |
46 | |
47 numofclono = len(final) | |
48 clust = len(final[final['Reads'] > 1]) | |
49 sing = len (final[final['Reads'] == 1]) | |
50 top10 = final[clono_comps + ['Frequency %']].head(10) | |
51 top10.to_csv(t10n , sep = '\t') | |
52 | |
53 summary = [[clono]] | |
54 summary.append([', '.join([top10[c].values[0] for c in clono_comps])]) | |
55 summary.append([top10['Frequency %'].values[0]]) | |
56 summary.append([numofclono]) | |
57 summary.append([clust,'{:.4f}'.format(100*clust/numofclono)]) | |
58 summary.append([sing,'{:.4f}'.format(100*sing/numofclono)]) | |
59 | |
60 ind = ['Clonotype Definition', 'Dominant Clonotype', 'Frequency', 'Number of Clonotypes' , 'Expanding Clonotypes','Singletons'] | |
61 spl = fname.split('_') | |
62 col = [spl[0],'%'] | |
63 | |
64 frsum = DataFrame(summary,index = ind, columns = col) | |
65 | |
66 return frsum | |
67 | |
68 | |
69 if __name__ == '__main__': | |
70 | |
71 start=time.time() | |
72 | |
73 # Parse input arguments | |
74 inp_name = sys.argv[1] | |
75 clono = sys.argv[2] | |
76 out1 = sys.argv[3] | |
77 t10n = sys.argv[4] | |
78 sname = sys.argv[5] | |
79 fname = sys.argv[6] | |
80 | |
81 # Execute basic function | |
82 frsum = clonotypeComputation(inp_name, clono, out1, t10n, fname) | |
83 | |
84 # Save output to CSV files | |
85 if not frsum.empty: | |
86 frsum.to_csv(sname, sep = '\t') | |
87 | |
88 # Print execution time | |
89 stop=time.time() | |
90 print('Runtime:' + str(stop-start)) |