#! /usr/bin/env python import os, sys, string, cp, SeqIO def main(): """ %(filename)s Converts fasta format sequence alignment to phylip2 (sequential) format. Mercilessly truncates sequence names to 10 characters. {{out Name of output file out.phylip2}} {{re Renames sequences s1, s2 .... sN. Useful for long sequence names}} {{h Prints documentation.}} {{v verbosity 1}} {{range Restricts the set of sequences written to the output file to a range.\ For example, use -range=5,27 to use the fifth to 27th sequences \ in the input file.}} {{s superclean. Changes all characters found in either names or \ sequence not in 'ACGTN\\n -1234567890S' to N.}} {{version print version info and exit}} %(version)s """ debug = 0 docstringdict = {'filename':os.path.split(sys.argv[0])[-1], 'version':'$Id: fasta2phylip.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 ) v = dict.value('v') superclean = dict.status('s') outfilename = dict.value('out') rename = dict.status('re') if dict.status('range'): range = dict.value('range').split(',') if len(range) != 2: sys.exit('The range was not specified properly') # convert to 0-index first = eval(range[0]) - 1 last = eval(range[1]) inlist = dict.value('infile_list','in','file') if v > 1: print 'Reading sequences from', inlist fasta = SeqIO.readFastaList(inlist) if v> 1: print 'A total of %s sequences were read.' % len(fasta) if v>0: print 'Writing %s ...' % outfilename outfile = open(outfilename,'w') if dict.status('range'): print 'Using sequences %s to %s.' % (range[0],range[1]) outStr = SeqIO.writePhylip( fasta[first:last], rename=rename, superclean=superclean, v=0 ) else: outStr = SeqIO.writePhylip( fasta, rename=rename, superclean=superclean, v=0 ) outfile.write(outStr) outfile.close() main()