File: SequenceFileDetector.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 (390 lines) | stat: -rw-r--r-- 8,231 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
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
/*
 *  Ray -- Parallel genome assemblies for parallel DNA sequencing
 *  Copyright (C) 2013 Sébastien Boisvert
 *
 *  http://DeNovoAssembler.SourceForge.Net/
 *
 *  This program 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.
 *
 *  This program 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 have received a copy of the GNU General Public License
 *  along with this program (gpl-3.0.txt).
 *  see <http://www.gnu.org/licenses/>
 */

#include "SequenceFileDetector.h"
#include "LoaderFactory.h"

#include <RayPlatform/core/OperatingSystem.h>

#include <vector>
#include <string>
#include <set>
using namespace std;

SequenceFileDetector::SequenceFileDetector() {

	m_directorySeparator = '/';

#ifdef _WIN32
	m_directorySeparator = '\\';
#endif

}

// TODO: this code does not detect loops...
void SequenceFileDetector::gatherAllFiles(string & root, vector<string> & rawFiles) {

#if 0
	cout << "DEBUG SequenceFileDetector::gatherAllFiles ";
	cout << root << endl;
#endif

	// explore the directory
	if(isDirectory(root)) {

		vector<string>  entries;
		getDirectoryFiles(root, entries);

		for(vector<string>::iterator i = entries.begin() ;
				i != entries.end() ; ++i) {

			string & entry = *i;

			// skip self and parent
			if(entry == "." || entry == "..")
				continue;

			ostringstream path;
			path << root;

			if(root[root.length()-1] != m_directorySeparator)
				path << m_directorySeparator;

			path << entry;

			string aPath = path.str();

			gatherAllFiles(aPath, rawFiles);
		}

	// add the file directly...
	} else {

		rawFiles.push_back(root);
	}
}

string SequenceFileDetector::replaceString(const string & templateString, const string & oldString,
		const string & newString) {

	int position = templateString.find(oldString);

	if(position < 0)
		return templateString;

	string mutableString = templateString;

	string result = mutableString.replace(position, oldString.length(), newString);

	return result;
}

