forked from enormandeau/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfasta_complexity_by_sliding_windows.py
executable file
·146 lines (113 loc) · 4.36 KB
/
fasta_complexity_by_sliding_windows.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
#!/usr/bin/env python3
"""Get sequence complexity by sliding-window for whole genomes
Sequence complexity is measured as the ratio of the length of a gzip-compressed
sequence of window_size over its non-compressed length. Reported values range
between 0 (infinite compression) to 1 (maximum sequence entropy). Lower values
thus represent regions of low complexity.
Results are written to the output_file and a different version is also printed
on screen. Namely, the delimitations between chromosomes or scaffolds are
reported with "#####" on the screen but with lines starting by "Twix" in the
output_file.
Usage:
<program> input_genome window_size min_scaf_size output_file
Where:
input_genome is a fasta or fasta.gz file with chromosomes or scaffolds
window_size is the size of the windows to evaluate complexity in
min_scaf_size is the minimum size of scaffold to be considered
output_file is the name of a file for the tabulation-separated output
"""
# Modules
from random import choice
import gzip
import sys
# Defining functions
def myopen(_file, mode="rt"):
if _file.endswith(".gz"):
return gzip.open(_file, mode=mode)
else:
return open(_file, mode=mode)
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, "".join(sequence))
name = line[1:].split(" ")[0]
sequence = ""
begun = True
else:
sequence += line
if name != "":
yield Fasta(name, "".join(sequence))
# 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[:31]
def compression_ratio(seq, maximum_ratio=1.0):
return len(gzip.compress(seq.upper().encode())) / (len(seq) * maximum_ratio)
# Parse user input
try:
input_file = sys.argv[1]
window_size = int(sys.argv[2])
min_scaf_size = int(sys.argv[3])
output_file = sys.argv[4]
except:
print(__doc__)
sys.exit(1)
# Normalize output by maximum
# Generate 20 random sequences of window_size and find maximum_ratio
print(f"Calibrating maximum entropy for windows of {window_size}bp")
maximum_ratio = 0.0
for i in range(20):
random_sequence = "".join(choice("ACGT") for _ in range(window_size))
ratio = compression_ratio(random_sequence)
print(ratio, end=" ", flush=True)
maximum_ratio = max(ratio, maximum_ratio)
print()
# Extract complexity by sliding window
tot_pos = 0
sequences = fasta_iterator(input_file)
with myopen(output_file, "wt") as outfile:
# If scaffold doesn't have at least 2 full windows, skip
for s in sequences:
if len(s.sequence) < (2 * window_size) or len(s.sequence) < min_scaf_size:
continue
pos = 0
while len(s.sequence) > (1.5 * window_size):
# Get next window
window, s.sequence = s.sequence[: window_size], s.sequence[window_size: ]
# Write stats to output
line = "\t".join([s.name, str(pos), str(tot_pos),
str(round(compression_ratio(window, maximum_ratio), 6))])
outfile.write(line + "\n")
outfile.flush()
# Upgrade positions
pos += window_size
tot_pos += window_size
# Report result on stdin
print(line)
# Delimiter between chromosomes
# Twix, definition:
# (a) With ref. to position or location in space: among (several
# animals); in among (surrounding objects); ~ hondes, in (one's) hands;
# (b) with ref. to association or relationship: between two
# (parties); also, among (parties) [quot. a1400, last];
tot_pos -= window_size
outfile.write(f"Twix\t{pos}\t{tot_pos}\t0\n")
tot_pos += window_size * 5
outfile.write(f"Twix\t{pos}\t{tot_pos}\t0\n")
print("#####")