/
run_alignment.py
79 lines (58 loc) · 2.25 KB
/
run_alignment.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
import sys
import os
from argparse import (ArgumentParser, FileType)
# ============================================================================ #
# PROGRAM USAGE #
# ============================================================================ #
_ustr = """
alignment.py
%s [options]
Input files: - fasta_file_1
- fasta_file_2
Example:
%s --input <path-to-file>/fasta1 <path-to-file>/fasta2 --module <path-to-module>/alignment.py
""" % (sys.argv[0], sys.argv[0])
def Parse_Commandline():
"Parse the input arguments, use '-h' for help"
global _args
parser = ArgumentParser(usage=_ustr, description='pileup2fasta.py')
parser.add_argument(
'--input', nargs=2, type=str, required=True,
help='Input mpileup file')
parser.add_argument(
'--modulepath', type=str, required=True,
help='Input path to module alignment.py')
parser.add_argument(
'--verbose', type=int, default=0, required=False, help='Verbosity')
if (len(sys.argv) <= 1):
parser.print_help()
sys.exit()
try:
_args = parser.parse_args()
except:
sys.stderr.write('Error: Failed to parse command-line arguments')
sys.exit(1)
# ============================================================================ #
# IMPORT MODULES #
# ============================================================================ #
Parse_Commandline()
module = _args.modulepath
module_folder = os.path.dirname(module)
if module_folder not in sys.path:
sys.path.append(module_folder)
import alignment
from Bio import SeqIO
# ============================================================================ #
# MAIN PROGRAM #
# ============================================================================ #
handle1 = open(_args.input[0], "rU")
records1 = {}
for record in SeqIO.parse(handle1, "fasta"):
records1[record.id] = record.seq
handle2 = open(_args.input[1], "rU")
records2 = {}
for record in SeqIO.parse(handle2, "fasta"):
records2[record.id]=record.seq
for allele in records1.keys():
print allele
alignment.needle(records1[allele], records2[allele])