-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataContainer.py
396 lines (375 loc) · 19.2 KB
/
dataContainer.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import utils
import records
import os
import numpy as np
from tensorflow import dtypes as tfdtypes
from scipy.sparse import save_npz, csr_matrix
from tqdm import tqdm
class DataContainer():
def __init__(self, chromosome, matrixfilepath, chromatinFolder, binsize=None):
self.chromosome = str(chromosome)
self.matrixfilepath = matrixfilepath
self.chromatinFolder = chromatinFolder
self.FactorDataArray = None
self.nr_factors = None
self.sparseHiCMatrix = None
self.sequenceArray = None
self.binsize = None
if matrixfilepath is None: #otherwise it will be defined by the Hi-C matrix itself upon loading
self.binsize = binsize
self.factorNames = None
self.prefixDict_factors = None
self.prefixDict_matrix = None
self.prefixDict_sequence = None
self.chromSize_factors = None
self.chromSize_matrix = None
self.storedFeatures = None
self.storedFiles = None
self.windowsize = None
self.flankingsize = None
self.maxdist = None
self.data_loaded = False
def __loadFactorData(self, ignoreChromLengths=False, scaleFeatures=False, clampFeatures=False):
#load chromatin factor data from bigwig files
if self.chromatinFolder is None:
return
#ensure that binsizes for matrix (if given) and factors match
if self.binsize is None:
msg = "No binsize given; use a Hi-C matrix or explicitly specify binsize for the container"
raise TypeError(msg)
###load data for a specific chromsome
#get the names of the bigwigfiles
bigwigFileList = utils.getBigwigFileList(self.chromatinFolder)
bigwigFileList = sorted(bigwigFileList)
if len(bigwigFileList) is None:
msg = "Warning: folder {:s} does not contain any bigwig files"
msg = msg.format(self.chromatinFolder)
print(msg)
return
#check the chromosome name prefixes (e.g. "" or "chr") and sizes
chromSizeList = []
prefixDict_factors = dict()
for bigwigFile in bigwigFileList:
try:
prefixDict_factors[bigwigFile] = utils.getChromPrefixBigwig(bigwigFile)
chromname = prefixDict_factors[bigwigFile] + self.chromosome
chromSizeList.append( utils.getChromSizesFromBigwig(bigwigFile)[chromname] )
except Exception as e:
msg = str(e) + "\n"
msg += "Could not load data from bigwigfile {}".format(bigwigFile)
raise IOError(msg)
#the chromosome lengths should be equal in all bigwig files
if len(set(chromSizeList)) != 1 and not ignoreChromLengths:
msg = "Invalid data. Chromosome lengths differ in bigwig files:"
for i, filename in enumerate(bigwigFileList):
msg += "{:s}: {:d}\n".format(filename, chromSizeList[i])
raise IOError(msg)
elif len(set(chromSizeList)) != 1 and ignoreChromLengths:
chromSize_factors = min(chromSizeList)
else:
chromSize_factors = chromSizeList[0]
#the chromosome lengths of matrices and bigwig files must be equal
if self.chromSize_matrix is not None \
and self.chromSize_matrix != chromSize_factors:
msg = "Chrom lengths not equal between matrix and bigwig files\n"
msg += "Matrix: {:d} -- Factors: {:d}".format(self.chromSize_matrix, chromSize_factors)
raise IOError(msg)
#load the data into memory now
self.factorNames = [os.path.splitext(os.path.basename(name))[0] for name in bigwigFileList]
self.nr_factors = len(self.factorNames)
self.prefixDict_factors = prefixDict_factors
self.chromSize_factors = chromSize_factors
nr_bins = int( np.ceil(self.chromSize_factors / self.binsize) )
self.FactorDataArray = np.empty(shape=(len(bigwigFileList),nr_bins))
msg = "Loaded {:d} chromatin features from folder {:s}\n"
msg = msg.format(self.nr_factors, self.chromatinFolder)
featLoadedMsgList = [] #pretty printing for features loaded
for i, bigwigFile in enumerate(bigwigFileList):
chromname = self.prefixDict_factors[bigwigFile] + self.chromosome
tmpArray = utils.binChromatinFactor(pBigwigFileName=bigwigFile,
pBinSizeInt=self.binsize,
pChromStr=chromname,
pChromSize=self.chromSize_factors)
if clampFeatures:
tmpArray = utils.clampArray(tmpArray)
if scaleFeatures:
tmpArray = utils.scaleArray(tmpArray)
self.FactorDataArray[i] = tmpArray
nr_nonzero_abs = np.count_nonzero(tmpArray)
nr_nonzero_perc = nr_nonzero_abs / tmpArray.size * 100
msg2 = "{:s} - min. {:.3f} - max. {:.3f} - nnz. {:d} ({:.2f}%)"
msg2 = msg2.format(bigwigFile, tmpArray.min(), tmpArray.max(), nr_nonzero_abs, nr_nonzero_perc)
featLoadedMsgList.append(msg2)
self.FactorDataArray = np.transpose(self.FactorDataArray)
print(msg + "\n".join(featLoadedMsgList))
def __loadMatrixData(self, scaleMatrix=False):
#load Hi-C matrix from cooler file
if self.matrixfilepath is None:
return
try:
prefixDict_matrix = {self.matrixfilepath: utils.getChromPrefixCooler(self.matrixfilepath)}
chromname = prefixDict_matrix[self.matrixfilepath] + self.chromosome
chromsize_matrix = utils.getChromSizesFromCooler(self.matrixfilepath)[chromname]
sparseHiCMatrix, binsize = utils.getMatrixFromCooler(self.matrixfilepath, chromname)
except:
msg = "Error: Could not load data from Hi-C matrix {:s}"
msg = msg.format(self.matrixfilepath)
raise IOError(msg)
#scale to 0..1, if requested
if scaleMatrix:
sparseHiCMatrix = utils.scaleArray(sparseHiCMatrix)
#ensure that chrom sizes for matrix and factors are the same
if self.chromSize_factors is not None and self.chromSize_factors != chromsize_matrix:
msg = "Chromsize of matrix does not match bigwig files\n"
msg += "Matrix: {:d} -- Bigwig files: {:d}"
msg = msg.format(chromsize_matrix, self.chromSize_factors)
raise IOError(msg)
self.chromSize_matrix = chromsize_matrix
#ensure that binsizes for matrix and factors (if given) match
if self.binsize is None or self.binsize == binsize:
self.binsize = binsize
self.sparseHiCMatrix = sparseHiCMatrix
elif self.binsize is not None and self.binsize != binsize:
msg = "Matrix has wrong binsize\n"
msg += "Matrix: {:d} -- Binned chromatin factors {:d}"
msg = msg.format(binsize, self.binsize)
raise IOError(msg)
msg = "Loaded cooler matrix {:s}\n".format(self.matrixfilepath)
msg += "chr. {:s}, matshape {:d}*{:d} -- min. {:d} -- max. {:d} -- nnz. {:d}"
msg = msg.format(self.chromosome, self.sparseHiCMatrix.shape[0], self.sparseHiCMatrix.shape[1], int(self.sparseHiCMatrix.min()), int(self.sparseHiCMatrix.max()), self.sparseHiCMatrix.getnnz() )
print(msg)
def __unloadFactorData(self):
#unload chromatin factor data to save memory, but do not touch metadata
self.FactorDataArray = None
def __unloadMatrixData(self):
#unload matrix data to save memory, but do not touch metadata
self.sparseHiCMatrix = None
def unloadData(self):
#unload all data to save memory, but do not touch metadata
self.__unloadFactorData
self.__unloadMatrixData
self.windowsize = None
self.flankingsize = None
self.maxdist = None
self.data_loaded = False
def loadData(self, windowsize, flankingsize=None, maxdist=None, scaleFeatures=False, clampFeatures=False, scaleTargets=False):
if not isinstance(windowsize, int):
msg = "windowsize must be integer"
raise TypeError(msg)
if isinstance(maxdist, int):
maxdist = np.clip(maxdist, a_min=1, a_max=self.windowsize)
self.__loadMatrixData(scaleMatrix=scaleTargets)
self.__loadFactorData(scaleFeatures=scaleFeatures, clampFeatures=clampFeatures)
self.windowsize = windowsize
self.flankingsize = flankingsize
self.maxdist = maxdist
self.data_loaded = True
def checkCompatibility(self, containerIterable):
ret = []
try:
for container in containerIterable:
ret.append(self.__checkCompatibility(container))
except:
ret = [self.__checkCompatibility(containerIterable)]
return np.all(ret)
def __checkCompatibility(self, container):
if not isinstance(container, DataContainer):
return False
if not self.data_loaded or not container.data_loaded:
return False
#check if the same kind of data is available for all containers
factorsOK = type(self.FactorDataArray) == type(container.FactorDataArray)
matrixOK = type(self.sparseHiCMatrix) == type(container.sparseHiCMatrix)
#check if windowsize, flankingsize and maxdist match
windowsizeOK = self.windowsize == container.windowsize
flankingsizeOK = self.flankingsize == container.flankingsize
maxdistOK = self.maxdist == container.maxdist
#sanity check loading of bigwig files
if self.chromatinFolder is not None and self.nr_factors is None:
return False
if container.chromatinFolder is not None and container.nr_factors is None:
return False
#if chromatin factors are present, the numbers and names of chromatin factors must match
factorsOK = factorsOK and (self.nr_factors == container.nr_factors)
factorsOK = factorsOK and (self.factorNames == container.factorNames)
return factorsOK and matrixOK and windowsizeOK and flankingsizeOK and maxdistOK
def writeTFRecord(self, pOutfolder, pRecordSize=None):
'''
Write a dataset to disk in tensorflow TFRecord format
Parameters:
pWindowsize (int): size of submatrices
pOutfolder (str): directory where TFRecords will be written
pFlankingsize (int): size of flanking regions left/right of submatrices
pMaxdist (int): cut the matrices off at this distance (in bins)
pRecordsize (int): split the TFRecords into multiple files containing approximately this number of samples
Returns:
list of filenames written
'''
if not self.data_loaded:
msg = "Warning: No data loaded, nothing to write"
print(msg)
return None
nr_samples = self.getNumberSamples()
#adjust record size (yields smaller files and reduces memory load)
recordsize = nr_samples
if pRecordSize is not None and pRecordSize < recordsize:
recordsize = pRecordSize
#compute number of record files, number of samples
#in each file and corresponding indices
nr_files = int( np.ceil(nr_samples/recordsize) )
target_ct = int( np.floor(nr_samples/nr_files) )
samples_per_file = [target_ct]*(nr_files-1) + [nr_samples-(nr_files-1)*target_ct]
sample_indices = [sum(samples_per_file[0:i]) for i in range(len(samples_per_file)+1)]
#write the single files
folderName = self.chromatinFolder.rstrip("/").replace("/","-")
recordfiles = [os.path.join(pOutfolder, "{:s}_{:s}_{:03d}.tfrecord".format(folderName, str(self.chromosome), i + 1)) for i in range(nr_files)]
for recordfile, firstIndex, lastIndex in tqdm(zip(recordfiles, sample_indices, sample_indices[1:]), desc="Storing TFRecord files", total=len(recordfiles)):
recordDict, storedFeaturesDict = self.__prepareWriteoutDict(pFirstIndex=firstIndex,
pLastIndex=lastIndex,
pOutfolder=pOutfolder)
records.writeTFRecord(pFilename=recordfile, pRecordDict=recordDict)
self.storedFiles = recordfiles
self.storedFeatures = storedFeaturesDict
return recordfiles
def getNumberSamples(self):
if not self.data_loaded:
return None
featureArrays = [self.FactorDataArray, self.sparseHiCMatrix, self.sequenceArray]
cutouts = [self.windowsize+2*self.flankingsize, self.windowsize+2*self.flankingsize, (self.windowsize+2*self.flankingsize)*self.binsize]
nr_samples_list = []
for featureArray, cutout in zip(featureArrays, cutouts):
if featureArray is not None:
nr_samples_list.append(featureArray.shape[0] - cutout + 1)
else:
nr_samples_list.append(0)
#check if all features have the same number of samples
if len(set( [x for x in nr_samples_list if x>0] )) != 1:
msg = "Error: sample binning / DNA sequence encoding went wrong"
raise RuntimeError(msg)
return max(nr_samples_list)
def __getMatrixData(self, idx):
if self.matrixfilepath is None:
return None # this can't work
if not self.data_loaded:
msg = "Error: Load data first"
raise RuntimeError(msg)
#the 0-th matrix starts flankingsize away from the boundary
windowsize = self.windowsize
flankingsize = self.flankingsize
if flankingsize is None:
flankingsize = windowsize
self.flankingsize = windowsize
startInd = idx + flankingsize
stopInd = startInd + windowsize
trainmatrix = self.sparseHiCMatrix[startInd:stopInd,startInd:stopInd].todense()
trainmatrix = np.array(np.nan_to_num(trainmatrix))
trainmatrix = np.expand_dims(trainmatrix, axis=-1) #make Hi-C (sub-)matrix an RGB image
return trainmatrix
def __getFactorData(self, idx):
if self.chromatinFolder is None:
return None
if not self.data_loaded:
msg = "Error: Load data first"
raise RuntimeError(msg)
#the 0-th feature matrix starts at position 0
windowsize = self.windowsize
flankingsize = self.flankingsize
if flankingsize is None:
flankingsize = windowsize
self.flankingsize = windowsize
startIdx = idx
endIdx = startIdx + 2*flankingsize + windowsize
factorArray = self.FactorDataArray[startIdx:endIdx]
factorArray = np.expand_dims(factorArray, axis=-1)
return factorArray
def getSampleData(self, idx):
if not self.data_loaded:
return None
factorArray = self.__getFactorData(idx)
matrixArray = self.__getMatrixData(idx)
if matrixArray is not None:
matrixArray = matrixArray.astype("float32")
return {"factorData": factorArray.astype("float32"),
"out_matrixData": matrixArray}
def plotFeatureAtIndex(self, idx, outpath, figuretype="png"):
if not self.data_loaded:
msg = "Warning: No data loaded, nothing to plot"
print(msg)
return
if isinstance(idx, int) and (idx >= self.FactorDataArray.shape[0] or idx < 0):
msg = "Error: Invalid index {:d}; must be None or integer in 0..{:d}".format(idx, self.FactorDataArray.shape[0]-1)
raise ValueError(msg)
if isinstance(idx, int):
factorArray = self.__getFactorData(idx)
startBin = idx
else:
factorArray = self.FactorDataArray
startBin = None
for plotType in ["box", "line"]:
utils.plotChromatinFactors(pFactorArray=factorArray,
pFeatureNameList=self.factorNames,
pChromatinFolder=self.chromatinFolder,
pChrom=self.chromosome,
pBinsize=self.binsize,
pStartbin=startBin,
pOutputPath=outpath,
pPlotType=plotType,
pFigureType=figuretype)
def plotFeaturesAtPosition(self, position, outpath, figuretype="png"):
if not self.data_loaded:
msg = "Warning: No data loaded, nothing to plot"
print(msg)
return
if isinstance(position, int) and position > self.chromSize_factors:
msg = "Error: Invalid position {:d}; must be in 0..{:d}"
msg = msg.format(position, self.chromSize_factors)
raise ValueError(msg)
#compute the bin index from the position
elif isinstance(position, int):
idx = int(np.floor(position / self.binsize))
else:
idx = None
return self.plotFeatureAtIndex(idx=idx,
outpath=outpath,
figuretype=figuretype)
def saveMatrix(self, outputpath, index=None):
if not self.data_loaded:
msg = "Warning: No data loaded, nothing to save"
print(msg)
return
sparseMatrix = None
windowsize = self.windowsize
flankingsize = self.flankingsize
if not isinstance(flankingsize, int):
flankingsize = windowsize
if isinstance(self.maxdist, int) and self.maxdist < windowsize and self.maxdist > 0:
maxdist = self.maxdist
else:
maxdist = windowsize
if isinstance(index, int) and index < self.getNumberSamples():
tmpMat = np.zeros(shape=(windowsize, windowsize))
indices = np.mask_indices(windowsize, utils.maskFunc, k=maxdist)
tmpMat[indices] = self.__getMatrixData(idx=index)
sparseMatrix = csr_matrix(tmpMat)
else:
sparseMatrix = self.sparseHiCMatrix
folderName = self.chromatinFolder.rstrip("/").replace("/","-")
filename = "matrix_{:s}_chr{:s}_{:s}".format(folderName, str(self.chromosome), str(index))
filename = os.path.join(outputpath, filename)
save_npz(file=filename, matrix=sparseMatrix)
def __prepareWriteoutDict(self, pFirstIndex, pLastIndex, pOutfolder):
if not self.data_loaded:
msg = "Error: no data loaded, nothing to prepare"
raise RuntimeError(msg)
data = [ self.getSampleData(idx=i) for i in range(pFirstIndex, pLastIndex) ]
recordDict = dict()
storedFeaturesDict = dict()
if len(data) < 1:
msg = "Error: No data to write"
raise RuntimeError(msg)
for key in data[0]:
featData = [feature[key] for feature in data]
if not any(elem is None for elem in featData):
recordDict[key] = np.array(featData)
storedFeaturesDict[key] = {"shape": recordDict[key].shape[1:], "dtype": tfdtypes.as_dtype(recordDict[key].dtype)}
return recordDict, storedFeaturesDict