File: SearchDirectory.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 (729 lines) | stat: -rw-r--r-- 15,527 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*
    Ray -- Parallel genome assemblies for parallel DNA sequencing
    Copyright (C) 2012, 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/>

*/

//#define CONFIG_SEARCH_DIR_VERBOSE

#include "SearchDirectory.h"

#include <assert.h>
#include <string.h>

/* 
 * I am not sure that dirent.h is available on Microsoft(R) Windows(R).
 * Anyway, at this point, you are better off installing Cygwin in Windows(R)
 * to acquire a POSIX compatibility layer on this otherwise not-POSIX 
 * system
 */
#include <dirent.h> /* for opendir, readdir and closedir */

#include <iostream>
#include <fstream>
#include <sstream>
#include <code/Mock/common_functions.h> /* for wordId */
using namespace std;


void SearchDirectory::constructor(string path){
	m_hasBufferedLine=false;
	strcpy(m_bufferedLine,"");

	m_path=path;

	// list entries
	readDirectory();

	// initialise empty counts
	for(int i=0;i<(int)m_files.size();i++){
		m_counts.push_back(0);
	}

	m_hasFile=false;

	m_currentFileStream=NULL;

}

string*SearchDirectory::getFileName(int j){

	return &m_files[j];
}

/*
 * \see http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c
 */
void SearchDirectory::readDirectory(){
	struct dirent *ent;
	DIR*dir = opendir (m_path.c_str());
	if(dir != NULL){

		while((ent = readdir (dir)) != NULL){
			string fileName=ent->d_name;

			if(fileName.find(".fasta")==string::npos)
				continue;

			if(fileName==".fasta")
				continue;

			m_files.push_back(fileName);
		}
		closedir (dir);

	} else {
		/* could not open directory */
		cout<<"Warning, directory "<<m_path<<" is invalid."<<endl;
	}
}

int SearchDirectory::getCount(int i){
	#ifdef CONFIG_ASSERT
	if(i>=(int)m_counts.size()){
		cout<<"Error, file= "<<i<<" but size= "<<m_counts.size()<<endl;
		cout<<"Directory= "<<getDirectoryName()<<endl;

	}

	assert(i<(int)m_counts.size());
	#endif

	return m_counts[i];
}

void SearchDirectory::countEntriesInFile(int fileNumber){
	#ifdef CONFIG_ASSERT
	assert(fileNumber<(int)m_files.size());
	#endif

	int count=0;
	ostringstream file;
	file<<m_path<<"/"<<m_files[fileNumber];

	ifstream f(file.str().c_str());

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"Opening "<<file.str()<<endl;
	#endif

	char line[10000];
	
	if(!f){
		cout<<"Error, cannot open "<<file.str()<<endl;
		f.close();
		return;
	}

	while(!f.eof()){
		f.getline(line,10000);

		if(lineIsSequenceHeader(line)){
			count++;
		}
	}

	m_counts[fileNumber]=count;

	f.close();
}

string* SearchDirectory::getDirectoryName(){
	return &m_path;
}

int SearchDirectory::getSize(){
	return m_files.size();
}

void SearchDirectory::setCount(int file,int count){
	
	#ifdef CONFIG_ASSERT
	int current=m_counts[file];
	if(current!=0){
		assert(current==count);
	}
	#endif

	m_counts[file]=count;
}

