annotate transpose.py @ 3:5d5bdc4c409b draft default tip

Uploaded
author jmsong
date Tue, 18 Mar 2014 11:38:24 -0400
parents fc1b758149c7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
1 #!/usr/bin/env python
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
2 #By: Clayton Turner
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
3
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
4 #imports
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
5 import sys
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
6 import csv
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
7
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
8 # error
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
9 def stop_err( msg ):
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
10 sys.stderr.write( "%s\n" % msg )
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
11 sys.exit()
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
12
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
13 # main
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
14 def main():
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
15 try:
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
16 # retrieve file locations/names
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
17 inputFile = sys.argv[1]
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
18 output = sys.argv[2]
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
19
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
20 # open input file
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
21 itemList = list()
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
22 with open(inputFile) as infile:
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
23 for line in infile:
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
24 items = line.strip('\n').split('\t')
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
25 itemList.append(items)
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
26 rows = zip(*itemList)
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
27
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
28 infile.close()
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
29
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
30 # open output file
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
31 outfile = open(output,'w')
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
32 writer = csv.writer(outfile, delimiter='\t')
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
33
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
34 # append data to output file
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
35 for row in rows:
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
36 writer.writerow(row)
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
37
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
38 # close output file
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
39 outfile.close()
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
40
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
41 except Exception, ex:
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
42 stop_err('Error running transpose.py\n' + str(ex))
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
43
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
44 # exit
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
45 sys.exit(0)
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
46
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
47 if __name__ == "__main__":
fc1b758149c7 Uploaded
jmsong
parents:
diff changeset
48 main()