annotate mergeCols.py @ 0:11de536c87b1 draft

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
author devteam
date Mon, 09 Nov 2015 11:45:18 -0500
parents
children 26d0d9bdbafc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
1 import sys, re
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
2
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
3 def stop_err( msg ):
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
4 sys.stderr.write( msg )
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
5 sys.exit()
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
6
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
7 def __main__():
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
8 try:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
9 infile = open ( sys.argv[1], 'r')
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
10 outfile = open ( sys.argv[2], 'w')
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
11 except:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
12 stop_err( 'Cannot open or create a file\n' )
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
13
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
14 if len( sys.argv ) < 4:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
15 stop_err( 'No columns to merge' )
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
16 else:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
17 cols = sys.argv[3:]
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
18
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
19 skipped_lines = 0
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
20
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
21 for line in infile:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
22 line = line.rstrip( '\r\n' )
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
23 if line and not line.startswith( '#' ):
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
24 fields = line.split( '\t' )
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
25 line += '\t'
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
26 for col in cols:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
27 try:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
28 line += fields[ int( col ) -1 ]
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
29 except:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
30 skipped_lines += 1
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
31
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
32 print >>outfile, line
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
33
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
34 if skipped_lines > 0:
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
35 print 'Skipped %d invalid lines' % skipped_lines
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
36
11de536c87b1 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff changeset
37 if __name__ == "__main__" : __main__()