Mercurial > repos > miller-lab > genome_diversity
comparison rtrnKEGGpthwfENSEMBLTc.py @ 0:2c498d40ecde
Uploaded
author | miller-lab |
---|---|
date | Mon, 09 Apr 2012 12:03:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2c498d40ecde |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 # | |
4 # calclenchange.py | |
5 # | |
6 # Copyright 2011 Oscar Bedoya-Reina <oscar@niska.bx.psu.edu> | |
7 # | |
8 # This program is free software; you can redistribute it and/or modify | |
9 # it under the terms of the GNU General Public License as published by | |
10 # the Free Software Foundation; either version 2 of the License, or | |
11 # (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program; if not, write to the Free Software | |
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
21 # MA 02110-1301, USA. | |
22 | |
23 import argparse,os,sys | |
24 | |
25 | |
26 def main(): | |
27 parser = argparse.ArgumentParser(description='Adds the fields KEGG gene codes and KEGG pathways to an input table of ENSEMBL transcript codes.') | |
28 parser.add_argument('--loc_file',metavar='correlational database',type=str,help='correlational database') | |
29 parser.add_argument('--species',metavar='species name',type=str,help='the species of interest in loc_file') | |
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' ) | |
31 parser.add_argument('--posENSEMBLclmn',metavar='column number',type=int,help='the column with the ENSEMBLE transcript code') | |
32 parser.add_argument('--input',metavar='input TXT file',type=str,help='the input file with the table in txt format') | |
33 #~ | |
34 #~Open arguments | |
35 class C(object): | |
36 pass | |
37 fulargs=C() | |
38 parser.parse_args(sys.argv[1:],namespace=fulargs) | |
39 #test input vars | |
40 inputf,loc_file,species,output,posENSEMBLclmn=fulargs.input,fulargs.loc_file,fulargs.species,fulargs.output,fulargs.posENSEMBLclmn | |
41 posENSEMBLclmn-=1#correct pos | |
42 #~ Get the extra variables | |
43 crDB=[x.split() for x in open(loc_file).read().splitlines() if x.split()[0]==species][0] | |
44 sppPrefx,dinput=crDB[0],crDB[1]#X should be replaced by the position in which the Conversion Dictionary File (CDF) is placed | |
45 #make a dictionary of the input CDF | |
46 dKEGGcPthws=dict([(x.split('\t')[0],'\t'.join(x.split('\t')[1:])) for x in open(dinput).read().splitlines() if x.strip()]) | |
47 #~ add the two new columns | |
48 sall=[] | |
49 #lENSEMBLTc=[x.split('\t') for x in open(inputf).read().splitlines() if x.strip()] | |
50 lENSEMBLTc = [] | |
51 with open(inputf) as fh: | |
52 for line in fh: | |
53 if line.startswith('#'): | |
54 continue | |
55 lENSEMBLTc.append(line.rstrip('\r\n').split('\t')) | |
56 nLines=len(lENSEMBLTc) | |
57 cLines=0 | |
58 sall=[]#the output list for with additional fields | |
59 #~ | |
60 while cLines<nLines: | |
61 cLines+=1 | |
62 lENSEMBLTcKEGGgKEGGpth=lENSEMBLTc.pop(0) | |
63 ENSEMBLTc=lENSEMBLTcKEGGgKEGGpth[posENSEMBLclmn] | |
64 try: | |
65 KEGGgKEGGpth=dKEGGcPthws[ENSEMBLTc] | |
66 except: | |
67 KEGGgKEGGpth='\t'.join(['U','N']) | |
68 sall.append('\t'.join(['\t'.join(lENSEMBLTcKEGGgKEGGpth),KEGGgKEGGpth])) | |
69 #~ | |
70 salef=open(output,'w') | |
71 salef.write('\n'.join(sall)) | |
72 salef.close() | |
73 return 0 | |
74 | |
75 | |
76 if __name__ == '__main__': | |
77 main() | |
78 |