view extract_GFF_Features.py @ 0:696e702ebf74 draft

"planemo upload commit 0f6eca49bafc3c946189d793161a7f81d595e1a1-dirty"
author petr-novak
date Mon, 09 May 2022 08:26:30 +0000
parents
children
line wrap: on
line source

#!/usr/bin/env python
# Guruprasad Ananda
"""
Extract features from GFF file.

usage: %prog input1 out_file1 column features
"""
from __future__ import print_function

import sys

from bx.cookbook import doc_optparse


def stop_err(msg):
    sys.stderr.write(msg)
    sys.exit()


def main():
    # Parsing Command Line here
    options, args = doc_optparse.parse(__doc__)

    try:
        inp_file, out_file, column, features = args
    except ValueError:
        stop_err("One or more arguments is missing or invalid.\nUsage: prog input output column features")
    try:
        column = int(column)
    except ValueError:
        stop_err("Column %s is an invalid column." % column)

    if features is None:
        stop_err("Column %d has no features to display, select another column." % (column + 1))

    fo = open(out_file, "w")
    for line in open(inp_file):
        line = line.rstrip("\r\n")
        if line and line.startswith("#"):
            # Keep valid comment lines in the output
            fo.write("%s\n" % line)
        else:
            try:
                if line.split("\t")[column] in features.split(","):
                    fo.write("%s\n" % line)
            except Exception:
                pass
    fo.close()

    print("Column %d features: %s" % (column + 1, features))


if __name__ == "__main__":
    main()