-
Notifications
You must be signed in to change notification settings - Fork 18
/
x2phy.py
executable file
·135 lines (107 loc) · 4.42 KB
/
x2phy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
"""
Simple script that uses Biopython AlignIO to convert
between various file formats for sequence alignments.
See documentation online at:
http://biopython.org/wiki/AlignIO
"""
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import traceback
import os
import sys
import warnings
__author__ = "Joe R. J. Healey"
__version__ = "1.0.1"
__title__ = "x2phy"
__license__ = "GPLv3"
__author_email__ = "jrj.healey@gmail.com"
def parseArgs():
"""Parse command line arguments"""
import argparse
try:
parser = argparse.ArgumentParser(description='Convert between alignment file types supported by BioPython')
parser.add_argument('-i',
'--infile',
action='store',
default='None',
help='The input phylogeny to convert.')
parser.add_argument('-j',
'--intype',
action='store',
default='None',
help='Specify the input file type. Script will try to guess from the extension if it is standard.')
parser.add_argument('-o',
'--outfile',
action='store',
default='None',
help='The output file name (default is infile with new extension.)')
parser.add_argument('-p',
'--outtype',
action='store',
help='The output file type.')
parser.add_argument('-v',
'--verbose',
action='count',
default=0,
help='Verbose behaviour, emit messages about progress/debugging etc.')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
exit(1)
except NameError:
sys.stderr.write("An exception occurred with argument parsing. Check your provided options.")
traceback.print_exc()
return parser.parse_args()
def convert(infile, type, outtype, outfile):
"""Make AlignIO call to convert using the specified parameters"""
from Bio import AlignIO
ifh = AlignIO.parse(infile, type)
AlignIO.write(ifh, outfile, outtype)
def getOutfile(infile, outtype):
"""Synthesise a default output name if none was provided"""
return os.path.splitext(infile)[0] + "." + outtype
def guessExt(infile, verbose):
"""If no input type was specified, guess it from the extension name or emit a warning"""
extension = os.path.splitext(infile)[1]
if verbose > 0:
sys.stderr.write("Extension is " + extension)
# Figure out what extension to return
if extension in (".clust", ".clustal",".aln"):
type = "clustal"
elif extension in (".emb", ".emboss"):
type = "emboss"
elif extension in (".fa", ".fasta", ".fas", ".fna", ".faa", ".afasta"):
type = "fasta"
elif extension in (".phy", ".phylip"):
type = "phylip"
elif extension in (".nexus", ".paup"):
type = "nexus"
else:
sys.stderr.write("Couldn't determine the file type from the extension. Reattempt with the -j|--intype option specified.")
sys.exit(1)
if verbose > 0:
sys.stderr.write("Your file looks like a " + type)
return type
def main():
args = parseArgs()
if args.verbose > 1:
print(args)
if args.intype is 'None':
args.intype = guessExt(args.infile, args.verbose)
if args.outfile is 'None':
args.outfile = getOutfile(args.infile, args.outtype)
if args.verbose > 0:
sys.stderr.write("Converting " + args.infile + " of type: " + args.intype + ", to " + args.outfile + " of type: " + args.outtype)
if args.infile is not 'None':
convert(args.infile, args.intype, args.outtype, args.outfile)
else:
sys.stderr.write("No input file was provided. Check the options you provided.")
sys.exit(1)
if __name__ == '__main__':
main()