view mergeCols.py @ 2:dd40b1e9eebe 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 10:22:39 -0400
parents 28ca7552e884
children ae7843d06f8f
line wrap: on
line source

import sys


def __main__():
    try:
        infile = open(sys.argv[1], 'r')
        outfile = open(sys.argv[2], 'w')
    except Exception:
        sys.exit('Cannot open or create a file\n')

    if len(sys.argv) < 4:
        sys.exit('No columns to merge\n')
    else:
        cols = sys.argv[3:]

    skipped_lines = 0

    for line in infile:
        line = line.rstrip('\r\n')
        if line and not line.startswith('#'):
            fields = line.split('\t')
            line += '\t'
            for col in cols:
                try:
                    line += fields[int(col) - 1]
                except Exception:
                    skipped_lines += 1

            print(line, file=outfile)

    if skipped_lines > 0:
        print('Skipped %d invalid lines' % skipped_lines)


if __name__ == "__main__":
    __main__()