#!/usr/bin/env python3

import os
import shutil
import subprocess
import sys

# GBfilter  6/25/01
# Synopsis:  GBfilter infile gbfile
# Description: If infile is GenBank, simply write it to gbfile, else
# filter it through readseq to generate a GenBank file. It is not
# possible to filter GenBank files through readseq, because readseq
# removes most of the annotation.

""" ensure that there are enough command line arguments to parse """
if len(sys.argv) < 2:
    print("Usage: GBfilter.py  infile  gbfile");
    exit();

infile = sys.argv[1]
gbfile = sys.argv[2]

call_readseq = True

if os.path.exists(infile):
    infile_h = open(infile, 'r')
    lines = infile_h.readlines()
    infile_h.close()

    for line in lines:
        if line.startswith('LOCUS'):
            call_readseq = False
            break

    if call_readseq:
        subprocess.call(['readseq', '-a', '-f2', '-o=' + gbfile, infile])
    else:
        shutil.copyfile(infile, gbfile)
else:
    print "ERROR: file not found!"
