annotate rtrnKEGGpthwfENSEMBLTc.py @ 20:8a4b8efbc82c

Removed unicode from Fst_column.c
author miller-lab
date Tue, 23 Oct 2012 14:38:04 -0400
parents 2c498d40ecde
children
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 # calclenchange.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
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
25
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
26 def main():
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
27 parser = argparse.ArgumentParser(description='Adds the fields KEGG gene codes and KEGG pathways to an input table of ENSEMBL transcript codes.')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
28 parser.add_argument('--loc_file',metavar='correlational database',type=str,help='correlational database')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
29 parser.add_argument('--species',metavar='species name',type=str,help='the species of interest in loc_file')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
30 parser.add_argument('--output',metavar='output TXT file',type=str,help='the output file with the table in txt format. The output will have two more fields: KEGG gene codes and KEGG pathways of each ENSEMBL code' )
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
31 parser.add_argument('--posENSEMBLclmn',metavar='column number',type=int,help='the column with the ENSEMBLE transcript code')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
32 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
33 #~
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
34 #~Open arguments
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
35 class C(object):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
36 pass
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
37 fulargs=C()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
38 parser.parse_args(sys.argv[1:],namespace=fulargs)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
39 #test input vars
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
40 inputf,loc_file,species,output,posENSEMBLclmn=fulargs.input,fulargs.loc_file,fulargs.species,fulargs.output,fulargs.posENSEMBLclmn
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
41 posENSEMBLclmn-=1#correct pos
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
42 #~ Get the extra variables
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
43 crDB=[x.split() for x in open(loc_file).read().splitlines() if x.split()[0]==species][0]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
44 sppPrefx,dinput=crDB[0],crDB[1]#X should be replaced by the position in which the Conversion Dictionary File (CDF) is placed
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
45 #make a dictionary of the input CDF
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
46 dKEGGcPthws=dict([(x.split('\t')[0],'\t'.join(x.split('\t')[1:])) for x in open(dinput).read().splitlines() if x.strip()])
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
47 #~ add the two new columns
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
48 sall=[]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
49 #lENSEMBLTc=[x.split('\t') for x in open(inputf).read().splitlines() if x.strip()]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
50 lENSEMBLTc = []
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
51 with open(inputf) as fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
52 for line in fh:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
53 if line.startswith('#'):
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
54 continue
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
55 lENSEMBLTc.append(line.rstrip('\r\n').split('\t'))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
56 nLines=len(lENSEMBLTc)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
57 cLines=0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
58 sall=[]#the output list for with additional fields
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
59 #~
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
60 while cLines<nLines:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
61 cLines+=1
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
62 lENSEMBLTcKEGGgKEGGpth=lENSEMBLTc.pop(0)
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
63 ENSEMBLTc=lENSEMBLTcKEGGgKEGGpth[posENSEMBLclmn]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
64 try:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
65 KEGGgKEGGpth=dKEGGcPthws[ENSEMBLTc]
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
66 except:
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
67 KEGGgKEGGpth='\t'.join(['U','N'])
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
68 sall.append('\t'.join(['\t'.join(lENSEMBLTcKEGGgKEGGpth),KEGGgKEGGpth]))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
69 #~
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
70 salef=open(output,'w')
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
71 salef.write('\n'.join(sall))
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
72 salef.close()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
73 return 0
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
74
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
75
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
76 if __name__ == '__main__':
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
77 main()
2c498d40ecde Uploaded
miller-lab
parents:
diff changeset
78