File: gnGBKSource.cpp

package info (click to toggle)
libgenome 1.3.1-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,156 kB
  • ctags: 1,212
  • sloc: cpp: 10,910; sh: 8,264; makefile: 79
file content (961 lines) | stat: -rw-r--r-- 31,743 bytes parent folder | download | duplicates (8)
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/////////////////////////////////////////////////////////////////////////////
// File:            gnGBKSource.h
// Purpose:         Implements gnBaseSource for GenBank sequences
// Description:     
// Changes:        
// Version:         libGenome 0.5.1 
// Author:          Aaron Darling 
// Modified by:     
// Copyright:       (c) Aaron Darling 
// Licenses:        See COPYING file for details
/////////////////////////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "libGenome/gnFilter.h"
#include "libGenome/gnFeature.h"
#include "libGenome/gnGBKSource.h"
#include "libGenome/gnSourceSpec.h"
#include "libGenome/gnSourceHeader.h"
#include "libGenome/gnSourceQualifier.h"
#include "libGenome/gnLocation.h"
#include "libGenome/gnStringTools.h"
#include "libGenome/gnDebug.h"
#include "libGenome/gnStringQualifier.h"
#include <string>
#include <cstring>


using namespace std;
namespace genome {


gnGBKSource::gnGBKSource()
{
	m_openString = "";
	m_pFilter = gnFilter::proteinSeqFilter();
	if(m_pFilter == NULL){
		DebugMsg("Error using static sequence filters.");
	}
}
gnGBKSource::gnGBKSource( const gnGBKSource& s ) : gnFileSource(s)
{
	vector< gnFileContig* >::const_iterator iter = s.m_contigList.begin();
	for( ; iter != s.m_contigList.end(); ++iter )
	{
		m_contigList.push_back( (*iter)->Clone() );
	}
}
gnGBKSource::~gnGBKSource()
{
	m_ifstream.close();
	vector< gnFileContig* >::iterator iter = m_contigList.begin();
	for( ; iter != m_contigList.end(); ++iter )
	{
		gnFileContig* fg = *iter;
		*iter = 0;
		delete fg;
	}
}
boolean gnGBKSource::HasContig( const string& name ) const
{
	for(uint32 i = 0 ; i <= m_contigList.size(); i++ )
	{
		if( name == m_contigList[i]->GetName() )
			return true;
	}
	return false;
}
uint32 gnGBKSource::GetContigID( const string& name ) const
{
	for(uint32 i = 0 ; i <= m_contigList.size(); i++ )
	{
		if( name == m_contigList[i]->GetName() )
			return i;
	}
	return ALL_CONTIGS;
}
string gnGBKSource::GetContigName( const uint32 i ) const
{
	if( i < m_contigList.size() )
	{
		return m_contigList[i]->GetName();
	}
	return "";
}
gnSeqI gnGBKSource::GetContigSeqLength( const uint32 i ) const
{
	if( i == ALL_CONTIGS)
		return m_spec->GetLength();
	if( i < m_contigList.size() )
	{
		return m_contigList[i]->GetSeqLength();
	}
	return GNSEQI_ERROR;
}

boolean gnGBKSource::SeqRead( const gnSeqI start, char* buf, gnSeqI& bufLen, const uint32 contigI )
{
	boolean result = false;
#pragma omp critical
{
	result = SeqReadImpl( start, buf, bufLen, contigI );
}
	return result;
}

boolean gnGBKSource::SeqReadImpl( const gnSeqI start, char* buf, gnSeqI& bufLen, const uint32 contigI ){

	uint64 startPos = 0;
	uint64 readableBytes = 0;
	if( !SeqSeek( start, contigI, startPos, readableBytes ) )
	{
		bufLen = 0;
		return false;
	}
	
	if( contigI == ALL_CONTIGS )
	{
		uint32 curLen = 0;
		uint64 bytesRead = 0;
		while (curLen < bufLen)
		{
//SeqSeek to start, Figure out how much can be read before SeqSeeking again.
			if(readableBytes <= 0)	//Look out for zero length contigs!  IMPLEMENT ME
				if( !SeqSeek( start + curLen, contigI, startPos, readableBytes ) ){
					bufLen = curLen;
					return true;
				}
			//readLen is the amount to read on this pass
			uint64 readLen = (bufLen - curLen) < readableBytes ? (bufLen - curLen) : readableBytes;	
			Array<gnSeqC> array_buf( readLen );
			gnSeqC* tmpBuf = array_buf.data;

			// read chars and filter
			m_ifstream.read(tmpBuf, readLen);
			uint64 gc = m_ifstream.gcount();
			bytesRead += gc;
			readableBytes -= gc;
			for(uint32 i=0; i < gc; i++){
				if( m_pFilter->IsValid(tmpBuf[i]) ){
					buf[curLen] = tmpBuf[i];
					curLen++;
				}
			}
			if(m_ifstream.eof()){	//we hit the end of the file.  bail out.
				m_ifstream.clear();
				bufLen = curLen;
				return true;
			}
		}
		bufLen = curLen;
	}
	else if( contigI < m_contigList.size() )
	{
		uint32 curLen = 0;
		//check to see if the buffer is bigger than the contig.  if so truncate it.
		gnSeqI contigSize = m_contigList[contigI]->GetSeqLength();
		bufLen = bufLen < contigSize ? bufLen : contigSize;
		while (curLen < bufLen)
		{
			uint64 readLen = bufLen - curLen;	//the amount to read on this pass
			Array<gnSeqC> array_buf( readLen );
			gnSeqC* tmpBuf = array_buf.data;

			// read chars and filter
			m_ifstream.read(tmpBuf, readLen);
			uint64 gc = m_ifstream.gcount();
//			cout << "Read " << gc << " chars from " << m_openString << "\n";
//			cout << "Checking character validity on: " << tmpBuf << "\n";
			for(uint32 i=0; i < gc; i++){
				if( m_pFilter->IsValid(tmpBuf[i]) ){
					buf[curLen] = tmpBuf[i];
					curLen++;
				}
			}
			if(m_ifstream.eof()){	//we hit the end of the file.  bail out.
				m_ifstream.clear();
				bufLen = curLen;
				return true;
			}
		}
		bufLen = curLen;
	}
	return true;

}
// private:
// figures out which contig the sequence starts at then calls SeqStartPos to get the offset within that contig
// returns startPos, the file offset where the sequence starts
// returns true if successful, false otherwise
boolean gnGBKSource::SeqSeek( const gnSeqI start, const uint32& contigI, uint64& startPos, uint64& readableBytes )
{
	if( contigI == ALL_CONTIGS )
	{
		// find first contig
		gnSeqI curIndex = 0;
		vector< gnFileContig* >::iterator iter = m_contigList.begin();
		for( ; iter != m_contigList.end(); ++iter )
		{
			uint64 len = (*iter)->GetSeqLength();
			if( (curIndex + len) > start )
				break;
			curIndex += len;
		}
		if( iter == m_contigList.end() )
			return false;
		// seek to start
		gnSeqI startIndex = start - curIndex;  //startIndex is starting pos. within the contig
		return SeqStartPos( startIndex, *(*iter), startPos, readableBytes );
	}
	else if( contigI < m_contigList.size() )
	{
		return SeqStartPos( start, *(m_contigList[contigI]), startPos, readableBytes );
	}
	return false;
}
//Returns startPos, the file offset where the sequence starts.
boolean gnGBKSource::SeqStartPos( const gnSeqI start, gnFileContig& contig, uint64& startPos, uint64& readableBytes )
{
	readableBytes = 0;
	uint32 curLen = 0;
	//seek to the file offset where the contig starts
	startPos = contig.GetSectStartEnd(gnContigSequence).first;	//set startPos to start where the contig starts
	m_ifstream.seekg( startPos, ios::beg );
	if( m_ifstream.eof() ){
		ErrorMsg("ERROR in gnGBKSource::Incorrect contig start position, End of file reached!\n");
		return false;
	}
	while( true )
	{
		  // READ the rest of the contig skipping over invalid characters until we get to the starting base pair.
		  // startPos will contain the file offset with the starting base pair
		uint32 tmpbufsize = contig.GetSectStartEnd(gnContigSequence).second - startPos;
		if(tmpbufsize == 0){
			ErrorMsg("ERROR in gnGBKSource: stored contig size is incorrect.");
			return false;
		}
		uint64 startOffset = start;
		if(contig.HasRepeatSeqGap()){	//check for sequence integrity
			startOffset += (9 + m_newlineSize) * (start / 60 + 1) + start / 10 + 1;
			if( m_newlineSize == 2 )	// test compensation for strange bug
				startOffset--;
			startPos+=startOffset;
			m_ifstream.seekg(startPos , ios::beg);
			readableBytes = contig.GetSectStartEnd(gnContigSequence).second - startPos;
			return true;
		}

		//sequence is corrupt.  read in base by base
		tmpbufsize = tmpbufsize < BUFFER_SIZE ? tmpbufsize : BUFFER_SIZE;  //read in the smaller of the two.
		Array<char> array_buf( tmpbufsize );
		char* tmpbuf = array_buf.data;

		m_ifstream.read( tmpbuf, tmpbufsize );
		if( m_ifstream.eof() ){
			ErrorMsg("ERROR in gnGBKSource::Read End of file reached!\n");
			return false;
		}
		for( uint32 i=0; i < tmpbufsize; ++i ){
			if( m_pFilter->IsValid(tmpbuf[i]) ){
				if( curLen >= start ){ //stop when we reach the starting offset within this contig
					startPos += i;
					m_ifstream.seekg( startPos, ios::beg );  //seek to startPos
					readableBytes = contig.GetSectStartEnd(gnContigSequence).second - startPos;
					return true;
				}
				++curLen;  //each time we read a valid b.p., increment the sequence length
			}
		}
		startPos += tmpbufsize;
	}
	return true;
}

void gnGBKSource::FormatString(string& data, uint32 offset, uint32 width){
	//first remove newlines and corresponding whitespace
	string::size_type newline_loc = data.find_first_of('\n', 0);
	while(newline_loc != string::npos){
		if(data[newline_loc-1] == '\r')
			newline_loc--;
		string::size_type text_loc = newline_loc;
		while((data[text_loc] == ' ') ||(data[text_loc] == '	')||(data[text_loc] == '\n')||(data[text_loc] == '\r')){
			text_loc++;
			if(text_loc+1 == data.length())
				break;
		}
		data = (data.substr(0, newline_loc) + " " + data.substr(text_loc));
		newline_loc = data.find_first_of('\n', 0);
	}
	//now reformat with newlines and whitespace, observing word boundaries...
	string output_string = "";
	for(uint32 charI = 0; charI < data.length();){
		//get the substring to append and increment charI
		string::size_type base_loc = charI;
		string append_string;
		while(base_loc - charI <= width){
			string::size_type space_loc = data.find_first_of(' ', base_loc+1);
			if(space_loc - charI < width)
				base_loc = space_loc;
			else if(base_loc == charI){
				//word is too big for one line.  split it.
				append_string = data.substr(charI, width);
				charI+=width;
			}else{
				append_string = data.substr(charI, base_loc - charI);
				charI = base_loc;
			}
		}
		output_string += string(offset, ' ') + append_string;
		if(charI + width < data.length())
			output_string += "\r\n";
	}
	data = output_string;
}

template< class SubSpec >
void WriteHeader(gnMultiSpec< SubSpec >* spec, const string& hdr, ofstream& m_ofstream) {
	gnBaseHeader* gpbh = NULL;
	uint32 header_index = 0;
	try{
		do{
			gpbh = spec->GetHeader(hdr, header_index);
			m_ofstream << gpbh->GetHeader();
			header_index++;
		}while(gpbh != NULL);
	}catch(gnException& gne){}
}

boolean gnGBKSource::Write(gnSequence& seq, const string& filename){
	ofstream m_ofstream(filename.c_str(), ios::out | ios::binary);
	if(!m_ofstream.is_open())
		return false;

	string newline = "\r\n";
	gnGenomeSpec* spec = seq.GetSpec();

	// output general file header first if one exists.
	if(spec->GetHeaderListLength() == 1){
		gnBaseHeader *gpbh = spec->GetHeader(0);
		string name = gpbh->GetHeaderName();
		//IMPLEMENT ME!  Is platform specific newline substitution necessary?
		if(string::npos != name.find(".SEQ")){
			string header = gpbh->GetHeader();
			m_ofstream << header;
		}
	}
	// TODO:  Figure out where the buffer overflow is and reduce
	// this back to BUFFER_SIZE -- overflow appears not to corrupt
	// sequence data
	Array<gnSeqC> array_buf( 2 * BUFFER_SIZE );
	gnSeqC *bases = array_buf.data;

	for(uint32 specI = 0; specI < spec->GetSpecListLength(); specI++){
		gnFragmentSpec* subSpec = spec->GetSpec(specI);
		
		//write out contig headers.  start with LOCUS...
		m_ofstream << "LOCUS       ";
		//write Locus Name
		string contigName = subSpec->GetName();
		if(contigName.length() > SEQ_LOCUS_NAME_LENGTH)
			contigName = contigName.substr(0, SEQ_LOCUS_NAME_LENGTH);
		uint32 filler_size = SEQ_LOCUS_NAME_LENGTH - contigName.length();
		m_ofstream << contigName << string(filler_size, ' ');
		//write Locus Length
		string length_string = uintToString(subSpec->GetLength());
		filler_size = SEQ_LOCUS_SIZE_LENGTH - length_string.size();
		m_ofstream << string(filler_size, ' ') << length_string << " bp ";
		//write dnatype
		string dnatype = string(SEQ_LOCUS_DNATYPE_LENGTH, ' ');
		uint32 head_look_i = 0;
		gnBaseHeader* gpbh = NULL;
		try{
			gpbh = subSpec->GetHeader("LOCUS", head_look_i);
		}catch(gnException& gne){}
		if( gpbh != NULL )
			dnatype = gpbh->GetHeader().substr(SEQ_LOCUS_DNATYPE_OFFSET, SEQ_LOCUS_DNATYPE_LENGTH);
		m_ofstream << dnatype << string(2, ' ');
		//write circularity
		string circular = subSpec->IsCircular() ? string("circular  ") : string(10, ' ');
		m_ofstream << circular;
		//write division code
		string division = string(SEQ_LOCUS_DIVCODE_LENGTH, ' ');
		if(gpbh != NULL)
			division = gpbh->GetHeader().substr(SEQ_LOCUS_DIVCODE_OFFSET, SEQ_LOCUS_DIVCODE_LENGTH);
		m_ofstream << division;
		//write date -- IMPLEMENT ME!  format the real date to spec! dd-mmm-yyyy
		string date = string(SEQ_LOCUS_DATE_LENGTH, ' ');
		if(gpbh != NULL)
			date = gpbh->GetHeader().substr(SEQ_LOCUS_DATE_OFFSET, SEQ_LOCUS_DATE_LENGTH);
		m_ofstream << string(7, ' ') << date << "\r\n";
		
		//write out the rest of the headers if they were supplied!
		WriteHeader(subSpec, "DEFINITION", m_ofstream);
		WriteHeader(subSpec, "ACCESSION", m_ofstream);
		WriteHeader(subSpec, "VERSION", m_ofstream);
		WriteHeader(subSpec, "KEYWORDS", m_ofstream);
		WriteHeader(subSpec, "SEGMENT", m_ofstream);
		WriteHeader(subSpec, "SOURCE", m_ofstream);
		WriteHeader(subSpec, "REFERENCE", m_ofstream);
		WriteHeader(subSpec, "COMMENT", m_ofstream);

		//write out feature table!
		m_ofstream << "FEATURES             Location/Qualifiers" << "\r\n";
		for(uint32 featureI = 0; featureI < subSpec->GetFeatureListLength(); featureI++){
			//write a feature tag
			gnBaseFeature *gpmf = subSpec->GetFeature(featureI);
			string featureName = gpmf->GetName();
			m_ofstream << string(SEQ_SUBTAG_COLUMN, ' ') << featureName;
			m_ofstream << string(SEQ_FEATURE_LOC_OFFSET - featureName.length() - SEQ_SUBTAG_COLUMN, ' ');
			//ready to output location b.s.
			uint32 location_count = gpmf->GetLocationListLength();
			uint32 line_pos = SEQ_FEATURE_LOC_OFFSET;
			uint32 parenthesis_count = 0;
			if(location_count > 1){
				m_ofstream << "join(";
				line_pos += 5;
				parenthesis_count++;
			}
			gnLocation::gnLocationType loc_type = gpmf->GetLocationType();
			switch(loc_type){
				case gnLocation::LT_Standard:
					break;
				case gnLocation::LT_Complement:
					m_ofstream << "complement(";
					line_pos += 11;
					parenthesis_count++;
					break;
				case gnLocation::LT_Order:
					m_ofstream << "order(";
					line_pos += 6;
					parenthesis_count++;
					break;
				case gnLocation::LT_Group:
					m_ofstream << "group(";
					parenthesis_count++;
					line_pos += 6;
					break;
				case gnLocation::LT_OneOf:
					m_ofstream << "one-of(";
					parenthesis_count++;
					line_pos += 7;
					break;				
				default:
					break;
			}
			//create the location string, then see if it will fit on the line
			string location;
			for(uint32 locationI = 0; locationI < location_count; locationI++){
				gnLocation gpl = gpmf->GetLocation(locationI);
				if(gpl.IsStartBoundLonger())
					location += ">";
				if(gpl.IsStartBoundShorter())
					location += "<";
				location += uintToString(gpl.GetStart());
				gnSeqI end_loc = gpl.GetEnd();
				if(end_loc != 0){
					switch(gpl.GetType()){
						case gnLocation::LT_BetweenBases:
							location += "^";
							break;
						case gnLocation::LT_OneOf:
							location += ".";
							break;
						default:
							location += "..";
							break;
					}
					if(gpl.IsEndBoundShorter())
						location += "<";
					if(gpl.IsEndBoundLonger())
						location += ">";
					location+= uintToString(end_loc);
				}
				if(locationI +1 < location_count)
					location += ",";
				else{	//append necessary parenthesis
					for(;parenthesis_count > 0; parenthesis_count--)
						location += ")";
				}
				//put it on this line if it fits.  otherwise make a new line.
				if(line_pos + location.length() < SEQ_COLUMN_WIDTH){
					m_ofstream << location;
					line_pos += location.length();
				}else{
					m_ofstream << "\r\n" << string(SEQ_FEATURE_LOC_OFFSET, ' ') << location;
					line_pos = SEQ_FEATURE_LOC_OFFSET + location.length();
				}
				location = "";
			}
			m_ofstream << "\r\n";
			//now output qualifiers!  yaay!
			//god damn this is a big ugly piece of code.
			
			uint32 qualifier_count = gpmf->GetQualifierListLength();
			for(uint32 qualifierI = 0; qualifierI < qualifier_count; qualifierI++){
				m_ofstream << string(SEQ_FEATURE_LOC_OFFSET, ' ');
				gnBaseQualifier* qualifier = gpmf->GetQualifier(qualifierI);
				m_ofstream << "/" << qualifier->GetName() << "=";
				//IMPLEMENT ME! do a better word wrap on this bitch.
				string qually = string(qualifier->GetValue());
//				FormatString(qually, SEQ_FEATURE_LOC_OFFSET, 80 - SEQ_FEATURE_LOC_OFFSET);
//				qually = qually.substr(SEQ_FEATURE_LOC_OFFSET);
				m_ofstream << qually << "\r\n";
			}
			if(gpmf != NULL)
				delete gpmf;
		}
		
		//get information about the sequence we're writing out.
		gnSeqI readOffset = seq.contigStart(specI);
		gnSeqI readLength = seq.contigLength(specI);

		//finally - output base count and origin
		m_ofstream << "BASE COUNT ";
		gnSeqI a_count=0, c_count=0, g_count=0, t_count=0, other_count=0;
		gnSeqI countLen = readLength + readOffset;
		for(gnSeqI countI = readOffset; countI < countLen;){
			gnSeqI writeLen = countLen - countI < BUFFER_SIZE ? countLen - countI : BUFFER_SIZE;
			if(!seq.ToArray(bases, writeLen, countI))
				return false;
			gnSeqI a, c, g, t, other;
			BaseCount(string(bases, writeLen), a, c, g, t, other);
			a_count += a;
			c_count += c;
			g_count += g;
			t_count += t;
			other_count += other;
			countI += writeLen;
		}
		m_ofstream << uintToString(a_count) << " a ";
		m_ofstream << uintToString(c_count) << " c ";
		m_ofstream << uintToString(g_count) << " g ";
		m_ofstream << uintToString(t_count) << " t ";
		m_ofstream << uintToString(other_count) << " others" << "\r\n";

		string origin = "ORIGIN\r\n";
		head_look_i = 0;
		try{
			gpbh = subSpec->GetHeader("ORIGIN", head_look_i);
			origin = gpbh->GetHeader();
			m_ofstream << origin;
		}catch(gnException& gne){
			m_ofstream << "ORIGIN" << endl;
		}
		//write out the sequence
		gnSeqI contig_bases = 0;
		while(readLength > 0){	//buffer the read/writes
			gnSeqI writeLen = readLength < BUFFER_SIZE + 20 ? readLength : BUFFER_SIZE + 20;
			boolean success = seq.ToArray(bases, writeLen, readOffset);
			if(!success)
				return false;
			//print each 60 on their own lines...
			for(gnSeqI curbaseI = 0; curbaseI < writeLen; curbaseI += 60){
				string baseIndexStr = uintToString(contig_bases + curbaseI +1);
				m_ofstream << string(SEQ_BASES_INDEX_END - baseIndexStr.length(), ' ');
				m_ofstream << baseIndexStr;
				for(gnSeqI base_offset = 0; base_offset <= 50; base_offset+=10){
					if(writeLen <= curbaseI + base_offset)
						break;
					int64 print_length = writeLen - (curbaseI + base_offset);
					print_length = print_length > 10 ? 10 : print_length;
					m_ofstream << ' ' << string(bases + curbaseI + base_offset, print_length);
				}
				m_ofstream << "\r\n";
			}
			readLength -= writeLen;
			readOffset += writeLen;
			contig_bases += writeLen;
		}
		m_ofstream << "//\r\n";
	}
	
	m_ofstream.close();
	return true;
}

gnFileContig* gnGBKSource::GetFileContig( const uint32 contigI ) const{
	if(m_contigList.size() > contigI)
		return m_contigList[contigI];
	return NULL;
}

//File parsing access routine
boolean gnGBKSource::ParseStream( istream& fin )
{
	// INIT temp varables
	uint32 readState = 0;
	uint32 lineStart = 0;
	// INIT buffer
	uint32 sectionStart = 0;
	uint64 streamPos = 0;
	uint64 bufReadLen = 0;
	uint64 remainingBuffer = 0;
	Array<char> array_buf( BUFFER_SIZE );
	char* buf = array_buf.data;
	gnFragmentSpec* curFrag = 0;
	gnSourceSpec* curSpec = 0;
	gnSourceHeader *curHeader;
	gnFeature* curFeature;
	gnFileContig* curContig = 0;
	gnLocation::gnLocationType curBaseLocationType;
	gnSeqI curLocationStart;
	int32 curStartLength = 0;
	int32 curEndLength = 0;
	string curLocContig = "";
	string curQualifierName;
	uint64 curQualifierStart;
	string curContigName = "";
	gnSeqI seqLength = 0;
	gnSeqI seqChunk, seqChunkCount, gapChunk;
	boolean corruptWarning = false;
	
	//decide what type of newlines we have
	DetermineNewlineType();

	m_spec = new gnGenomeSpec();
	while( !fin.eof() )
	{
		size_t newstart = sectionStart < lineStart ? sectionStart : lineStart;
		if(sectionStart > 0){
			if(readState == 14)
				sectionStart = lineStart;
			else if( sectionStart >= lineStart )
				sectionStart -= lineStart;
			remainingBuffer = bufReadLen - newstart;
			memmove(buf, buf+newstart, remainingBuffer);
		}
		  // read chars
		fin.read( buf + remainingBuffer, BUFFER_SIZE - remainingBuffer);
		streamPos -= remainingBuffer;
		lineStart -= newstart;
		bufReadLen = fin.gcount();
		bufReadLen += remainingBuffer;
		
		for( uint32 i=remainingBuffer ; i < bufReadLen ; i++ )
		{
			char ch = buf[i];
			switch( readState )
			{
				case 0: 	//Assume we are in header at the start of a new line.  
							//Look for keywords starting in column 1
					if((ch == '\n')&&(buf[lineStart] != ' ')&&(buf[lineStart] != '\t') && buf[lineStart] != '\r' && buf[lineStart] != '\n'){  //not equal to whitespace
						if(curSpec == NULL){
							curSpec = new gnSourceSpec(this, m_spec->GetSpecListLength());
							curFrag = new gnFragmentSpec();
							curFrag->AddSpec(curSpec);
							curSpec->SetSourceName(m_openString);
							m_spec->AddSpec(curFrag);
						}
						if(lineStart != sectionStart){	//Add the previous header to our list
							uint32 j = SEQ_HEADER_NAME_LENGTH-1;
							for(; j > 0; j--)	
								if((buf[sectionStart+j] != ' ')&&(buf[sectionStart+j] != '	'))
									break;
							string header_name = string(buf+sectionStart, j+1);
							curHeader = new gnSourceHeader(this, header_name, sectionStart + streamPos, lineStart - sectionStart);
							//if this is header info _before_ a locus statement then its a general file header.
							if(strncmp(&buf[lineStart], "LOCUS", 5) == 0)
								m_spec->AddHeader(curHeader);
							else	//otherwise its a fragment header.
								curFrag->AddHeader(curHeader);
							sectionStart = lineStart;
						}
						
						if(strncmp(&buf[lineStart], "FEATURES", 8) == 0){
							sectionStart = i + 1;
							readState = 1;  //read in features
						}else if(strncmp(&buf[lineStart], "ORIGIN", 6) == 0){
							curHeader = new gnSourceHeader(this, string("ORIGIN"), sectionStart + streamPos, i - sectionStart + 1);
							curFrag->AddHeader(curHeader);
							curContig = new gnFileContig();
							curContig->SetName(curContigName);
							curContigName = "";
							readState = 13;  //read in base pairs
						}else if(strncmp(&buf[lineStart], "LOCUS", 5) == 0){
							if(strncmp(&buf[lineStart+SEQ_LOCUS_CIRCULAR_COLUMN-1], "circular", 8) == 0)
								curFrag->SetCircular(true);
							string locus_line = string(buf+lineStart, 80);
							if( locus_line.find(" DNA ") == string::npos &&
								locus_line.find(" dna ") == string::npos )
								m_pFilter = gnFilter::proteinSeqFilter();
							else
								m_pFilter = gnFilter::fullDNASeqFilter();

							uint32 j = SEQ_LOCUS_NAME_LENGTH+1;
							for(; j > 0; j--)	
								if((buf[lineStart+SEQ_LOCUS_NAME_COLUMN+j-1] != ' ')&&(buf[sectionStart+SEQ_LOCUS_NAME_COLUMN+j-1] != '	'))
									break;
							curContigName = string(buf+lineStart+SEQ_LOCUS_NAME_COLUMN-1, j+1);
							curFrag->SetName(curContigName);
						}
					}
					if(ch == '\n'){
						lineStart = i + 1;
					}
					break;
				case 1:	//look for feature tag in column six.  ignore whitespace before feature.
					if((ch == ' ')||(ch == '	')){
						break;
					}else if(ch == '\n'){
						lineStart = i + 1;
						sectionStart = i + 1;
						break;
					}else if(sectionStart == i){ //there was no whitespace, we hit a TAG instead
						i--;
						readState = 0; //Deal with a Header TAG
						sectionStart = i + 1;
						break;
					}else if((i - lineStart == SEQ_SUBTAG_COLUMN)||((buf[lineStart]=='	')&&(i==lineStart+1))){
						sectionStart = i;
						readState = 2;
					} //
				case 2:  //Get the feature name.  stop on whitespace
					if((ch == ' ')||(ch == '	')){
						string featureName(buf+sectionStart, i - sectionStart);
						curFeature = new gnFeature(featureName);
						curFrag->AddFeature(curFeature);
						sectionStart = i + 1;
						readState = 3;
					}
					break;
				case 3:   //Ignore whitespace before feature location
					if((ch == ' ')||(ch == '	')){
						break;
					}else if((ch == '\r')||(ch == '\n')){
						lineStart = i+1;
						break;
					}
					sectionStart = i;
					readState = 4;
				// KNOWN BUG HERE!
				// if JOIN is outside a group of complemented coordinates the feature type
				// is incorrectly set to LT_COMPLEMENT.  Instead each location's type should be set to LT_COMPLEMENT 
				case 4:		//Read a location start.  stop on (<.:^ and whitespace
					if((ch == ' ')||(ch == '	')||(ch == '(')||(ch == '.')||(ch=='^')||(ch==':')){
						string starter(buf+sectionStart, i - sectionStart);
						if(ch == '('){
							if(starter == "complement")
								curFeature->SetLocationType(gnLocation::LT_Complement);
							else if(starter == "order")
								curFeature->SetLocationType(gnLocation::LT_Order);
							else if(starter == "group")
								curFeature->SetLocationType(gnLocation::LT_Group);
							else if(starter == "one-of")
								curFeature->SetLocationType(gnLocation::LT_OneOf);
							sectionStart = i + 1;	//ignore join since it is default.
							break;
						}else if(ch == ':'){
							curLocContig = starter;
							sectionStart = i + 1;
							break;
						}
						curLocationStart = atoi(starter.c_str());
						readState = 6;	//read in end base by default.
						if(ch == '.'){
							//go to special state to look for another one.
							readState = 5;
							sectionStart = i + 1;
							break;
						}else if(ch == '^'){
							curBaseLocationType = gnLocation::LT_BetweenBases;
						}else if((ch == ' ')||(ch == '	')){
							//no end location go to qualifier
							gnLocation curLocation(curLocationStart, curLocationStart);
							curFeature->AddLocation(curLocation, curFeature->GetLocationListLength());
							readState = 7;
						}
						sectionStart = i + 1;

					}else if(ch == '<'){
						curStartLength = -1;
						sectionStart = i + 1;
					}else if(ch == '>'){
						curStartLength = 1;
						sectionStart = i + 1;
					}
					break;
				case 5: //look for another period or location start.
					if(ch == '.'){
						curBaseLocationType = gnLocation::LT_Standard;
						readState = 6;
						sectionStart = i + 1;
						break;
					}
					curBaseLocationType = gnLocation::LT_OneOf;
				case 6:	//see if there's a second location value.  stop on >, and whitespace
					if(ch == '>'){
						curEndLength = 1;
						sectionStart = i + 1;
					}else if(ch == '<'){
						curEndLength = -1;
						sectionStart = i + 1;
					}else if((ch == ' ')||(ch == '	')||(ch == ',')||(ch == '\r')||(ch == '\n')){
						//read end location
						string ender(buf+sectionStart, i - sectionStart);
						gnSeqI curLocationEnd = atoi(ender.c_str());
						gnLocation curLocation(curLocationStart, curStartLength, curLocationEnd, curEndLength, curBaseLocationType);
						curEndLength = 0;
						curStartLength = 0;
						curFeature->AddLocation(curLocation, curFeature->GetLocationListLength());
						readState = ch == ',' ? 3 : 7;  //read another loc if we need to.
						sectionStart = i+1;
						if( ch == '\n' )
							lineStart = i+1;
					}
					break;
				case 7:  //skip to start of qualifier
					if((ch != ' ')&&(ch != '	')&&(lineStart == i)){
						sectionStart = i;	// Hit a tag.  go deal with it.
						readState = 0;
						i--;
					}else if((ch != ' ')&&(ch != '	')&&((lineStart == i - SEQ_SUBTAG_COLUMN)||((buf[lineStart]=='	')&&(i==lineStart+1)))){
						sectionStart = i;	// Hit a feature.  go deal with it.
						readState = 2;
						i--;
					}else if(ch == ','){  //oops!  another location to read!
						sectionStart = i+1;
						readState = 3;
					}else if(ch == '/'){  //finally, a qualifier.
						sectionStart = i+1;
						readState = 8;
					}else if(ch == '\n')
						lineStart = i + 1;
					break;
				case 8:		//get a qualifier, stop on =
					if(ch == '='){
						curQualifierName = string(buf+sectionStart, i - sectionStart);
						readState = 9;
						sectionStart = i+1;
					}else if( ch == '\r' || ch == '\n' ){
						// this is a value-less qualifier
						curQualifierName = string(buf+sectionStart, i - sectionStart);
						curFeature->AddQualifier( new gnStringQualifier( curQualifierName, "" ));
						readState = 7;
						sectionStart = i+1;
						if( ch == '\n' )
							lineStart = i + 1;
					}
					break;
				case 9:		//are we getting a string? look for " or [
					if(ch == '"'){
						readState = 10;
						sectionStart = i;
						curQualifierStart = i + streamPos;
					}else if(ch == '['){
						readState = 12;
						sectionStart = i;
					}else if((ch == '\r')||(ch == '\n')){
						curFeature->AddQualifier(new gnSourceQualifier(this, curQualifierName, sectionStart + streamPos, i - sectionStart));
						sectionStart = i+1;
						readState = 7; //look for another qualifier
						if( ch == '\n' )
							lineStart = i + 1;
					}
					break;
				case 10:		//read until the end of the quotation. look out for escaped quotes
					if(ch == '"')
						readState = 11;
					if(ch == '\n'){
						lineStart = i + 1;
					}
					break;
				case 11:
					if(ch != '"'){
						gnSourceQualifier* gnsq = new gnSourceQualifier(this, curQualifierName, curQualifierStart, i - sectionStart);
						curFeature->AddQualifier(gnsq);
						sectionStart = i+1;
						readState = 7;	//look for another qualifier.
						if(ch == '\n')
							lineStart = i + 1;
					}else
						readState = 10;  //quote was escaped.  look for another.
					break;
				case 12:
					if(ch == ']'){
						curFeature->AddQualifier(new gnSourceQualifier(this, curQualifierName, sectionStart + streamPos, i - sectionStart));
						sectionStart = i+1;
						readState = 7;	//look for another qualifier.
					}
					break;
				case 13:	//start the sequence read.
					curContig->SetSectStart(gnContigSequence, i - 1 + streamPos);
					curContig->SetRepeatSeqGap(true);
					seqChunk = 0;
					seqChunkCount = 0;
					gapChunk = m_newlineSize + 1;
					readState = 14;
					break;
				case 14:
					while(i < bufReadLen){
						ch = buf[i];
						if((ch == '/')&&(i==lineStart)){
							readState = 15;	//end of this sequence
							break;
						}else if(m_pFilter->IsValid(ch)){
							if(gapChunk > 0){
								if((gapChunk > 1 && seqChunkCount > 0) ||
								  (gapChunk != 10 + m_newlineSize && seqChunkCount == 0)){
								  	if( !corruptWarning ){
										ErrorMsg("File is corrupt.  Proceed with caution.");
										corruptWarning = true;
									}
									curContig->SetRepeatSeqGap(false);
								}
								gapChunk = 0;
							}
							seqChunk++;
							seqLength++;
						}else{
							gapChunk++;
							if(seqChunk == 10){
								seqChunk = 0;
								seqChunkCount++;
								if(seqChunkCount == 6){
									//got a complete line.  start over
									seqChunkCount = 0;
								}
							}
							if(ch == '\n')
								lineStart = i + 1;
						}
						i++;
					}
					break;
				case 15:
					if((ch == '\n')&&(buf[lineStart+1] == '/')){
						curContig->SetSectEnd(gnContigSequence, lineStart - m_newlineSize + streamPos);
						curContig->SetSeqLength(seqLength);
						m_contigList.push_back(curContig);
						curContig = 0;
						curSpec->SetLength(seqLength);
						curSpec = 0;
						seqLength = 0;
						lineStart = i + 1;
						sectionStart = i + 1;
						readState = 0;
					}
					break;
			}
		}
		streamPos += bufReadLen;
	}
	if(curContig != 0){
		curContig->SetSectEnd(gnContigSequence, streamPos - 1);
		curContig->SetSeqLength(seqLength);
		m_contigList.push_back(curContig);
		curSpec->SetLength(seqLength);
	}
	if(curSpec != 0)
		if((curFrag->GetFeatureListLength() == 0) && (curFrag->GetHeaderListLength() == 0)
			&&(curSpec->GetLength() == 0)){
			m_spec->RemoveSpec(m_spec->GetSpecListLength() - 1);
			delete curFrag;
		}
	m_ifstream.clear();
	return true;
}

}	// end namespace genome