void SearchDirectory::createSequenceReader(int file,int sequence,int kmerLength){

	#ifdef CONFIG_ASSERT
	assert(file<(int)m_files.size());
	assert(sequence<m_counts[file]);
	#endif

	// close the file and open the good one
	if(m_hasFile && m_currentFile!=file){

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"Closing current file"<<endl;
		#endif

		#ifdef CONFIG_ASSERT
		//assert(m_currentFileStream.is_open());
		#endif

		fclose(m_currentFileStream);
		m_currentFileStream=NULL;
		m_hasFile=false;

		#ifdef CONFIG_ASSERT
		//assert(!m_currentFileStream.is_open());
		assert(m_currentFileStream==NULL);
		#endif

	}

	// open the file
	if(!m_hasFile){

		#ifdef CONFIG_ASSERT
		assert(m_currentFileStream==NULL);
		#endif

		ostringstream fileName;
		fileName<<m_path<<"/"<<m_files[file];

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"Opening current file "<<fileName.str()<<endl;
		cout<<"File has "<<getCount(file)<<" sequences"<<endl;
		#endif

		m_currentFileStream=fopen(fileName.str().c_str(),"r");
		m_currentFile=file;
	
		// we set it to -1 to be able to pick up 0
		m_currentSequence=-1;// start at the beginning

		m_hasFile=true;

		#ifdef CONFIG_ASSERT
		//assert(m_currentFileStream.is_open());
		assert(m_currentFileStream!=NULL);
		assert(m_hasFile);
		#endif
	}

	#ifdef CONFIG_ASSERT
	assert(m_hasFile);
	assert(m_currentFile==file);
	assert(m_currentFileStream!=NULL);
	if(m_currentSequence>=sequence){
		cout<<"m_currentSequence: "<<m_currentSequence<<" sequence: "<<sequence<<endl;
	}
	
	// be need to be at least before the one we want to 
	// pick it up
	assert(m_currentSequence < sequence);
	#endif

	// here we want to advance to the sequence 
	
	while(m_currentSequence<sequence && !feof(m_currentFileStream)){
		
		char line[CONFIG_COLORED_LINE_MAX_LENGTH];
		strcpy(line,"");

		readLineFromFile(line,CONFIG_COLORED_LINE_MAX_LENGTH);

		if(lineIsSequenceHeader(line)){

			m_currentSequence++;

			// we have the header
			strcpy(m_currentSequenceHeader,line);
		}
	}

	#ifdef CONFIG_ASSERT
	if(m_currentSequence!=sequence){
		cout<<"Expected: "<<sequence<<" Actual: "<<m_currentSequence<<endl;
	}
	assert(m_currentSequence==sequence);
	#endif

	strcpy(m_currentSequenceBuffer,"");
	m_currentSequencePosition=0;
	m_noMoreSequence=false;

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"Ready to process sequence "<<sequence<<endl;
	#endif


	/* at this point, we are ready to read. but first, we want to get the number of k-mers to
 * pump */

	m_currentSequenceNumberOfAvailableKmers=0;

	#ifdef CONFIG_ASSERT
	assert(strlen(m_currentSequenceHeader)>0);
	assert(m_currentSequenceHeader[0]=='>');
	assert(strlen(m_currentSequenceBuffer)==0);
	assert(!m_noMoreSequence);
	assert(m_currentFileStream!=NULL);
	#endif
}

void SearchDirectory::readLineFromFile(char*line,int length){
	// use the buffer
	if(m_hasBufferedLine){
		#ifdef CONFIG_ASSERT
		assert(m_hasBufferedLine);
		#endif

		strcpy(line,m_bufferedLine);
		strcpy(m_bufferedLine,"");
		m_hasBufferedLine=false;

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"Using buffered line!"<<endl;
		#endif

		#ifdef CONFIG_ASSERT
		assert(!m_hasBufferedLine);
		#endif

		return;
	}
	
	#ifdef CONFIG_ASSERT
	assert(strlen(m_bufferedLine)==0);
	assert(!m_hasBufferedLine);
	assert(m_currentFileStream!=NULL);
	#endif

	#ifdef CONFIG_ASSERT
	assert(line!=NULL);
	assert(length>0);
	assert(m_currentFileStream!=NULL);
	#endif

	fgets(line,length,m_currentFileStream);

	// remove the new line symbol, if any
	
	if(line[strlen(line)-1]=='\n'){
		line[strlen(line)-1]='\0';
	}
}

int SearchDirectory::getCurrentSequenceLengthInKmers(){

	return m_currentSequenceNumberOfAvailableKmers;
}

bool SearchDirectory::hasNextKmer(int kmerLength){

	#ifdef CONFIG_ASSERT
	assert(kmerLength>0);
	#endif

	// attempt to load some data
	if( !m_noMoreSequence  && ((int)strlen(m_currentSequenceBuffer) - m_currentSequencePosition) < kmerLength
		){

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"hasNextKmer calls loadSomeSequence()"<<endl;

		#endif

		// load some more
		loadSomeSequence();

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"hasNextKmer returns true kmer= "<<kmerLength<<" position= "<<m_currentSequencePosition;
		cout<<" buffer: "<<strlen(m_currentSequenceBuffer)<<endl;
		#endif

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"after"<<endl;
		#endif
	}

	// if we still don't have enough data
	// we are done
	if(((int)strlen(m_currentSequenceBuffer) - m_currentSequencePosition) <kmerLength){

		// close the file and reset the thing
		// if the sequence is the last one
		if(m_hasFile && m_currentSequence == getCount(m_currentFile)-1){

			#ifdef CONFIG_SEARCH_DIR_VERBOSE
			cout<<"Closing file in hasNextKmer"<<endl;
			#endif

			#ifdef CONFIG_ASSERT
			//assert(m_currentFileStream.is_open());
			assert(m_currentFileStream!=NULL);
			#endif

			// remove the current file.
			fclose(m_currentFileStream);
			m_hasFile=false;
			m_currentFileStream=NULL;

			#ifdef CONFIG_ASSERT
			//assert(!m_currentFileStream.is_open());
			#endif

			#ifdef CONFIG_ASSERT
			assert(m_hasFile==false);
			assert(m_currentFileStream==NULL);
			#endif
		}

		#ifdef CONFIG_SEARCH_DIR_VERBOSE
		cout<<"don't have more k-mers"<<endl;
		#endif

		return false;
	}

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"hasNextKmer returns true kmer= "<<kmerLength<<" position= "<<m_currentSequencePosition;
	cout<<" buffer: "<<strlen(m_currentSequenceBuffer)<<endl;
	#endif

	return true;
}

