-
Notifications
You must be signed in to change notification settings - Fork 2
/
splits_helper_functions.py
265 lines (214 loc) · 8.16 KB
/
splits_helper_functions.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import os
import re
import argparse
import numpy as np
from sklearn.model_selection import KFold
import pandas as pd
from Bio import SeqIO
from os.path import join, exists, abspath, isdir, dirname
import subprocess
def remove_header_gaps(folder, infile, outfile):
'''
CD-HIT truncates fasta record headers at whitespace,
need to remove these before I run the algorithm
'''
if not exists(outfile):
with open(join(folder, outfile), 'w') as f:
for record in SeqIO.parse(join(folder, infile), 'fasta'):
header = record.description.replace(' ', '_')
seq = str(record.seq)
f.write('>%s\n%s\n' % (header, seq))
def run_cd_hit(infile, outfile, cutoff, memory):
'''
Run a specific cd-hit command
'''
# get the right word size for the cutoff
if cutoff < 0.5:
word = 2
elif cutoff < 0.6:
word = 3
elif cutoff < 0.7:
word = 4
else:
word = 5
mycmd = '%s -i %s -o %s -c %s -n %s -T 1 -M %s -d 0' % ('cd-hit', infile, outfile, cutoff, word, memory)
print(mycmd)
process = subprocess.Popen(mycmd, shell=True, stdout=subprocess.PIPE)
process.wait()
def cluster_all_levels(start_folder, cluster_folder, filename):
'''
Run cd-hit on fasta file to cluster on all levels
'''
mem = 2000 # memory in mb
cutoff = 1.0
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(start_folder, '%s.fasta' % filename), outfile=outfile, cutoff=cutoff, memory=mem)
cutoff = 0.9
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(cluster_folder, '%s_clustered_sequences_100.fasta' % filename), outfile=outfile,
cutoff=cutoff, memory=mem)
cutoff = 0.8
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(cluster_folder, '%s_clustered_sequences_90.fasta' % filename), outfile=outfile,
cutoff=cutoff, memory=mem)
cutoff = 0.7
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(cluster_folder, '%s_clustered_sequences_80.fasta' % filename), outfile=outfile,
cutoff=cutoff, memory=mem)
cutoff = 0.6
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(cluster_folder, '%s_clustered_sequences_70.fasta' % filename), outfile=outfile,
cutoff=cutoff, memory=mem)
cutoff = 0.5
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(cluster_folder, '%s_clustered_sequences_60.fasta' % filename), outfile=outfile,
cutoff=cutoff, memory=mem)
cutoff = 0.4
outfile = join(cluster_folder, '%s_clustered_sequences_%s.fasta' % (filename, str(int(cutoff * 100))))
if not exists(outfile):
run_cd_hit(infile=join(cluster_folder, '%s_clustered_sequences_50.fasta' % filename), outfile=outfile,
cutoff=cutoff, memory=mem)
def parse_cd_hit(path_to_clstr):
'''
Gather the clusters of CD-hit output `path_to_clust` into a dict.
'''
# setup regular expressions for parsing
pat_id = re.compile(r">(.+?)\.\.\.")
is_center = re.compile(r">(.+?)\.\.\. \*")
with open(path_to_clstr) as f:
clusters = {}
cluster = []
id_clust = None
next(f) # advance first cluster header
for line in f:
if line.startswith(">"):
# if cluster ended, flush seq ids to it
clusters[id_clust] = cluster
cluster = []
continue
match = pat_id.search(line)
if match:
if is_center.search(line):
id_clust = match[1]
else:
cluster.append(match[1])
clusters[id_clust] = cluster
return clusters
def scale_up_cd_hit(paths_to_clstr):
'''
Hierarchically expand CD-hit clusters.
Parameters
----------
paths_to_clstr: list[str]
paths to rest of the cd-hit output files, sorted by
decreasing similarity (first is 100).
Output
------
clust_now: dict
id: ids
'''
clust_above = parse_cd_hit(paths_to_clstr[0])
for path in paths_to_clstr[1:]:
clust_now = parse_cd_hit(path)
for center in clust_now:
clust_now[center] += [
seq
for a_center in clust_now[center] + [center]
for seq in clust_above[a_center]
]
clust_above = clust_now
return clust_above
def find_cluster_members(folder, filename):
'''
Go through the cluster files and collect
all the cluster members, while indicating
which belongs where.
'''
# get a list of filenames
CLUSTER_FILES = [
join(folder, filename + f"_clustered_sequences_{sim}.fasta.clstr")
for sim in [100, 90, 80, 70, 60, 50, 40]
]
# collect all cluster members
clusters = scale_up_cd_hit(CLUSTER_FILES)
ind_clusters = {}
i = 0
for clus in clusters:
ind_clusters[i] = [clus] + clusters[clus]
i += 1
# convert to format that is suitable for data frames
clusters_for_df = {'cluster': [], 'member': []}
for ind in ind_clusters:
for member in ind_clusters[ind]:
clusters_for_df['cluster'].append(ind)
clusters_for_df['member'].append(member)
df = pd.DataFrame.from_dict(clusters_for_df, orient='columns')
return df
def kfold_by(df, key, k=5):
"""K-Split dataset `df` by values in `key` into `k` groups.
Parameters
----------
df: pandas.DataFrame
key: str
columns to use as splitting
k: int
number of groups.
Returns
-------
k*(groups): pandas.DataFrame
each df is the training set of the fold
"""
kf = KFold(n_splits=k, random_state=4321, shuffle=True)
set_keys = np.unique(df[key])
return [
df[df[key].isin(set_keys[train_index])]
for train_index, _ in kf.split(set_keys)
]
def split_by(df, key, frac=0.8):
"""Split dataset `df` by values in `key`.
Parameters
----------
df: pandas.DataFrame
key: str
columns to use as splitting
frac: float
fraction of `key` groups into `df`.
Returns
-------
(train, test, valid): pandas.DataFrames
"""
# shuffle the data frame
df = df.sample(frac=1, random_state=4321).reset_index(drop=True)
# get all the unique identifiers
set_keys = np.unique(df[key])
# get the training identifiers
train_clusters = np.random.choice(
set_keys, size=int(len(set_keys) * frac), replace=False
)
train = df[df[key].isin(train_clusters)]
# from the remaining ones, put half as validation and half as test
remaining = df[~df.index.isin(train.index)]
# valid and test sets will have equal sizes of 1-frac
# at this point we are not worried about `key` anymore
valid = remaining.sample(frac=1 / 2)
test = remaining[~remaining.index.isin(valid.index)]
return train, test, valid
def make_splits(folder, df):
'''
Takes an input data frame with information on cluster
belongings and generates train/validation/test splits for DL.
'''
# make train/validation/test splits for DL
train, validation, test = split_by(df, "cluster", frac=0.8)
train.drop('cluster', axis=1).to_csv(join(folder, f"split_training.tsv"),
sep="\t", index=False, header=False)
validation.drop('cluster', axis=1).to_csv(join(folder, f"split_validation.tsv"),
sep="\t", index=False, header=False)
test.drop('cluster', axis=1).to_csv(join(folder, "split_test.tsv"),
sep="\t", index=False, header=False)