Mercurial > repos > chmaramis > testirprofiler
comparison cmpb2016/ext_repertoire_V.py @ 0:8be019b173e6 draft
Uploaded included tools
| author | chmaramis |
|---|---|
| date | Sun, 18 Mar 2018 05:54:20 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:8be019b173e6 |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 """ | |
| 3 Created on Fri Jun 20 14:58:08 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 def repertoireComputation(inp_name, fname): | |
| 18 | |
| 19 df = DataFrame() | |
| 20 df = read_csv(inp_name, sep='\t', index_col=0 ) | |
| 21 #tp = read_csv(inp_name, iterator=True, chunksize=5000,sep='\t', index_col=0 ) | |
| 22 #df = concat([chunk for chunk in tp]) | |
| 23 | |
| 24 | |
| 25 vgroup = df.groupby(['V-GENE']) | |
| 26 vdi = vgroup.size() | |
| 27 rep = DataFrame(list(vdi.index), columns=['V-GENE']) | |
| 28 rep['Reads'] = vdi.values | |
| 29 #rep['Reads/Total'] = ['{r}/{l}'.format(r=p , l = len(df)) for p in vdi.values] | |
| 30 rep['Reads/Total'] = rep['Reads'].map(ft.partial(frm, y=len(df))) | |
| 31 rep['Frequency %'] = (100*rep['Reads']/len(df)).map('{:.4f}'.format) | |
| 32 | |
| 33 rep = rep.sort_values(by = ['Reads'] , ascending = False) | |
| 34 rep.index = range(1,len(rep)+1) | |
| 35 | |
| 36 su = rep[['V-GENE','Frequency %']].head(10) | |
| 37 spl = fname.split('_') | |
| 38 summdf = DataFrame([su['V-GENE'].values[0],su['Frequency %'].values[0]], | |
| 39 index = ['Dominant V-GENE','Frequency'], columns = [spl[0]]) | |
| 40 summdf['%'] = '' | |
| 41 | |
| 42 return (rep, su, summdf) | |
| 43 | |
| 44 | |
| 45 if __name__ == '__main__': | |
| 46 | |
| 47 start=time.time() | |
| 48 | |
| 49 # Parse input arguments | |
| 50 inp_name = sys.argv[1] | |
| 51 outrep = sys.argv[2] | |
| 52 summ_rep = sys.argv[3] | |
| 53 summ_rep2 = sys.argv[4] | |
| 54 fname = sys.argv[5] | |
| 55 | |
| 56 # Execute basic function | |
| 57 rep, su, summdf = repertoireComputation(inp_name, fname) | |
| 58 | |
| 59 # Save output to CSV files | |
| 60 if not rep.empty: | |
| 61 rep.to_csv(outrep, sep = '\t') | |
| 62 if not su.empty: | |
| 63 su.to_csv(summ_rep, sep = '\t') | |
| 64 if not summdf.empty: | |
| 65 summdf.to_csv(summ_rep2, sep = '\t') | |
| 66 | |
| 67 # Print execution time | |
| 68 stop=time.time() | |
| 69 print('Runtime:' + str(stop-start)) |
