Mercurial > repos > devteam > merge_cols
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:11de536c87b1 |
---|---|
1 import sys, re | |
2 | |
3 def stop_err( msg ): | |
4 sys.stderr.write( msg ) | |
5 sys.exit() | |
6 | |
7 def __main__(): | |
8 try: | |
9 infile = open ( sys.argv[1], 'r') | |
10 outfile = open ( sys.argv[2], 'w') | |
11 except: | |
12 stop_err( 'Cannot open or create a file\n' ) | |
13 | |
14 if len( sys.argv ) < 4: | |
15 stop_err( 'No columns to merge' ) | |
16 else: | |
17 cols = sys.argv[3:] | |
18 | |
19 skipped_lines = 0 | |
20 | |
21 for line in infile: | |
22 line = line.rstrip( '\r\n' ) | |
23 if line and not line.startswith( '#' ): | |
24 fields = line.split( '\t' ) | |
25 line += '\t' | |
26 for col in cols: | |
27 try: | |
28 line += fields[ int( col ) -1 ] | |
29 except: | |
30 skipped_lines += 1 | |
31 | |
32 print >>outfile, line | |
33 | |
34 if skipped_lines > 0: | |
35 print 'Skipped %d invalid lines' % skipped_lines | |
36 | |
37 if __name__ == "__main__" : __main__() |