-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcut_alignment.py
32 lines (30 loc) · 1.44 KB
/
cut_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
import argparse
from Bio import AlignIO
def main(alignment_in, alignment_out, regions, format):
alignment = AlignIO.read(alignment_in, format)
fields = regions.split(',')
alignment_regions = []
for field in fields:
if ':' in field:
start,stop = map(int, field.split(':'))
start = start-1 #Input index starts at 1 instead of 0
alignment_region = alignment[:, start:stop]
else:
start = int(field)-1
stop = int(field)
alignment_region = alignment[:, start:stop]
alignment_regions.append(alignment_region)
#paste the regions together
merged_alignment = alignment_regions[0]
for align in alignment_regions[1:]:
for i, record in enumerate(align):
merged_alignment[i].seq+=record.seq
AlignIO.write(merged_alignment, alignment_out, format)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("alignment_in", type=str, help="Input alignment")
parser.add_argument("alignment_out", type=str, help="Output alignment")
parser.add_argument("regions", type=str, help="Which region(s) to cut. If multiple, separate by a comma. E.g. 1:8,10:12,21.")
parser.add_argument("--format", type=str, default="fasta", help="Format of the alignment (Default: fasta)")
args = parser.parse_args()
main(args.alignment_in, args.alignment_out, args.regions, args.format)