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