view commons/tools/dbBestLength.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env python

## @file
# This program extracts the n longest sequences from the input fasta file.
# usage: dbBestLength.py [ options ]
# options:
#      -h: this help
#      -i: name of the input fasta file
#      -n: maximum number of sequences in the output file (default=20)
#      -o: name of the output fasta file (default=inFileName+'.best20')
#      -v: verbose (default=0/1/2)

import os
import sys
import getopt

if not os.environ.has_key( "REPET_PATH" ):
    print "*** Error: no environment variable REPET_PATH"
    sys.exit(1)
sys.path.append( os.environ["REPET_PATH"] )

from pyRepet.seq.fastaDB import * 


def help():
    """
    Give the list of the command-line options.
    """
    print
    print "usage: dbBestLength.py [ options ]"
    print "options:"
    print "     -h: this help"
    print "     -i: name of the input fasta file"
    print "     -n: maximum number of sequences in the output file (default=20)"
    print "     -o: name of the output fasta file (default=inFileName+'.best20')"
    print "     -v: verbose (default=0/1/2)"
    print


def main():
    """
    This program extracts the n longest sequences from the input fasta file.
    """

    inFileName = ""
    nbSeq = 20
    outFileName = ""
    verbose = 0

    try:
        opts, args = getopt.getopt(sys.argv[1:],"hi:n:o:v:")
    except getopt.GetoptError, err:
        print str(err)
        help()
        sys.exit(1)
    for o,a in opts:
        if o == "-h":
            help()
            sys.exit(0)
        elif o == "-i":
            inFileName = a
        elif o == "-n":
            nbSeq = a
        elif o == "-o":
            outFileName = a
        elif o == "-v":
            verbose = int(a)

    if  inFileName == "":
        print "ERROR: missing input file (-i)"
        help()
        sys.exit(1)

    if verbose > 0:
        print "START dbBestLength.py"
        sys.stdout.flush()

    if outFileName == "":
        outFileName = "%s.best%s" % ( inFileName, nbSeq )

    log = dbBestLength( nbSeq, inFileName, outFileName, verbose )
    if log != 0:
        print "ERROR: dbBestLength() returned %i" % ( log )
        sys.exit(1)

    if verbose > 0:
        print "END dbBestLength.py"
        sys.stdout.flush()

    return 0


if __name__ == "__main__":
    main()