void SearchDirectory::iterateToNextKmer(){

	m_currentSequencePosition++;
}

void SearchDirectory::getNextKmer(int kmerLength,Kmer*kmer){

	#ifdef CONFIG_ASSERT
	assert(kmerLength>0);
	#endif

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"getNextKmer m_currentSequencePosition= "<<m_currentSequencePosition<<endl;
	#endif

	m_hasN=false;

	char sequenceBuffer[400];
	memcpy(sequenceBuffer,m_currentSequenceBuffer+m_currentSequencePosition,kmerLength);
	sequenceBuffer[kmerLength]='\0';

	#ifdef CONFIG_ASSERT
	assert((int)strlen(sequenceBuffer)==kmerLength);
	#endif

	// convert bases to upper case
	for(int i=0;i<kmerLength;i++){
		char nucleotide=sequenceBuffer[i];
		switch (nucleotide){
			case SYMBOL_LOWER_A:
				nucleotide=SYMBOL_A;
				break;
			case SYMBOL_LOWER_T:
				nucleotide=SYMBOL_T;
				break;
			case SYMBOL_LOWER_C:
				nucleotide=SYMBOL_C;
				break;
			case SYMBOL_LOWER_G:
				nucleotide=SYMBOL_G;
				break;
		}

		if(nucleotide!=SYMBOL_A && nucleotide!=SYMBOL_T && nucleotide!=SYMBOL_C
			&& nucleotide!=SYMBOL_G){

			m_hasN=true;
		}

		sequenceBuffer[i]=nucleotide;
	}

	*kmer=wordId(sequenceBuffer);

}

bool SearchDirectory::kmerContainsN(){

	return m_hasN;
}

string SearchDirectory::getCurrentSequenceName(){

	int maximumLength=64;

	string currentSequenceHeader=m_currentSequenceHeader;

	// per default, just returns the header without any parsing...
	#ifdef CONFIG_USE_NCBI_HEADERS
	// if '|' are there, this is the NCBI format
	// skip 4 '|' and return the rest
	size_t position=currentSequenceHeader.find_last_of('|');

	if(position!=string::npos){
		if(position+2<currentSequenceHeader.length()){
			position+=2;
		}

		#ifdef CONFIG_ASSERT
		if(position>=currentSequenceHeader.length()){
			cout<<"Header= "<<currentSequenceHeader<<endl;
		}
		assert(position<currentSequenceHeader.length());
		#endif

		//skip the '|' and the ' '
		return filterName(currentSequenceHeader.substr(position,maximumLength));
	}

	#endif

	// otherwise
	// remove the '>' and keep only maximumLength characters.
	//cout<<"code 108"<<endl;

	#ifdef CONFIG_ASSERT
	if(currentSequenceHeader.length()==0){
		cout<<"currentSequenceHeader is empty, fatal"<<endl;
	}
	assert(currentSequenceHeader.length()>0);
	#endif

	return filterName(currentSequenceHeader.substr(1,maximumLength));
}

