File: GenomeGraphReader.cpp

package info (click to toggle)
ray 2.3.1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,008 kB
  • sloc: cpp: 49,973; sh: 339; makefile: 281; python: 168
file content (292 lines) | stat: -rw-r--r-- 6,577 bytes parent folder | download | duplicates (5)
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
/*
    Copyright 2013 Sébastien Boisvert
    Copyright 2013 Université Laval
    Copyright 2013 Centre Hospitalier Universitaire de Québec

    This file is part of Ray Surveyor.

    Ray Surveyor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3 of the License.

    Ray Surveyor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ray Surveyor.  If not, see <http://www.gnu.org/licenses/>.
*/

// TODO: validate that the kmer length is the same for this file
// and the provided -k argument

#include "GenomeGraphReader.h"
#include "CoalescenceManager.h"

#include <code/Mock/constants.h>
#include <code/Mock/common_functions.h>
#include <code/KmerAcademyBuilder/Kmer.h>
#include <code/VerticesExtractor/Vertex.h>

#include <iostream>
#include <sstream>
using namespace std;

#include <string.h>

GenomeGraphReader::GenomeGraphReader() {

}

GenomeGraphReader::~GenomeGraphReader() {

}

void GenomeGraphReader::receive(Message & message) {

	int type = message.getTag();

	/*
	printName();
	cout << "received tag " << type << endl;
*/

	if(type == START_PARTY) {

		startParty(message);

	} else if(type == CoalescenceManager::PAYLOAD_RESPONSE) {

		/*
		printName();
		cout << " DEBUG readLine because PAYLOAD_RESPONSE" << endl;
		*/
		// read the next line now !
		readLine();
	}
}

void GenomeGraphReader::startParty(Message & message) {

	char * buffer = (char*) message.getBufferBytes();

	memcpy(&m_aggregator, buffer, sizeof(int));
	//m_aggregator = *(int*)(message.getBufferBytes());

	m_reader.open(m_fileName.c_str());

	m_bad = false;

	if(!m_reader.isValid())
		m_bad = true;

	m_loaded = 0;

	printName();
	cout <<"opens file " << m_fileName << endl;

	m_parent = message.getSourceActor();

	/*
	printName();
	cout << "DEBUG startParty" << endl;
	cout << " bytes in message: " << message.getNumberOfBytes();
	cout << " must send messages to aggregator " << m_aggregator;
	cout << endl;
*/

	int source = message.getSourceActor();
	Message response;
	response.setTag(START_PARTY_OK);

	send(source, response);

	readLine();
}

// DONE 2013-10-16: add a BufferedLineReader class in RayPlatform
// and use it here.
void GenomeGraphReader::readLine() {

	char buffer[1024];
	buffer[0] = '\0';

	while(!m_bad && !m_reader.eof()) {
		m_reader.getline(buffer, 1024);

		// skip comment
		if(strlen(buffer) > 0 && buffer[0] == '#')
			continue;

		break;
	}

	if(m_bad || m_reader.eof()) {

		m_reader.close();

		printName();

		if(m_bad) {
			cout << " Error: file " << m_fileName << " does not exist";
			cout << endl;

		} else {
			cout << " finished reading file " << m_fileName;
			cout << " got " << m_loaded << " objects" << endl;
		}

		Message finishedMessage;
		finishedMessage.setTag(DONE);

		send(m_parent, finishedMessage);

		die();
	} else {

		// AGCTGTGAAACTGGTGCAAGCTACCAGAATC;36;A;C
		string sequence;
		CoverageDepth coverage;
		string parents;
		string children;

		for(int i = 0 ; i < (int) strlen(buffer) ; ++i) {
			if(buffer[i] == ';')
				buffer[i] = ' ';
		}

		istringstream stringBuffer(buffer);

		stringBuffer >> sequence;
		stringBuffer >> coverage;
		stringBuffer >> parents;
		stringBuffer >> children;

		///////////////////////////////////////////////////////////////////////
		// convert the sequence to upper case

		map<char,char> translationTable;
		translationTable['a'] = 'A';
		translationTable['t'] = 'T';
		translationTable['g'] = 'G';
		translationTable['c'] = 'C';

		for(int i = 0 ; i < (int) sequence.length() ; ++i) {

			char symbol = sequence[i];

			if(translationTable.count(symbol) > 0) {
				char correct = translationTable[symbol];

				sequence [i] = correct;
			}
		}
#if 0
		cout << "DEBUG " << sequence << " with " << coverage << endl;
#endif

		// if this is the first one, send the k-mer length too
		if(m_loaded == 0) {

			Message aMessage;
			aMessage.setTag(CoalescenceManager::SET_KMER_LENGTH);

			int length = sequence.length();
			aMessage.setBuffer(&length);
			aMessage.setNumberOfBytes(sizeof(length));

			send(m_aggregator, aMessage);
		}

		Kmer kmer;
		kmer.loadFromTextRepresentation(sequence.c_str());

		Vertex vertex;
		vertex.setKey(kmer);
		vertex.setCoverageValue(coverage);

		// add parents
		for(int i = 0 ; i < (int)parents.length() ; ++i) {

			string parent = sequence;
			for(int j = 0 ; j < (int) parent.length()-1 ; ++j) {
				parent[j + 1] = parent[j];
			}
			parent[0] = parents[i];

			Kmer parentKmer;
			parentKmer.loadFromTextRepresentation(parent.c_str());

			vertex.addIngoingEdge(&kmer, &parentKmer, sequence.length());
		}

		// add children
		for(int i = 0 ; i < (int)children.length() ; ++i) {

			string child = sequence;
			for(int j = 0 ; j < (int) child.length()-1 ; ++j) {
				child[j] = child[j + 1];
			}
			child[child.length() - 1] = children[i];

			Kmer childKmer;
			childKmer.loadFromTextRepresentation(child.c_str());

			vertex.addOutgoingEdge(&kmer, &childKmer, sequence.length());
		}

		char messageBuffer[100];
		int position = 0;

		position += vertex.dump(messageBuffer + position);
		memcpy(messageBuffer + position, &m_sample, sizeof(m_sample));

		position += sizeof(m_sample);

// maybe: accumulate many objects before flushing it.
// we can go up to MAXIMUM_MESSAGE_SIZE_IN_BYTES bytes.

		/*
		printName();
		cout << " got data line " << buffer;
		cout << " sending PAYLOAD to " << m_aggregator << endl;
*/
		Message message;
		message.setTag(CoalescenceManager::PAYLOAD);
		message.setBuffer(messageBuffer);
		message.setNumberOfBytes(position);

#if 0
		printName();
		cout << "DEBUG sending PAYLOAD to " << m_aggregator;
		cout << " with " << position << " bytes ";
		vertex.print(sequence.length(), false);
		cout << endl;
#endif

		int period = 1000000;
		if(m_loaded % period == 0) {
			printName();
			cout << " loaded " << m_loaded << " sequences" << endl;

		}
		m_loaded ++;
		send(m_aggregator, message);
	}
}

void GenomeGraphReader::setFileName(string & fileName, int sample) {

	//int nameSpace = COLOR_NAMESPACE_SAMPLE;
	//m_sample = (uint64_t)sample + nameSpace * COLOR_NAMESPACE_MULTIPLIER;

	m_sample = sample;

	m_fileName = fileName;

#if 0
	printName();
	cout << " DEBUG setFileName " << m_fileName << endl;
#endif
}