annotate calctfreq.py @ 8:30771a097f14

changed max line length from 5,000 to 50,000
author Richard Burhans <burhans@bx.psu.edu>
date Wed, 20 Jun 2012 14:02:30 -0400
parents 2c498d40ecde
children d6b961721037
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
1 #!/usr/bin/env python
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
2 # -*- coding: utf-8 -*-
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
3 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
4 # calcfreq.py
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
5 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
6 # Copyright 2011 Oscar Bedoya-Reina <oscar@niska.bx.psu.edu>
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
7 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
8 # This program is free software; you can redistribute it and/or modify
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
9 # it under the terms of the GNU General Public License as published by
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
10 # the Free Software Foundation; either version 2 of the License, or
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
11 # (at your option) any later version.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
12 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
16 # GNU General Public License for more details.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
17 #
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
18 # You should have received a copy of the GNU General Public License
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
19 # along with this program; if not, write to the Free Software
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
21 # MA 02110-1301, USA.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
22
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
23 import argparse,os,sys
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
24 from decimal import Decimal,getcontext
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
25 from LocationFile import LocationFile
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
26
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
27 #method to rank the the pthways by mut. freq.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
28 def rankd(ltfreqs):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
29 ordvals=sorted(ltfreqs)#sort and reverse freqs.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
30 #~
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
31 outrnk=[]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
32 tmpFreq0,tmpCount,tmpPthw=ordvals.pop()#the highest possible value
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
33 crank=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
34 outrnk.append('\t'.join([str(tmpCount),str(tmpFreq0),str(crank),tmpPthw]))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
35 totalnvals=len(ordvals)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36 cnt=0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37 while totalnvals>cnt:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
38 cnt+=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
39 tmpFreq,tmpCount,tmpPthw=ordvals.pop()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
40 if tmpFreq!=tmpFreq0:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
41 crank=len(outrnk)+1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
42 tmpFreq0=tmpFreq
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
43 outrnk.append('\t'.join([str(tmpCount),str(tmpFreq),str(crank),tmpPthw]))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
44 return outrnk
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47 def main():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
48 parser = argparse.ArgumentParser(description='Obtain KEGG images from a list of genes.')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
49 parser.add_argument('--input',metavar='input TXT file',type=str,help='the input file with the table in txt format')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50 parser.add_argument('--output',metavar='output TXT file',type=str,help='the output file with the table in txt format. Column 1 is the count of genes in the list, Column 2 is the percentage of the pathway genes present on the list. Column 3 is the rank based on column 2')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51 parser.add_argument('--posKEGGclmn',metavar='column number',type=int,help='the column with the KEGG pathway code/name')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52 parser.add_argument('--KEGGgeneposcolmn',metavar='column number',type=int,help='column with the KEGG gene code')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53 parser.add_argument('--loc_file',metavar='location file',type=str,help='location file')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
54 parser.add_argument('--species',metavar='species',type=str,help='species')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
55 #~Open arguments
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
56 class C(object):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
57 pass
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
58 fulargs=C()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
59 parser.parse_args(sys.argv[1:],namespace=fulargs)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 #test input vars
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61 inputf,outputf,posKEGGclmn,Kgeneposcolmn=fulargs.input,fulargs.output,fulargs.posKEGGclmn,fulargs.KEGGgeneposcolmn
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 locf,species=fulargs.loc_file,fulargs.species
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 #make a dictionary of valid genes
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64 posKEGGclmn-=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 Kgeneposcolmn-=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66 dKEGGcPthws=dict([(x.split('\t')[Kgeneposcolmn],set(x.split('\t')[posKEGGclmn].split('.'))) for x in open(inputf).read().splitlines()[1:] if x.strip()])
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67 sdGenes=set([x for x in dKEGGcPthws.keys() if x.find('.')>-1])
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68 while True:#to correct names with more than one gene
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70 mgenes=sdGenes.pop()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71 pthwsAssotd=dKEGGcPthws.pop(mgenes)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 mgenes=mgenes.split('.')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73 for eachg in mgenes:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74 dKEGGcPthws[eachg]=pthwsAssotd
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75 except:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 break
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 #~ Count genes
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78 getcontext().prec=2#set 2 decimal places
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
79
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
80 location_file = LocationFile(locf)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
81 prefix, kxml_dir_path, dict_file = location_file.get_values(species)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
82 dPthContsTotls = {}
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
83 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
84 with open(dict_file) as fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
85 for line in fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
86 line = line.rstrip('\r\n')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
87 value, key = line.split('\t')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
88 dPthContsTotls[key] = int(value)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
89 except IOError, err:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
90 print >> sys.stderr, 'Error opening dict file {0}: {1}'.format(dict_file, err.strerror)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
91 sys.exit(1)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
92
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
93 dPthContsTmp=dict([(x,0) for x in dPthContsTotls.keys()])#create a list of genes
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
94 sdGenes=set([x for x in dKEGGcPthws.keys()])#list of all genes
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
95 cntGens=0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
96 ltGens=len(sdGenes)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
97 while cntGens<ltGens:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
98 cGen=sdGenes.pop()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
99 sKEGGcPthws=dKEGGcPthws.pop(cGen)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
100 for eachP in sKEGGcPthws:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
101 if eachP!='N':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
102 dPthContsTmp[eachP]+=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
103 cntGens+=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
104 #~ Calculate Freqs.
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
105 ltfreqs=[((Decimal(dPthContsTmp[x])/Decimal(dPthContsTotls[x])),Decimal(dPthContsTmp[x]),x) for x in dPthContsTotls]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
106 tabllfreqs='\n'.join(rankd(ltfreqs))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
107 salef=open(outputf,'w')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
108 salef.write(tabllfreqs)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
109 salef.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
110 return 0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
111
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
112
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
113 if __name__ == '__main__':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
114 main()