-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_dataset.py
320 lines (257 loc) · 10.8 KB
/
create_dataset.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#### import from coconet
from pydca.contact_visualizer.contact_visualizer import DCAVisualizer
from pydca.fasta_reader import fasta_reader
from inputreader import InputReader
import subprocess
import numpy as np
import logging
import os, errno
import glob
from datetime import datetime
import pickle
import random
from pathlib import Path
from argparse import ArgumentParser
import sys
import torch
from torch.utils.data.dataset import Dataset
logger = logging.getLogger(__name__)
class CoCoNetDataset:
"""Implements RNA contact prediction using direct coupling analysis enhanced by
a simple convolutional neural network.
"""
def __init__(self, data_dir, linear_dist=None, contact_dist=None):
"""Initializes CocoNet instance.
Parameters
----------
self : CocoNet
An instance of CocoNet class.
dir_msa_files : str
Path to directory containing the MSA files.
dir_pdb_file : str
Path to the directory containing the PDB files.
dir_refseq_files : str
Path to the directory containing reference sequence files.
linear_dist : int
Distance between sites in reference sequence
contact_dist : float
Maximum distance between two residues in PDB file to be considered
contacts.
"""
self.__data_dir = os.path.abspath(data_dir)
self.__linear_dist = linear_dist if linear_dist is not None else 4
self.__contact_dist = contact_dist if contact_dist is not None else 10.0
pdb_chains_list_file = os.path.join(self.__data_dir, 'CCNListOfPDBChains.txt')
msa_files_list_file = os.path.join(self.__data_dir, 'CCNListOfMSAFiles.txt')
pdb_files_list_file = os.path.join(self.__data_dir, 'CCNListOfPDBFiles.txt')
input_reader = InputReader()
self.__msa_file_names_list = input_reader.read_from_one_column_text_file(msa_files_list_file)
self.__pdb_chains_list = input_reader.read_from_one_column_text_file(pdb_chains_list_file)
self.__pdb_file_names_list = input_reader.read_from_one_column_text_file(pdb_files_list_file)
self.__msa_files_dir = os.path.join(self.__data_dir, 'MSA_200') ######################### Using the MSA from the new directory
self.__refseqs_dir = os.path.join(self.__data_dir, 'sequences')
self.__pdb_files_dir = os.path.join(self.__data_dir, 'PDBFiles')
self.__secstruct_files_dir = os.path.join(self.__data_dir, 'secstruct')
self.msa_files_list = [
os.path.abspath(os.path.join(self.__msa_files_dir, msa_file + '.faclean')) for msa_file in self.__msa_file_names_list
]
self.__refseqs = self.get_refseqs()
self.__refseqs_len = self.get_refseqs_len()
logmsg = """
Data directory : {},
PDB chains list file : {},
MSA files list file : {},
PDB files list file : {},
""".format(self.__data_dir, pdb_chains_list_file,
msa_files_list_file, pdb_files_list_file,
)
logger.info(logmsg)
return None
@property
def pdb_file_names_list(self):
return self.__pdb_file_names_list
@property
def msa_file_names_list(self):
return self.__msa_file_names_list
@property
def pdb_chains_list(self):
return self.__pdb_chains_list
def map_pdb_id_to_family(self):
"""Mapps PDB ID to family name.
Parameters
----------
self : CocoNet(self, data_dir, linear_dist=None, contact_dist=None)
Returns
-------
pdb_id_to_fam_name : dict
pdb_id_to_fam_name[pdb_id]=fam_name
"""
pdb_id_to_fam_name = dict()
for pdb_id, fam_name in zip(self.__pdb_file_names_list, self.__msa_file_names_list):
pdb_id_to_fam_name[pdb_id] = fam_name
return pdb_id_to_fam_name
@staticmethod
def _to_dict(files_list):
"""Puts a list of file paths into a dictionary
Parameters
----------
files_list : list
A list of file paths
Returns
-------
files_dict : dict
A dictionary whose keys are basenames of files and values file path.
"""
files_dict = dict()
for f in files_list:
basename, _ = os.path.splitext(os.path.basename(f))
files_dict[basename] = f
return files_dict
def get_refseq_files_list(self):
"""
"""
refseq_files_list = [
os.path.join(self.__refseqs_dir, pdb_file[:4] + '.fa') for pdb_file in self.__pdb_file_names_list
]
return tuple(refseq_files_list)
def get_pdb_files_list(self):
"""
"""
pdb_files_list = [
os.path.join(self.__pdb_files_dir, pdb_file + '.pdb') for pdb_file in self.__pdb_file_names_list
]
return tuple(pdb_files_list)
def create_directories(self, dir_path):
"""Creates (nested) directory given path.
Parameters
----------
self : CocoNet
An instance of CocoNet class.
dir_path : str
Directory path.
Returns
-------
None : None
"""
try:
os.makedirs(dir_path)
except OSError as e:
if e.errno !=errno.EEXIST:
logger.error('Unable to create directory using path {}'.format(
dir_path)
)
raise
return None
def get_pdb_data(self):
"""Computes mapped PDB contacts for multiple RNA families. The computed
mapped PDB data is pickled and only recomputed if any of reference sequence
files, PDB files or PDB chain metadata file is updated.
Parameters
----------
self : CocoNet
An instance of CocoNet class
Returns
-------
mapped_pdb_data : dict
A dictionary whose keys are RNA familiy names and values dictionaries
that have site pair keys and PDB data values.
"""
refseq_files_list = self.get_refseq_files_list()
pdb_files_list = self.get_pdb_files_list()
logger.info('\n\tObtaining mapped PDB data')
txtfreader = InputReader()
mapped_pdb_data = dict()
for chain_id, pdb_file, refseq_file, msa_file in zip(self.__pdb_chains_list, pdb_files_list, refseq_files_list, self.__msa_file_names_list):
curr_pdb_data, _missing, _refseq_len = txtfreader.get_mapped_pdb_data(pdb_chain_id=chain_id,
refseq_file=refseq_file, pdb_file=pdb_file, linear_dist=self.__linear_dist,
contact_dist=self.__contact_dist
)
# self.__msa_file_names_list contains the list of MSA files, not the full path of the files
famname, _ext = os.path.splitext(msa_file)
mapped_pdb_data[famname] = curr_pdb_data
print(famname)
return mapped_pdb_data
def get_refseqs(self):
"""Obtains reference sequences of several RNA famlies from fasta formatted file.
Parameters
----------
self : CocoNet
An instance of CocoNet class
Returns
-------
reference_sequenes_dict : dict()
reference_sequences_dict[FAMILY_NAME] = sequence
"""
refseq_files_list = self.get_refseq_files_list()
logger.info('\n\tObtaining reference sequences from FASTA files')
reference_sequences_dict = dict()
for refseq_file, msa_file_basename in zip(refseq_files_list, self.__msa_file_names_list):
# if reference sequence file contains multiple sequences, take the first one.
reference_sequences_dict[msa_file_basename.strip()] = fasta_reader.get_alignment_from_fasta_file(refseq_file)[0].strip()
return reference_sequences_dict
def get_refseqs_len(self):
"""Obtains length of reference sequence for each RNA family
Parameters
----------
self : CocoNet
An instance of CocoNet class
Returns
-------
refseqs_len_dict : dict()
refseqs_len_dict[FAMILY_NAME] = refseq_length
"""
refseqs_dict = self.__refseqs
logger.info('\n\tObtaining length of reference sequences for {} RNA families'.format(len(refseqs_dict)))
refseqs_len_dict = {
fam : len(refseqs_dict[fam]) for fam in refseqs_dict
}
return refseqs_len_dict
class RNA_DATASET(Dataset):
"""
This file is directly modified from https://pytorch.org/docs/stable/torchvision/datasets.html
"""
def __init__(self, input_dict, label_dict):
self.input_dict = input_dict
self.label_dict = label_dict
assert [k for k in self.input_dict] == [k for k in self.label_dict]
self.rna_fam_names = [k for k in self.input_dict]
self.data_len = len(self.rna_fam_names)
def __getitem__(self, index):
rna_fam_name = self.rna_fam_names[index]
input_tensor = self.input_dict[rna_fam_name]
label_tensor = self.label_dict[rna_fam_name]
seq, msa, idx = input_tensor['seq'].squeeze(0), input_tensor['msa'].squeeze(0), input_tensor['index'].squeeze(0)
return seq, msa, idx, label_tensor.squeeze(0)
def __len__(self):
return self.data_len
class RNA_FEATSET(Dataset):
"""
This file is directly modified from https://pytorch.org/docs/stable/torchvision/datasets.html
"""
def __init__(self, input_dict, label_dict):
self.input_dict = input_dict
self.label_dict = label_dict
assert [k for k in self.input_dict] == [k for k in self.label_dict]
self.rna_fam_names = [k for k in self.input_dict]
self.data_len = len(self.rna_fam_names)
def __getitem__(self, index):
rna_fam_name = self.rna_fam_names[index]
input_tensor = self.input_dict[rna_fam_name]
label_tensor = self.label_dict[rna_fam_name]
return input_tensor.squeeze(0), label_tensor.squeeze(0)
def __len__(self):
return self.data_len
def pad_collate(batch):
(xx, yy) = zip(*batch) ## xx.shape[L, L, C], yy.shape[L, L]
x_lens = [x.shape[0] for x in xx]
y_lens = [y.shape[0] for y in yy]
assert max(x_lens) == max(y_lens)
max_L = max(x_lens)
batch_size = len(x_lens)
C = batch[0][0].shape[-1]
xx_pad = torch.zeros(batch_size, max_L, max_L, C)
yy_pad = torch.ones(batch_size, max_L, max_L) * (-100) #### ignored index in nn.CrossEntropyLoss()
for i in range(batch_size):
xx_pad[i, :x_lens[i], :x_lens[i], :] = xx[i]
yy_pad[i, :y_lens[i], :y_lens[i]] = yy[i]
return xx_pad.float(), yy_pad.long()