def main(): # Set defaults verbose = 0 gapin = "-.~" gapout = "-" outfile = "" Idna = "auto" Ipep = "auto" outform = "fasta" matchmet = "trans" mtx_file = "" mtx = None allinternal = False readthroughstop = False # Quick sanity check if len(sys.argv) < 3: print __doc__ sys.exit(1) # Process arguments dnafile = "" pepfile = "" argv = sys.argv[1:] while (len(argv) > 0): arg = argv[0] if arg == "-h": print __doc__ sys.exit(0) if arg == "-v": verbose = 1 if arg == "-vv": verbose = 2 if arg == "-vvv": verbose = 3 if arg == "-match": if len(argv) == 0: argerr("-match") matchmet = argv[1] argv = argv[2:] continue if arg == "-gapin": if len(argv) == 0: argerr("-gapin") gapin = argv[1] argv = argv[2:] continue if arg == "-gapout": if len(argv) == 0: argerr("-gapout") gapout = argv[1][0] # Use only the first char argv = argv[2:] continue if arg == "-Idna": if len(argv) == 0: argerr("-Idna") Idna = (argv[1]).lower() argv = argv[2:] continue if arg == "-Ipep": if len(argv) == 0: argerr("-Ipep") Ipep = (argv[1]).lower() argv = argv[2:] continue if arg == "-O": if len(argv) == 0: argerr("-O") outform = (argv[1]).lower() argv = argv[2:] continue if arg == "-mtx": if len(argv) == 0: argerr("-mtx") mtx_file = argv[1] argv = argv[2:] continue if arg == "-allinternal": allinternal = True if arg == "-readthroughstop": readthroughstop = True if arg[0] != "-": if dnafile == "": dnafile = arg elif pepfile == "": pepfile = arg else: outfile = arg argv = argv[1:] # Output extra info if requested if verbose: warn("verbose level: " + str(verbose)) warn("dnafile: " + dnafile + " [format:" + Idna + "]") warn("pepfile: " + pepfile + " [format:" + Ipep + "]") if outfile: warn("outfile: " + outfile) else: warn("outfile: None - writing to STDOUT") warn("out format: " + outform) warn("Mtxfile: " + mtx_file) # Read input files try: if Idna == "auto": Idna = mod_seqfiles.autotype(dnafile) if verbose: warn("DNA file format appears to be......: " + Idna) dnaseqs = mod_seqfiles.readfile(dnafile, Idna) if verbose: warn("#DNA entries read: " + str(len(dnaseqs))) if Ipep == "auto": Ipep = mod_seqfiles.autotype(pepfile) if verbose: warn("Peptide file format appears to be..: " + Ipep) pepseqs = mod_seqfiles.readfile(pepfile, Ipep) if verbose: warn("#pep entries read: " + str(len(pepseqs))) #if 1: if len(dnaseqs) == 0 or len(pepseqs) == 0: warn("Error: Bad input.") warn("Dna sequences read: " + str(len(dnaseqs))) warn("peptide sequence read: " + str(len(pepseqs))) sys.exit(1) if mtx_file: try: mtx = mod_translate.parseMatrixFile(mtx_file) except: warn("Invalid translation matrix: " + mtx_file) sys.exit(1) except Exception, msg: warn("Error reading input files: " + str(msg)) warn("DNA File type: " + Idna) warn("Pep File type: " + Ipep) sys.exit(1)
def main(): # Set defaults verbose = 0 gapin = "-.~" gapout = "-" outfile = "" Idna = "auto" Ipep = "auto" outform = "fasta" matchmet = "trans" mtx_file = "" mtx = None allinternal = False readthroughstop = False # Quick sanity check if len(sys.argv)<3: print __doc__ sys.exit(1) # Process arguments dnafile = "" pepfile = "" argv = sys.argv[1:] while (len(argv)>0): arg = argv[0] if arg == "-h" : print __doc__ sys.exit(0) if arg == "-v" : verbose = 1 if arg == "-vv" : verbose = 2 if arg == "-vvv" : verbose = 3 if arg == "-match" : if len(argv) == 0: argerr("-match") matchmet = argv[1] argv = argv[2:] continue if arg == "-gapin" : if len(argv) == 0: argerr("-gapin") gapin = argv[1] argv = argv[2:] continue if arg == "-gapout" : if len(argv) == 0: argerr("-gapout") gapout = argv[1][0] # Use only the first char argv = argv[2:] continue if arg == "-Idna" : if len(argv) == 0: argerr("-Idna") Idna = (argv[1]).lower() argv = argv[2:] continue if arg == "-Ipep" : if len(argv) == 0: argerr("-Ipep") Ipep = (argv[1]).lower() argv = argv[2:] continue if arg == "-O" : if len(argv) == 0: argerr("-O") outform = (argv[1]).lower() argv = argv[2:] continue if arg == "-mtx" : if len(argv) == 0: argerr("-mtx") mtx_file = argv[1] argv = argv[2:] continue if arg == "-allinternal": allinternal = True if arg == "-readthroughstop": readthroughstop = True if arg[0] != "-": if dnafile == "" : dnafile = arg elif pepfile == "" : pepfile = arg else : outfile = arg argv = argv[1:] # Output extra info if requested if verbose: warn("verbose level: "+str(verbose)) warn("dnafile: "+dnafile+" [format:"+Idna+"]") warn("pepfile: "+pepfile+" [format:"+Ipep+"]") if outfile : warn("outfile: "+outfile) else : warn("outfile: None - writing to STDOUT") warn("out format: "+outform) warn("Mtxfile: "+mtx_file) # Read input files try: if Idna == "auto": Idna = mod_seqfiles.autotype(dnafile) if verbose: warn("DNA file format appears to be......: "+Idna) dnaseqs = mod_seqfiles.readfile(dnafile,Idna) if verbose: warn("#DNA entries read: "+str(len(dnaseqs))) if Ipep == "auto": Ipep = mod_seqfiles.autotype(pepfile) if verbose: warn("Peptide file format appears to be..: "+Ipep) pepseqs = mod_seqfiles.readfile(pepfile,Ipep) if verbose: warn("#pep entries read: "+str(len(pepseqs))) #if 1: if len(dnaseqs) == 0 or len(pepseqs) == 0: warn("Error: Bad input.") warn("Dna sequences read: "+str(len(dnaseqs))) warn("peptide sequence read: "+str(len(pepseqs))) sys.exit(1) if mtx_file: try: mtx = mod_translate.parseMatrixFile(mtx_file) except: warn("Invalid translation matrix: "+mtx_file) sys.exit(1) except Exception,msg: warn("Error reading input files: "+str(msg)) warn("DNA File type: "+Idna) warn("Pep File type: "+Ipep) sys.exit(1)
if not opt.outformat in ["AUTO","TAB","FASTA","REPORT"]: print >> sys.stderr("Invalid output format [%s]\n" % opt.outformat) sys.exit(-1) # Check mutually exclusive options if opt.intronrf: opt.exonann=False return (opt, args) if __name__ == "__main__": reports = [] pepseqs = [] opt, args = parseOpts() # Initialize translation matrix mtx = mod_translate.parseMatrixFile(opt.matrix) if not mtx: sys.stderr.write("Cannot initialize matrix [%s]\n" % matrix) sys.exit(-1) # Start the translation report reports.append( [REPORTHEADER,"Translation table: %s\n\n" % mtx.description] ) # Read input data if not args: sys.stderr.write("Reading from STDIN. -h for help\n") lines = sys.stdin.readlines() else: lines = [] for fn in args: try:
if arg == "--readthroughstop": readThroughStop = True continue if arg == "--allinternal": firstIsStart = False continue if arg == "-mtx": mtxfn = argv[0] argv = argv[1:] continue fn = arg mtx = mod_translate.parseMatrixFile(mtxfn) try: seqs = mod_seqfiles.readfileauto(fn) newseqs = {} if not maxseqs: maxseqs = len(seqs.keys()) for key in seqs.keys()[0:maxseqs]: seq, note = seqs[key] newseqs[key] = mod_translate.translate(seq, mtx, firstIsStart, readThroughStop), note mod_seqfiles.writestream(sys.stdout, newseqs, "fasta", "P") except Exception, e: sys.stderr.write("Translation error: %s\n" % str(e))
if arg == "--readthroughstop": readThroughStop = True continue if arg == "--allinternal": firstIsStart = False continue if arg == "-mtx": mtxfn = argv[0] argv = argv[1:] continue fn = arg mtx = mod_translate.parseMatrixFile(mtxfn) try: seqs = mod_seqfiles.readfileauto(fn) newseqs = {} if not maxseqs: maxseqs = len(seqs.keys()) for key in seqs.keys()[0:maxseqs]: seq, note = seqs[key] newseqs[key] = mod_translate.translate(seq,mtx,firstIsStart,readThroughStop) , note mod_seqfiles.writestream(sys.stdout,newseqs,"fasta","P") except Exception, e: sys.stderr.write("Translation error: %s\n" % str(e)) sys.exit(1)