view extract_GFF_Features.py @ 3:1069776f7ae2 draft default tip

planemo upload for repository https://github.com/kavonrtep/galaxy_packages commit 3b9f93ed06cc32dbfa271789739e7a1e8fac528c
author petr-novak
date Tue, 30 Apr 2024 08:27:27 +0000
parents 696e702ebf74
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()