#! /usr/bin/env python import sys, os, string, SeqIO, cp def main(): """ %(filename)s Reads names in a tfa file. Prints to stdout by default. {{h Prints documentation.}} {{out name of outfile}} {{len include length of sequence after name}} {{c force upper or lower case output u l}} {{fname include filename. Only works if a single file is used as input.}} {{version print version info and exit}} %(version)s """ debug = 0 docstringdict = {'filename':os.path.split(sys.argv[0])[-1], 'version':'$Id: fastanames.py,v 1.1 2004/10/10 02:53:10 nghoffma Exp $'} optlist, formattedDocString, formattedSummary = cp.optStringParser(main.__doc__ % docstringdict, optWidth=15, lineWidth=60, valOffset = 4, putVals=1) dict = cp.commandparser(options=optlist, usage='Options:\n' + formattedSummary, debug = debug, exitWithUsage=1) if dict.status('version'): sys.exit( 'Version: %(version)s\n' % docstringdict ) if dict.status('h'): print formattedDocString sys.exit( 0 ) printLen = dict.status('len') forceCase = 0 if dict.status('c'): forceCase = 1 if dict.value('c') == 'u': casefun = string.upper elif dict.value('c') == 'l': casefun = string.lower #read from a list of fasta files fasta_list = dict.value('infile_list', 'in','file') fasta = SeqIO.readFastaList(fasta_list, output='list') if dict.status('out'): outfile = dict.value('out','out','file') outfile = sys.stdout writename = 0 if dict.status('fname') and len(fasta_list) == 1 and fasta_list[0].name != '': writename = fasta_list[0].name for seq in fasta: outStr = seq.getName() if printLen: outStr += '\t' + `len(seq)` if writename: outStr = writename + '\t' + outStr if forceCase: outStr = casefun(outStr) outfile.write(outStr + '\n') main()