void SequenceFileDetector::detectSequenceFiles(string & directory) {

	// read files
	vector<string> rawFiles;
	gatherAllFiles(directory, rawFiles);

	vector<string> files;

	// keep supported files
	LoaderFactory factory;

	for(int i = 0 ; i < (int) rawFiles.size() ; ++ i) {

		string & file = rawFiles[i];
		if(factory.makeLoader(file) != NULL)
			files.push_back(file);
	}

	// build an index

	map<string, int> fileIndex;

	for(int i = 0 ; i < (int) files.size() ; ++i) {

		string & fileName = files[i];

		fileIndex[fileName] = i;
	}

	set<int> consumedFiles;

	/**
	 * Options for the matching algorithm
	 */
	bool enableSmartMatchingMode = true;

#ifdef BRUTE_FORCE_MATCHING
	bool enableBestHitMatchingMode = true;
	int configurationMaximumHits = 1;
	int configurationMinimumScore = 0;
	int configurationMaximumScore = 2;
#endif

	// detect pairs
	for(int i = 0 ; i < (int) files.size() ; ++i){

		if(consumedFiles.count(i) > 0)
			continue;

		// First, try to match the files using pairing rules.
		//
		// _R1_ + _R2_
		// _R2_ + _R1_
		// _1.fa + _2.fa
		// _2.fa + _1.fa

		// first, replace the _R1_ with _R2_
		string & file1 = files[i];
		string newFile = replaceString(file1, "_R1_", "_R2_");

		if(enableSmartMatchingMode && fileIndex.count(newFile) > 0
				&& newFile != file1) {

			int index2 = fileIndex[newFile];

#ifdef CONFIG_ASSERT
			assert(i != index2);
#endif // CONFIG_ASSERT

			if(consumedFiles.count(index2) == 0) {
				m_leftFiles.push_back(file1);
				m_rightFiles.push_back(newFile);
				consumedFiles.insert(i);
				consumedFiles.insert(index2);

				continue;
			}
		}

		// try to replace the _R2_ with _R1_
		file1 = files[i];
		newFile = replaceString(file1, "_R2_", "_R1_");

		if(enableSmartMatchingMode && fileIndex.count(newFile) > 0
				&& newFile != file1) {

			int index2 = fileIndex[newFile];

#ifdef CONFIG_ASSERT
			assert(i != index2);
#endif // CONFIG_ASSERT

			if(consumedFiles.count(index2) == 0) {
				m_leftFiles.push_back(newFile);
				m_rightFiles.push_back(file1);
				consumedFiles.insert(i);
				consumedFiles.insert(index2);

				continue;
			}
		}

		// replace the _1.fa with _2.fa
		file1 = files[i];
		newFile = replaceString(file1, "_1.f", "_2.f");

		if(enableSmartMatchingMode && fileIndex.count(newFile) > 0
				&& newFile != file1) {

			int index2 = fileIndex[newFile];

#ifdef CONFIG_ASSERT
			assert(i != index2);
#endif // CONFIG_ASSERT

			if(consumedFiles.count(index2) == 0) {
				m_leftFiles.push_back(file1);
				m_rightFiles.push_back(newFile);
				consumedFiles.insert(i);
				consumedFiles.insert(index2);

				continue;
			}
		}

		// try to replace the _2.fa with _1.fa
		file1 = files[i];
		newFile = replaceString(file1, "_2.f", "_1.f");

		if(enableSmartMatchingMode && fileIndex.count(newFile) > 0
				&& newFile != file1) {

			int index2 = fileIndex[newFile];

#ifdef CONFIG_ASSERT
			assert(i != index2);
#endif // CONFIG_ASSERT

			if(consumedFiles.count(index2) == 0) {

				m_leftFiles.push_back(newFile);
				m_rightFiles.push_back(file1);
				consumedFiles.insert(i);
				consumedFiles.insert(index2);

				continue;
			}
		}

		// try to do it by brute force !

#ifdef BRUTE_FORCE_MATCHING // not very good

		int bestMatch = -1;
		int bestScore = 999;
		int withBestMatch = 0;

		for(int j = 0 ; j < (int) files.size() ; ++j) {

			if(!enableBestHitMatchingMode)
				break;

			if(consumedFiles.count(j) > 0)
				continue;

			if(i == j)
				continue;

			string & file2 = files[j];

			int position1 = 0;
			int position2 = 0;

			// find the last m_directorySeparator

			int positionForFile1 = 0;
			while(positionForFile1 < (int)file1.length()) {
				if(file1[positionForFile1] == m_directorySeparator)
					position1 = positionForFile1;

				positionForFile1 ++;
			}

			int positionForFile2 = 0;
			while(positionForFile2 < (int)file2.length()) {
				if(file2[positionForFile2] == m_directorySeparator)
					position2 = positionForFile2;

				positionForFile2 ++;
			}

			// the files must be in the same directory
			if(position1 != position2)
				continue;

			// compute mismatches before the last separator

			int mismatches = 0;

			int positionBefore1 = 0;
			int positionBefore2 = 0;

			while(positionBefore1 < position1 && positionBefore2 < position2) {

				if(file1[positionBefore1] != file2[positionBefore2])
					mismatches++;

				positionBefore1 ++;
				positionBefore2 ++;
			}

			// skip because they are not in the same directory
			if(mismatches != 0)
				continue;
#if 0
			cout << "DEBUG position1 " << position1 << " position2 " << position2 << endl;
#endif
			mismatches = 0;

			while(position1 < (int)file1.length() && position2 < (int)file2.length()) {

				if(file1[position1++] != file2[position2++])
					mismatches++;
			}

			if(bestMatch < 0) {
				bestMatch = j;
				bestScore = mismatches;

				withBestMatch = 1;
			}

			if(mismatches < bestScore) {
				bestMatch = j;
				bestScore = mismatches;
				withBestMatch = 1;
			} else if(mismatches == bestScore) {

				withBestMatch ++;
			}
		}

		if(bestScore > configurationMinimumScore
				&& bestScore <= configurationMaximumScore
				&& withBestMatch <= configurationMaximumHits) {

			string & file1 = files[i];
			string & file2 = files[bestMatch];

#if 0
			cout << "DEBUG matching files " << file1 << " and " << file2 << endl;
#endif

			m_leftFiles.push_back(file1);
			m_rightFiles.push_back(file2);

			consumedFiles.insert(i);
			consumedFiles.insert(bestMatch);
		}

#endif


	}

	// detect single files
	for(int i = 0 ; i < (int) files.size() ; ++i){

		if(consumedFiles.count(i) > 0)
		continue;

		string & file = files[i];

		m_singleFiles.push_back(file);
		consumedFiles.insert(i);
	}
}

vector<string> & SequenceFileDetector::getLeftFiles() {
	return m_leftFiles;
}

vector<string> & SequenceFileDetector::getRightFiles() {

	return m_rightFiles;
}

vector<string> & SequenceFileDetector::getSingleFiles() {

	return m_singleFiles;
}