|
12
|
1 # -*- coding: utf-8 -*-
|
|
|
2 """
|
|
|
3 Created on Thu Jun 19 17:33:34 2014
|
|
|
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
|
|
|
18 def cdr3Computation(inp_name, out1, t10n, fname):
|
|
|
19
|
|
|
20 frame = DataFrame()
|
|
|
21 tp = read_csv(inp_name, iterator=True, chunksize=5000,sep='\t', index_col=0 )
|
|
|
22 frame = concat([chunk for chunk in tp])
|
|
|
23
|
|
|
24 grouped = frame.groupby(['AA JUNCTION'])
|
|
|
25 x=grouped.size()
|
|
|
26 x1=DataFrame(x.index, columns=['AA JUNCTION'])
|
|
|
27 x1['Reads']=x.values
|
|
|
28 total = sum(x1['Reads'])
|
|
|
29 #x1['Reads/Total'] = ['{r}/{l}'.format(r=pr , l = total) for pr in x1['Reads']]
|
|
|
30 x1['Reads/Total'] = x1['Reads'].map(ft.partial(frm, y=total))
|
|
|
31 x1['Frequency %'] = (100*x1['Reads']/total).map('{:.4f}'.format)
|
|
|
32
|
|
|
33 final = x1.sort_values(by = ['Reads'] , ascending = False)
|
|
|
34
|
|
|
35 final.index=range(1,len(final)+1)
|
|
|
36 final.to_csv(out1 , sep = '\t')
|
|
|
37
|
|
|
38 numofclono = len(final)
|
|
|
39 clust = len(final[final['Reads'] > 1])
|
|
|
40 sing = len (final[final['Reads'] == 1])
|
|
|
41 top10 = final[['AA JUNCTION','Frequency %']].head(10)
|
|
|
42 top10.to_csv(t10n , sep = '\t')
|
|
|
43
|
|
|
44 summary = [[str(top10['AA JUNCTION'].values[0])]]
|
|
|
45 summary.append([top10['Frequency %'].values[0]])
|
|
|
46 summary.append([numofclono])
|
|
|
47 summary.append([clust,'{:.4f}'.format(100*clust/numofclono)])
|
|
|
48 summary.append([sing,'{:.4f}'.format(100*sing/numofclono)])
|
|
|
49
|
|
|
50 ind = ['Dominant Clonotype (CDR3)', 'Frequency', 'Number of Clonotypes' , 'Expanding Clonotypes','Singletons']
|
|
|
51 spl = fname.split('_')
|
|
|
52 col = [spl[0],'%']
|
|
|
53
|
|
|
54 frsum = DataFrame(summary,index = ind, columns = col)
|
|
|
55
|
|
|
56 return frsum
|
|
|
57
|
|
|
58 if __name__ == '__main__':
|
|
|
59
|
|
|
60 start=time.time()
|
|
|
61
|
|
|
62 # Parse input arguments
|
|
|
63 inp_name = sys.argv[1]
|
|
|
64 out1 = sys.argv[2]
|
|
|
65 t10n = sys.argv[3]
|
|
|
66 sname = sys.argv[4]
|
|
|
67 fname = sys.argv[5]
|
|
|
68
|
|
|
69 # Execute basic function
|
|
|
70 frsum = cdr3Computation(inp_name,out1,t10n,fname)
|
|
|
71
|
|
|
72 # Save output to CSV files
|
|
|
73 if not frsum.empty:
|
|
|
74 frsum.to_csv(sname, sep = '\t')
|
|
|
75
|
|
|
76 # Print execution time
|
|
|
77 stop=time.time()
|
|
|
78 print('Runtime:' + str(stop-start))
|