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__() |
---|