Mercurial > repos > devteam > merge_cols
annotate 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 |
rev | line source |
---|---|
1
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
1 import sys |
0
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 |
11de536c87b1
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff
changeset
|
4 def __main__(): |
1
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
5 if len(sys.argv) < 4: |
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
6 sys.exit('No columns to merge\n') |
0
11de536c87b1
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff
changeset
|
7 else: |
1
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
8 cols = sys.argv[3:] |
0
11de536c87b1
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff
changeset
|
9 |
2
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
10 with open(sys.argv[1], 'r') as infile, open(sys.argv[2], 'w') as outfile: |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
11 skipped_lines = 0 |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
12 for line in infile: |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
13 line = line.rstrip('\r\n') |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
14 if line and not line.startswith('#'): |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
15 fields = line.split('\t') |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
16 line += '\t' |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
17 for col in cols: |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
18 try: |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
19 line += fields[int(col) - 1] |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
20 except Exception: |
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
21 skipped_lines += 1 |
0
11de536c87b1
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff
changeset
|
22 |
2
4576d639d805
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit ed2daf7024ea79a04bbb4a0896b9402ab2daf89f"
devteam
parents:
1
diff
changeset
|
23 outfile.write("{}\n".format(line)) |
1
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
24 |
0
11de536c87b1
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit 5a4e0ca9992af3a6e5ed2b533f04bb82ce761e0b
devteam
parents:
diff
changeset
|
25 if skipped_lines > 0: |
1
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
26 print('Skipped %d invalid lines' % skipped_lines) |
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
27 |
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
28 |
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
29 if __name__ == "__main__": |
26d0d9bdbafc
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/merge_cols commit dc2e3b631f515e905d5645baa4fb49df8e8cc0d5"
devteam
parents:
0
diff
changeset
|
30 __main__() |