Mercurial > repos > devteam > merge_cols
comparison mergeCols.py @ 1:26d0d9bdbafc draft
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
author | devteam |
---|---|
date | Wed, 03 Jun 2020 14:22:15 +0000 |
parents | 11de536c87b1 |
children | 4576d639d805 |
comparison
equal
deleted
inserted
replaced
0:11de536c87b1 | 1:26d0d9bdbafc |
---|---|
1 import sys, re | 1 import sys |
2 | 2 |
3 def stop_err( msg ): | |
4 sys.stderr.write( msg ) | |
5 sys.exit() | |
6 | 3 |
7 def __main__(): | 4 def __main__(): |
8 try: | 5 try: |
9 infile = open ( sys.argv[1], 'r') | 6 infile = open(sys.argv[1], 'r') |
10 outfile = open ( sys.argv[2], 'w') | 7 outfile = open(sys.argv[2], 'w') |
11 except: | 8 except Exception: |
12 stop_err( 'Cannot open or create a file\n' ) | 9 sys.exit('Cannot open or create a file\n') |
13 | 10 |
14 if len( sys.argv ) < 4: | 11 if len(sys.argv) < 4: |
15 stop_err( 'No columns to merge' ) | 12 sys.exit('No columns to merge\n') |
16 else: | 13 else: |
17 cols = sys.argv[3:] | 14 cols = sys.argv[3:] |
18 | 15 |
19 skipped_lines = 0 | 16 skipped_lines = 0 |
20 | 17 |
21 for line in infile: | 18 for line in infile: |
22 line = line.rstrip( '\r\n' ) | 19 line = line.rstrip('\r\n') |
23 if line and not line.startswith( '#' ): | 20 if line and not line.startswith('#'): |
24 fields = line.split( '\t' ) | 21 fields = line.split('\t') |
25 line += '\t' | 22 line += '\t' |
26 for col in cols: | 23 for col in cols: |
27 try: | 24 try: |
28 line += fields[ int( col ) -1 ] | 25 line += fields[int(col) - 1] |
29 except: | 26 except Exception: |
30 skipped_lines += 1 | 27 skipped_lines += 1 |
31 | 28 |
32 print >>outfile, line | 29 print(line, file=outfile) |
33 | 30 |
34 if skipped_lines > 0: | 31 if skipped_lines > 0: |
35 print 'Skipped %d invalid lines' % skipped_lines | 32 print('Skipped %d invalid lines' % skipped_lines) |
36 | 33 |
37 if __name__ == "__main__" : __main__() | 34 |
35 if __name__ == "__main__": | |
36 __main__() |