-
Notifications
You must be signed in to change notification settings - Fork 18
/
x2y.py
executable file
·150 lines (112 loc) · 4.23 KB
/
x2y.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
"""
Simple script that uses Biopython SeqIO to convert
between various file formats for sequence data.
See documentation online at:
http://biopython.org/wiki/SeqIO
"""
# 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.0"
__title__ = "x2y"
__license__ = "GPLv3"
__author_email__ = "[email protected]"
def parseArgs():
"""Parse command line arguments"""
import argparse
try:
parser = argparse.ArgumentParser(
description='Convert between sequence file types supported by BioPython')
parser.add_argument('-i',
'--infile',
action='store',
default='None',
help='The input sequence file 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.')
except:
print("An exception occurred with argument parsing. Check your provided options.")
traceback.print_exc()
return parser.parse_args()
def convert(infile, type, outtype, outfile):
"""Make SeqIO call to convert using the specified parameters"""
from Bio import SeqIO
ifh = SeqIO.parse(infile, type)
SeqIO.write(ifh, outfile, outtype)
def getOutfile(infile, outtype):
"""Synthesise a default output name if none was provided"""
# Return an output file with the input file name and extension, but with " appended.
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: print("Extension is " + extension)
# Figure out what extension to return
if extension in (".abi",".ab1"):
type = "abi"
elif extension in (".embl"):
type = "embl"
elif extension in (".clust", ".cw", ".clustal"):
type = "clustal"
elif extension in (".fa", ".fasta", ".fas", ".fna", ".faa", ".afasta"):
type = "fasta"
elif extension in (".fastq", ".fq"):
type = "fastq"
elif extension in (".gbk", ".genbank", ".gb"):
type = "genbank"
elif extension in (".paup", ".nexus"):
type = nexus
else:
print("Couldn't determine the file type from the extension. Reattempt with the -j|--intype option specified.")
sys.exit(1)
if verbose > 0: print("Your file looks like a " + type)
return type
def main():
args = parseArgs()
infile = args.infile
type = args.intype
outfile = args.outfile
outtype = args.outtype
verbose = args.verbose
if verbose > 1: print(args)
if type is 'None':
type = guessExt(infile, verbose)
if outfile is 'None':
outfile = getOutfile(infile,outtype)
if verbose > 0:
print("Converting " + infile + " of type: " + type + ", to " + outfile + " of type: " + outtype)
if infile is not 'None':
convert(infile, type, outtype, outfile)
else:
print("No input file was provided. Check the options you provided.")
sys.exit(1)
if __name__ == '__main__':
main()