annotate append_gene_name.py @ 0:12d69a5e4303 draft

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