comparison append_gene_name.py @ 0:12d69a5e4303 draft

Uploaded
author mandorodriguez
date Thu, 12 May 2016 21:47:50 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:12d69a5e4303
1 #!/usr/bin/env python
2
3 import argparse
4 import os
5 import sys
6 import pdb
7
8
9
10
11
12
13 #-------------------------------------------------------------------------------
14 # Main function call
15 def __main__():
16
17 parser = argparse.ArgumentParser()
18
19 parser.add_argument("-t", "--table", type=str,required=True,
20 help="The table gene name conversion")
21 parser.add_argument("-e", "--ensgene", type=str,required=True,
22 help="Ensegene IDs")
23 parser.add_argument("-o", "--out", type=str,default="table.txt",
24 help="output file")
25
26
27 args = parser.parse_args()
28
29
30 table_file = args.table
31 ensgene_file = args.ensgene
32 outfile = args.out
33
34 ensgene = {}
35 gene_name = {}
36
37 lines = None
38
39 with open(table_file,'r') as tf:
40 lines = tf.readlines()
41
42 for line in lines:
43
44 parts = line.split()
45
46 gene_name[ parts[0] ] = parts[1]
47
48
49
50 #---------------------------------------------------------------------------
51
52 new_ensgene = []
53
54 with open(ensgene_file,'r') as ef:
55 lines = ef.readlines()
56
57 for line in lines:
58
59 parts = line.split()
60
61 if parts[0] == "tracking_id":
62
63 parts.append("gene_name")
64
65 else:
66
67 # check for the gene name in the ensign ids
68 if gene_name.has_key(parts[0]):
69
70 parts.append( gene_name[parts[0]] )
71
72 else:
73
74 parts.append("")
75
76 new_ensgene.append(parts)
77
78
79
80 #--- done getting stuff ----------------------------------------------------
81
82 print "Writing %d lines to output file %s" % (len(new_ensgene),outfile)
83
84 with open(outfile,'w') as of:
85
86 for row in new_ensgene:
87
88 of.write("\t".join(row)+"\n")
89
90
91
92 print "Done!"
93
94 #-------------------------------------------------------------------------------
95 if __name__=="__main__": __main__()