Mercurial > repos > chmaramis > testirprofiler
comparison cmpb2016/ext_repertoire_J.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 repertoireJgComputation(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 vgroup = df.groupby(['J-GENE']) | |
| 25 vdi = vgroup.size() | |
| 26 rep = DataFrame(list(vdi.index), columns=['J-GENE']) | |
| 27 rep['Reads'] = vdi.values | |
| 28 #rep['Reads/Total'] = ['{r}/{l}'.format(r=p , l = len(df)) for p in vdi.values] | |
| 29 rep['Reads/Total'] = rep['Reads'].map(ft.partial(frm, y=len(df))) | |
| 30 rep['Frequency %'] = (100*rep['Reads']/len(df)).map('{:.4f}'.format) | |
| 31 | |
| 32 rep = rep.sort_values(by = ['Reads'] , ascending = False) | |
| 33 | |
| 34 rep.index = range(1,len(rep)+1) | |
| 35 | |
| 36 su = rep[['J-GENE','Frequency %']].head(10) | |
| 37 spl = fname.split('_') | |
| 38 summdf = DataFrame([su['J-GENE'].values[0],su['Frequency %'].values[0]], | |
| 39 index = ['Dominant J-GENE','Frequency'], columns = [spl[0]]) | |
| 40 summdf['%'] = '' | |
| 41 | |
| 42 return (rep, 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_rep2 = sys.argv[3] | |
| 53 fname = sys.argv[4] | |
| 54 | |
| 55 # Execute basic function | |
| 56 rep, summdf = repertoireJgComputation(inp_name, fname) | |
| 57 | |
| 58 # Save output to CSV files | |
| 59 if not rep.empty: | |
| 60 rep.to_csv(outrep, sep = '\t') | |
| 61 if not summdf.empty: | |
| 62 summdf.to_csv(summ_rep2, sep = '\t') | |
| 63 | |
| 64 | |
| 65 # Print execution time | |
| 66 stop=time.time() | |
| 67 print('Runtime:' + str(stop-start)) |
