view commons/tools/blast2align.py @ 19:9bcfa7936eec

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:23:29 -0400
parents 94ab73e8a190
children
line wrap: on
line source

#!/usr/bin/env python

import sys
import getopt


def help():
    print
    print "usage: blast2align.py [ options ]"
    print "options:"
    print "     -h: this help"
    print "     -i: input file name (format=tabulated BLAST)"
    print "     -o: output file name (format=align, default=inFileName+'.align')"
    print


def blast2align( inFile, outFile ):
    inFileHandler = open( inFile, "r" )
    outFileHandler = open( outFile, "w" )
    while True:
        line = inFileHandler.readline()
        if line == "":
            break
        if line[0] != "#":
            data = line.split("\t")
            qryName = data[0]
            sbjName = data[1]
            percId = data[2]
            qryStart = data[6]
            qryEnd = data[7]
            sbjStart = data[8]
            sbjEnd = data[9]
            Eval = data[10]
            bitScore = data[11][:-1]
            string = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( qryName, qryStart, qryEnd, sbjName, sbjStart, sbjEnd, Eval, bitScore, percId )
            outFileHandler.write( string )
    inFileHandler.close()
    outFileHandler.close()
    
    
def main():
    inFileName = ""
    outFileName = ""

    try:
        opts, args = getopt.getopt(sys.argv[1:],"hi:o:")
    except getopt.GetoptError, err:
        sys.stderr.write( "%s\n" % 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 == "-o":
            outFileName = a
  
    if  inFileName == "":
        msg =  "ERROR: missing input file name (-i)"
        sys.stderr.write( "%s\n" % msg )
        help()
        sys.exit(1)

    if outFileName == "":
        outFileName = inFileName + ".align"

    blast2align( inFileName, outFileName )

    return 0


if __name__ == "__main__":
    main()