-
Notifications
You must be signed in to change notification settings - Fork 42
/
fasta_extract_flanking_regions.py
executable file
·115 lines (90 loc) · 2.96 KB
/
fasta_extract_flanking_regions.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
#!/usr/bin/env python3
"""Extract genomic regions
Usage:
<program> input_genome wanted_file flanking_size output_fasta
Where:
wanted_files contains two tab separated columns with scaffold name and position
(there may be other columns after, as in a bedfile)
"""
# Modules
from collections import defaultdict
import gzip
import sys
# Defining classes
class Fasta(object):
"""Fasta object with name and sequence
"""
def __init__(self, name, sequence):
self.name = name
self.sequence = sequence
def write_to_file(self, handle):
handle.write(">" + self.name + "\n")
handle.write(self.sequence + "\n")
def __repr__(self):
return self.name + " " + self.sequence[0:31]
# Functions
def fasta_iterator(input_file):
"""Takes a fasta file input_file and returns a fasta iterator
"""
with myopen(input_file) as f:
sequence = ""
name = ""
begun = False
for line in f:
line = line.strip()
if line.startswith(">"):
if begun:
yield Fasta(name, sequence)
name = line.replace(">", "")
sequence = ""
begun = True
else:
sequence += line
if name != "":
yield Fasta(name, sequence)
def myopen(infile, mode="rt"):
"""Replacement for `open` function to accept gzip files
Use gzip compression algorithm on files ending with `.gz`
`myopen` can be used like the `open` function because it has the same
behaviour. Namely, it returns a file handle (ie: an opened connection
to a file).
"""
# If filename ends with .gz, open in gzip mode
if infile.endswith(".gz"):
return gzip.open(infile, mode=mode)
# Else open normally
else:
return open(infile, mode=mode)
# Parse user input
try:
input_genome = sys.argv[1]
wanted_file = sys.argv[2]
flanking_size = int(sys.argv[3])
output_fasta = sys.argv[4]
except:
print(__doc__)
sys.exit(1)
# Create wanted set
wanted_regions = defaultdict(list)
with open(wanted_file) as wfile:
for line in wfile:
l = line.strip().split("\t")
scaffold = l[0]
position = l[1]
wanted_regions[scaffold].append(position)
# Extract regions
sequences = fasta_iterator(input_genome)
with open(output_fasta, "w") as outfile:
for scaffold in sequences:
scaffold_id = scaffold.name.split()[0]
if scaffold_id in wanted_regions:
seq = scaffold.sequence
for position in wanted_regions[scaffold_id]:
pos = int(position)
assert pos <= len(seq), "Errof: SNP outside the scaffold"
left = pos - flanking_size
if left < 0:
left = 0
region = Fasta(scaffold_id + "_" + position,
seq[pos - flanking_size: pos + flanking_size].upper())
region.write_to_file(outfile)