-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergebw.cpp
272 lines (223 loc) · 7.1 KB
/
mergebw.cpp
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
//
// mergebw.cpp
// tool for merging bigwig files
//
// Author: Alan Boyle
// Copyright (c) 2020 Alan Boyle
// This program comes with ABSOLUTELY NO WARRANTY.
// This is free software, and you are welcome to redistribute it
// under certain conditions.
extern "C" {
#include "./libBigWig/bigWig.h"
}
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <stdint.h>
#include <math.h>
#include <dirent.h>
#include <string>
#include <algorithm>
#include <iterator>
#include <tuple>
using namespace std;
class SignalData {
public:
std::vector<float> binsInput;
// vector of vectors
// chrom names and chrom lengths
SignalData(std::string wigFile, std::string chromosome, int chromLength) {
this->wigFile = wigFile;
this->chromosome = chromosome;
this->chromLength = chromLength;
}
SignalData() {
}
void setWigFile(std::string wigFile) {
this->wigFile = wigFile;
}
std::string getWigFile() {
return wigFile;
}
// Method:
// 1: Read through every poisiton to create a ranked mean - for every cell type sort all positions and create a mean of the values at each rank
// This rankedMean list can probably stay in memory
// need rankedMean and rankedMeanTie for each bin in each chromosome
// 2: Read through every cell type rankify all bins and replace with appropriate ranked mean/mean tie
// Either output as Quantile normalized or this can be used to identify max, min, etc at each position
// Read in the bigWig file for all chromosomes into memory
void readBigWig()
{
int chromStart = 0;
int chromEnd = chromLength;
this->binsInput.clear();
// Note that libBigWig uses char* for input so we have to convert
char *fname = new char[wigFile.length() + 1];
strcpy(fname, wigFile.c_str());
bigWigFile_t *bwFile = bwOpen(fname, NULL, "r");
// convert string to char*
char *chrom = new char[chromosome.length() + 1];
strcpy(chrom, chromosome.c_str());
// If there is no file attached die
if(bwFile == NULL){
cerr << "Failed to open file: " << wigFile << endl;
exit(1);
}
bwOverlappingIntervals_t *ptr = bwGetValues(bwFile, chrom,
static_cast<uint32_t>(chromStart),
static_cast<uint32_t>(chromEnd),
0);
for(int k = 0; k < (int)(ptr->l); ++k){
if(!isnan( ptr->value[k] )){
this->binsInput.push_back(roundf(ptr->value[k] * 1000.0) / 1000.0);
}
}
// cout << bwFile->cl->chrom[0] << " " << bwFile->cl->len[0] << endl;
}
private:
std::string wigFile;
std::string chromosome;
int chromLength;
};
int getdir(std::string dirname, std::vector<string> & files, std::string filetype)
{
DIR *dir;
int pos;
struct dirent *ent;
dir = opendir(dirname.c_str());
if (dir != NULL) {
while ((ent = readdir (dir)) != NULL) {
pos = strlen(ent->d_name) - 7;
if (! strcmp(&ent->d_name[pos], filetype.c_str())) {
//printf("%s\n", ent->d_name); //DEBUG
files.push_back(string(ent->d_name));
}
}
closedir (dir);
} else {
/* could not open directory */
cerr << "Unable to read input files!" << endl;
exit(1);
}
}
double quantile(std::vector<double> v, double q) {
sort(v.begin(), v.end());
double h = ((v.size() - 1) * q) + 1;
if(h >= v.size()) {
h = v.size() - 1;
}
return v[floor(h)] + ((h - floor(h)) * (v[floor(h) + 1] - v[floor(h)]));
}
int quantile(std::vector<int> v, double q) {
sort(v.begin(), v.end());
double h = (((double)v.size() - 1) * q) + 1;
return floor((double)v[floor(h)] + ((h - floor(h)) * ((double)v[floor(h) + 1] - (double)v[floor(h)])));
}
// Function to find rank
void rankify(std::vector<double>& A) {
int n = A.size();
std::vector<double> R(n,0);
std::vector< std::tuple<double, int> > T;
int r = 1;
// Create array of tuples storing value and index
for(int j = 0; j < n; j++) {
T.push_back(std::make_tuple(A[j], j));
}
// Sort tubples by data value
std::sort(begin(T), end(T), [](auto const &t1, auto const &t2) {
return get<0>(t1) < get<0>(t2); // or use a custom compare function
});
int i = 0;
int index, j;
while(i < n) {
j = i;
// Get elements of same rank
while(j < n && std::get<0>(T[j]) == std::get<0>(T[j+1])) {
j++;
}
int m = j - i + 1;
for(j = 0; j < m; j++) {
// For each equal element use .5
index = std::get<1>(T[i+j]);
R[index] = r + (m-1)*0.5;
}
// Increment rank and index
r+=m;
i+=m;
}
A.swap(R);
}
// convert vectors to vector of SignalData
void quantileNormalize(std::vector<SignalData>& data) {
int cellCount = data.size();
int binCount = data[0].binsInput.size();
//First calculate rank means
std::vector<double> rankedMean(binCount,0);
for(int cellID = 0; cellID < cellCount; cellID++) {
std::vector<double> x(binCount,0);
for(int binID = 0; binID < binCount; binID++) {
x[binID] = data[cellID].binsInput[binID];
}
sort(x.begin(), x.end());
for(int binID = 0; binID < binCount; binID++) {
rankedMean[binID] += x[binID];
}
}
for(int binID = 0; binID < binCount; binID++) {
rankedMean[binID] /= (double)cellCount;
}
//calculate half value for ties
std::vector<double> rankedMeanTie(binCount-1,0);
for(int binID = 0; binID < (binCount-1); binID++) {
rankedMeanTie[binID] = ((rankedMean[binID]+rankedMean[binID+1])/2);
}
//Iterate through each cell line
for(int s = 0; s < cellCount; s++) {
std::vector<double> bins(binCount,0);
for(int p = 0; p < binCount; p++) {
bins[p] = data[s].binsInput[p];
}
rankify(bins);
std::vector<double> binsQuantileNormalized(binCount, 0);
for(int p = 0; p < binCount; p++) {
if(std::fmod(bins[p],1) != 0) {
binsQuantileNormalized[p] = rankedMeanTie[(int)floor(bins[p])-1];
} else {
binsQuantileNormalized[p] = rankedMean[(int)(bins[p]-1)];
}
data[s].binsInput[p] = binsQuantileNormalized[p];
}
}
}
int main(int argc, char* argv[])
{
std::vector<string> fileList;
std::vector<SignalData> inputData;
std::string wigFile;
std::string chromosome;
int chromLength;
int segmentSize;
// Will need to read in a file of chromosome sizes and names
chromosome = "chr1";
chromLength = 249250621;
segmentSize = 100000;
//Parameters
std::string inputFolder = "/nfs/boylelabnr_turbo/ENCODE/bigwig/DNase_regulome/"; // make this a commandline option - ends with /!
std::string tempFolder = ".";
//Read in bigWig files for processing
getdir(inputFolder, fileList, ".bigWig"); // may need to also consider bw files
// Now we need to do this for every chromosome
// And then for every sub-segment - need to be careful at the end
// For each file perform calculation on the chromosome and segment
// we should be able to keep the results in memory and write it all out at the end
for(int i = 0; i < fileList.size(); i++) {
wigFile = inputFolder + fileList[i];
inputData.push_back(SignalData(wigFile, chromosome, segmentSize));
cerr << "Processing " << wigFile << endl;
inputData.back().readBigWig();
}
//Options for output: raw or quantile normalized: max, min, quantile
//Quantile Normalize
quantileNormalize(inputData);
}