-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
76 lines (58 loc) · 1.88 KB
/
sample.py
File metadata and controls
76 lines (58 loc) · 1.88 KB
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
from pathlib import Path
import numpy as np
BLUB = {
0.1: "uniref50_0",
0.2: "uniref50_10",
0.3: "uniref50_20",
0.4: "uniref50_30",
0.5: "uniref50_40",
0.6: "uniref50_50",
0.7: "uniref50_60",
0.8: "uniref50_70",
0.9: "uniref50_80",
1.0: "uniref50_90",
1.1: "uniref50_100",
}
def parse_fasta(path):
"""
Parse a FASTA file and do some validity checks if requested.
Args:
path: Path to the FASTA file
Returns:
Dictionary mapping sequences IDs to amino acid sequences
"""
print(f"Parsing {path}...")
seq_map = {}
with open(path, "r") as fasta:
for line in fasta.readlines():
line = line.strip()
if len(line) == 0:
continue
if line[0] == '>':
entry_id = line[1:]
seq_map[entry_id] = ''
else:
seq_map[entry_id] += line
return seq_map
def collect_seqs(max_sim):
seqs = dict()
for s in BLUB:
if s <= max_sim:
seqs.update(parse_fasta(f"/wibicomfs/STBS/roman/{BLUB[s]}.fasta"))
return seqs
def sample(max_sim, num_seqs, output_file):
output_file.parent.mkdir(parents=True, exist_ok=True)
seqs = list(collect_seqs(max_sim).items())
with open(output_file, "w") as f:
for pos in np.random.choice(len(seqs), num_seqs, replace=False):
f.write(f"{seqs[pos][1][:1022]}\n")
if __name__ == "__main__":
import sys
if len(sys.argv) != 4 or (len(sys.argv[1]) >= 2 and sys.argv[1] in {"-h", "--help"}):
print("Usage: python sample.py <max_sim> <num_seqs> <output_file>")
sys.exit(0)
max_sim = float(sys.argv[1])
num_seqs = int(sys.argv[2])
output_file = Path(sys.argv[3])
sample(max_sim, num_seqs, output_file)
print(f"Sampled {num_seqs} sequences with maximum similarity {max_sim} to {output_file}.")