def main(argv): usage = 'ConvertAln -i <infile> -x <informat> -o <outfile> -f <outformat>' infile = '' informat = '' outfile = '' outformat = '' try: opts, args = getopt.getopt(argv,"hi:x:o:f:",["infile=", "informat=", "outfile=", "outformat="]) except getopt.GetoptError: sys.exit(usage) for opt, arg in opts: if opt == '-h': print usage sys.exit() elif opt in ("-i", "--infile"): infile = arg elif opt in ("-x", "--informat"): informat = arg elif opt in ("-o", "--outfile"): outfile = arg elif opt in ("-f", "--outformat"): outformat = arg if not infile: sys.exit("must specify infile! %s" % usage) if not outformat: sys.exit("must specify format to convert to! %s" % usage) if not informat: informat = guess_format(infile) if not outfile: if '.' in infile: outfile = '.'.join((infile.split('.')[:-1] + [get_extension(outformat)])) else: outfile = '.'.join((infile, get_extension(outformat))) if infile == 'pipe' or infile == 'stdin' or infile == 'STDIN' or infile == '|': infile = sys.stdin if outformat == 'phylip': alignment=AlignIO.read(infile, informat, alphabet=IUPAC.ambiguous_dna) alignment = remove_blank(alignment) if len(alignment) == 0 or len(alignment[0]) == 0: sys.exit() if outfile == 'pipe' or outfile == 'stdout' or outfile == 'STDOUT' or outfile == '|' or outfile == '>': write_phylip(alignment, sys.stdout) else: out_fh = open(outfile, 'w') write_phylip(alignment, out_fh) out_fh.close() else: if outfile == 'pipe' or outfile == 'stdout' or outfile == 'STDOUT' or outfile == '|' or outfile == '>': outfile = sys.stdout if outformat == 'nexus': alignment=AlignIO.read(infile, informat, alphabet=IUPAC.ambiguous_dna) write_nexus(alignment, outfile) else: AlignIO.convert(infile, informat, outfile, outformat, alphabet=IUPAC.ambiguous_dna)
def test_write_phylip(): output = StringIO() write_phylip(align1, output) results = output.getvalue().split('\n') output.close() assert results[0] == '3 12' assert results[1] == 'Alpha ACTGCTAGCTAG' assert results[2] == 'Beta ACT-CTAGC.AG' assert results[3] == 'Gamma .....A......'
def test_write_phylip2(): """ if writer deals with long ids correctly""" align1[0].id = 'very_long_name' output = StringIO() write_phylip(align1, output) results = output.getvalue().split('\n') output.close() assert results[0] == '3 12' assert results[1] == 'very_long_name ACTGCTAGCTAG' assert results[2] == 'Beta ACT-CTAGC.AG' assert results[3] == 'Gamma .....A......'