// load in chunks
void SearchDirectory::loadSomeSequence(){

	// nothing to load
	if(m_noMoreSequence)
		return;

	#ifdef CONFIG_ASSERT
	assert(m_currentFileStream!=NULL);
	assert(!m_noMoreSequence);
	#endif

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"Loading some more bits"<<endl;
	#endif

	char newContent[CONFIG_COLORED_LINE_MAX_LENGTH];
	int contentPosition=0;
	strcpy(newContent,"");
	
	// copy old content
	// discard already processed content -- the bytes 
	// before m_currentSequencePosition that is
	//cout<<"code 203"<<endl;
	
	int theLength=strlen(m_currentSequenceBuffer);

	while(m_currentSequencePosition<theLength){

		#ifdef CONFIG_ASSERT
		assert(theLength>0);
		#endif

		newContent[contentPosition++]=m_currentSequenceBuffer[m_currentSequencePosition++];
	}

	newContent[contentPosition]='\0';

	// load some lines
	int lines=100;
	int loaded=0;
	int i=0;

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"after copying remainings, newContent is "<<strlen(newContent)<<endl;
	#endif

	while(!feof(m_currentFileStream) && (i++ < lines)){

		char line[CONFIG_COLORED_LINE_MAX_LENGTH];
		strcpy(line,"");

		readLineFromFile(line,CONFIG_COLORED_LINE_MAX_LENGTH);

		// we reached the next sequence
		// rollback to where we were before
		if(lineIsSequenceHeader(line)){

			#ifdef CONFIG_ASSERT
			assert(strlen(m_bufferedLine)==0);
			assert(!m_hasBufferedLine);
			#endif

			strcpy(m_bufferedLine,line);

			m_noMoreSequence=true;

			m_hasBufferedLine=true;

			#ifdef CONFIG_SEARCH_DIR_VERBOSE
			cout<<"THe line is a header, buffering"<<endl;
			#endif

			break; // we don't add this line and we stop here
		}else{

			strcat(newContent,line);
			loaded++;
		}
	}

	/* there is no more sequence if the end was reached... */
	if(feof(m_currentFileStream)){
		m_noMoreSequence=true;
	}

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"Loaded "<<loaded<<" lines in working buffer"<<endl;
	#endif

	// set the new content.
	m_currentSequencePosition=0;
	strcpy(m_currentSequenceBuffer,newContent);

	#ifdef CONFIG_ASSERT
	assert(m_currentSequencePosition==0);
	#endif

	#ifdef CONFIG_SEARCH_DIR_VERBOSE
	cout<<"new m_currentSequenceBuffer has "<<strlen(m_currentSequenceBuffer)<<" nucleotides"<<endl;
	#endif
}

bool SearchDirectory::lineIsSequenceHeader(char*line){

	return strlen(line)>0 && line[0]=='>';
}

/**
 * \see http://www.ncbi.nlm.nih.gov/RefSeq/RSfaq.html
 */
bool SearchDirectory::hasCurrentSequenceIdentifier(){

	string currentSequenceHeader=m_currentSequenceHeader;

	//cout<<"Identifier= "<<m_currentSequenceHeader<<endl;

	// this means that the sequences are not genome sequences
	// but are genes or something like that.
	//if(m_currentSequenceHeader.find("|:") != string::npos){
		//cout<<"Contains '|:'"<<endl;

		//return false;
	//}

	if(currentSequenceHeader.find(">gi|") == string::npos)
		return false;

	if(currentSequenceHeader.find(">gi|")==0)
		return true;

	return false;
}

PhysicalKmerColor SearchDirectory::getCurrentSequenceIdentifier(){
	int count=0;
	int i=0;

	string currentSequenceHeader=m_currentSequenceHeader;

	while(i<(int)currentSequenceHeader.length() && count<2){
		if(currentSequenceHeader[i]=='|')
			count++;
		
		
		i++;
	}

	if(count!=2){
		return DUMMY_IDENTIFIER; // return a dummy identifier
	}

	// >gi|1234|
	//
	// 0123456789
	//
	// 9-4-1 = 4
	//
	string content=currentSequenceHeader.substr(4,i-4-1);

	istringstream aStream;
	aStream.str(content);

	PhysicalKmerColor identifier;

	aStream>>identifier;

	return identifier;
}

bool SearchDirectory::hasDirectory(int file){
	return m_createdDirectories.count(file)>0;
}

void SearchDirectory::setCreatedDirectory(int file){
	m_createdDirectories.insert(file);
}

string SearchDirectory::filterName(string a){
	for(int i=0;i<(int)a.length();i++){
		if(a[i]=='<' || a[i]=='>' || a[i]=='&'){
			a[i]='_';
		}
	}

	return a;
}

/* 
 * >EMBL_CDS:CBW26015 CBW26015.1 */
bool SearchDirectory::hasIdentifier_EMBL_CDS(){
	
	string currentSequenceHeader=m_currentSequenceHeader;

	if(currentSequenceHeader.find(">EMBL_CDS:") == 0){
		return true;
	}

	return false;
}

PhysicalKmerColor SearchDirectory::getIdentifier_EMBL_CDS(){

	#ifdef CONFIG_ASSERT
	assert(hasIdentifier_EMBL_CDS());
	#endif

	/* >EMBL_CDS:CBW26015 CBW26015.1 */

	string header=m_currentSequenceHeader;
	string token=header.substr(10,8);

	return m_encoder.getEncoded_EMBL_CDS(token.c_str());
}