annotate SMART/Java/Python/Cpp/ncListCreator.cpp @ 24:452e051f6562

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:28:24 -0400
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 #include "ncListCreator.hpp"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 NCListCreator::NCListCreator(string inputFileName): inputFileName(inputFileName) {}
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 string NCListCreator::getFileName(string chromosome) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 return inputFileName.substr(0, inputFileName.find_last_of('.')) + "_" + chromosome + ".bed";
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 void NCListCreator::splitFile () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 Chromosomes chromosomes;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 GenomicInterval interval;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 map <string, ofstream> splittedFiles;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 map <string, ofstream>::iterator it;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 ifstream file;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 string line, fileName;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 string chromosome;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 file.open(inputFileName.c_str());
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 if (file.is_open()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 while (file.good()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 getline(file, line);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 if (line.size() > 0) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 interval.parseFromLine(line);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 chromosomes.insert(interval.chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 fileName = getFileName(interval.chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 it = splittedFiles.find(interval.chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 if (it == splittedFiles.end()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 ofstream outputFile;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 outputFile.open(fileName.c_str(), ios::out | ios::binary);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 interval.writeBinary(outputFile);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 splittedFiles[chromosome] = outputFile;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 else {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 it->second << line << "\n";
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 file.close();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 for (it = splittedFiles.begin(); it != splittedFiles.end(); it++) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 it->second.close();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 else {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 cout << "Unable to open file" << inputFileName;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 void NCListCreator::run() {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 for (Chromosomes::iterator it = chromosomes.begin(); splittedFiles != chromosomes.end(); splittedFiles++) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 buildLists(*it);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 void NCListCreator::buildLists(string chromosome) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 createTables(chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 labelLists();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 computeSubStart();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 computeAbsPosition();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 cleanFiles();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 transfer(chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 void NCListCreator::createTables(string chromosome) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 initLists(chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 h = new Table(H_CELL_SIZE, nbLists);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 t = new Table(T_CELL_SIZE, nbLines);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 l = new Table(L_CELL_SIZE, nbLines);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 fillTables(chromosome);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 void NCListCreator::initLists (string chromosome) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 nbLists = 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 nbLines = 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 ifstream file;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 file.open(getFileName(chromosome).c_str(), ios::in | ios::binary);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 Interval currentInterval, previousInterval;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 if (file.is_open()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 while (file.good()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 if (currentInterval.parseBinary(file)) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 nbLines++;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 if (previousInterval.include(currentInterval)) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 nbLists++;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 previousInterval = currentInterval;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 file.close();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 void NCListCreator::fillTables (string chromosome) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 ifstream file;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 file.open(getFileName(chromosome).c_str(), ios::in | ios::binary);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 Interval currentInterval, previousInterval;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 unsigned int i = 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 if (file.is_open()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 while (file.good()) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 if (currentInterval.parseBinary(file)) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 t->write(currentInterval.start, i, 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 t->writeHere(currentInterval.end);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 t->writeHere(-1);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 t->writeHere(-1);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 i++;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 file.close();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 t->write(SENTINEL, LIST, -1);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 l->write(SENTINEL, LIST, 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 t->write(SENTINEL, NEW, -1);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 void NCListCreator::labelLists () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 unsigned int nextL = 0, thisL, length;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 unsigned int p;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 Interval current, parent;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 for (unsigned int i = 0; i < nbLines; i++) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 p = i - 1;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 t->moveTo(p, 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 parent.readBinary(t->file);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 t->moveTo(i, 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 current.readBinary(t->file);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 while ((p != SENTINEL) && (! parent.include(current))) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 p = t->read(p, PARENT);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 t->moveTo(p, 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 parent.readBinary(t->file);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 thisL = t->read(p, LIST);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 if (thisL == SENTINEL) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 thisL = nextL;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 nextL++;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131 length = 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 t->write(p, LIST, thisL);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 else {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 length = h->read(thisL, LENGTH);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 t->write(i, PARENT, p);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 h->write(thisL, LENGTH, length+1);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 void NCListCreator::computeSubStart () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 unsigned int total = 0;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 for (unsigned int i = 0; i < nbLists; i++) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 h->write(i, START, total);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146 total += h->read(i, LENGTH);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 h->write(i, LENGTH, 0);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 void NCListCreator::computeAbsPosition () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152 Value s, e, pt, hp, pl, nb, lp;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 for (unsigned int i = 0; i < nbLines; i++) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 s = t->read(i, START);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155 e = t->read(i, END);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 pt = t->read(i, PARENT);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 hp = t->read(pt, LIST);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 pl = t->read(pt, NEW);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159 nb = h->read(hp, LENGTH);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 lp = h->read(hp, START) + nb;
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 t->write(i, NEW, lp);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 l->write(lp, START, s);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 l->write(lp, END, e);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 l->write(lp, LIST, SENTINEL);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165 l->write(lp, PARENT, pl);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 h->write(lp, LENGTH, nb+1);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 if (nb == 0) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168 l->write(pl, LIST, hp);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
169 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
170 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
171 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
172
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
173 void NCListCreator::cleanFiles () {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
174 t->destroy();
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
175 }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
176
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
177 void NCListCreator::transfer (string chromosome) {
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
178 ncLists[chromosome] = NCList(h, l);
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
179 }