forked from MichaelStromberg-KTH/Forage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAce.cpp
More file actions
1530 lines (1128 loc) · 47.7 KB
/
Copy pathAce.cpp
File metadata and controls
1530 lines (1128 loc) · 47.7 KB
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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Ace.h"
#include <algorithm>
CAce::CAce(char* dirPrefix, char* ace)
: m_Contigs(NULL)
, m_ContigSequences(NULL)
, m_ContigSeqsMask(NULL)
, m_NumContigs(0)
, m_NumContigSeqs(NULL)
, m_NumContigSeqsMasked(NULL)
, m_IsOldVersion(false)
, m_IsAligned(false)
{
BuildContigs(dirPrefix,ace);
}
CAce::~CAce(void) {
//printf("Ace destructor.\n");
// delete our contigs
if(m_Contigs) delete [] m_Contigs;
// delete our temporary contig sequence container and masking vector
for(unsigned int i=0;i<m_NumContigs;i++) {
if(m_ContigSequences[i]) delete [] m_ContigSequences[i];
if(m_ContigSeqsMask[i]) delete [] m_ContigSeqsMask[i];
}
if(m_ContigSequences) delete [] m_ContigSequences;
if(m_ContigSeqsMask) delete [] m_ContigSeqsMask;
// delete the contig sequences count vector
if(m_NumContigSeqs) delete [] m_NumContigSeqs;
// delete the contig masked sequences count vector
if(m_NumContigSeqsMasked) delete [] m_NumContigSeqsMasked;
}
// Loads the specified contig from the ace file
void CAce::BuildContigs(char* dirPrefix, char* ace) {
char buffer[BUFFER_SIZE];
char sequenceBuffer[SEQ_BUFFER_SIZE];
char absoluteFilename[FILENAME_SIZE];
char* consensusPosition;
char* sequencePosition;
char* token;
unsigned int positionsRead;
//printf("Loading %s.\n",ace);
sprintf(absoluteFilename,"%s%cedit_dir%c%s",dirPrefix,OS_SLASH,OS_SLASH,ace);
//printf("Absolute filename: %s\n",absoluteFilename);
ifstream in(absoluteFilename);
// if our file is missing, bomb
if(in.fail()) {
printf("ERROR: Could not open the ace file: %s\n",absoluteFilename);
exit(1);
}
// find the first non-empty line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
token = strtok(buffer," ");
if(strncmp(token,"AS",2) == 0) {
// extract the # of contigs
token = strtok(NULL," ");
m_NumContigs = atoi(token);
//printf("contigs: %u\n",m_NumContigs);
// initialize main variables
m_Contigs = new CContig[m_NumContigs];
m_NumContigSeqs = new unsigned int[m_NumContigs];
m_NumContigSeqsMasked = new unsigned int[m_NumContigs];
m_ContigSequences = new CSequence*[m_NumContigs];
m_ContigSeqsMask = new bool*[m_NumContigs];
for(unsigned int i=0;i<m_NumContigs;i++) {
m_ContigSequences[i] = NULL;
m_ContigSeqsMask[i] = NULL;
m_NumContigSeqsMasked[i] = 0;
}
// investigate all of our contigs
for(unsigned int currentContig=0;currentContig<m_NumContigs;currentContig++) {
// find the next contig tag
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"CO",2) != 0);
// extract the contig name
token = strtok(buffer," ");
token = strtok(NULL," ");
unsigned int nameLen = (unsigned int)strlen(token);
m_Contigs[currentContig].m_Name = new char[nameLen+1];
strncpy(m_Contigs[currentContig].m_Name,token,nameLen);
m_Contigs[currentContig].m_Name[nameLen] = 0;
//printf("Contig name: %s.\n",m_Contigs[currentContig].m_Name);
// extract the consensus length
token = strtok(NULL," ");
m_Contigs[currentContig].m_ConsensusLength = atoi(token);
//printf("Consensus length: %d.\n",m_Contigs[currentContig].m_ConsensusLength);
// extract the # of contig sequences
token = strtok(NULL," ");
m_NumContigSeqs[currentContig] = atoi(token);
//printf("Contig sequences: %u.\n",m_NumContigSeqs[currentContig]);
// read in the entire consensus sequence
sequencePosition = sequenceBuffer;
do {
in.getline(sequencePosition,SEQ_BUFFER_SIZE);
positionsRead = (unsigned int)strlen(sequencePosition);
sequencePosition += positionsRead;
} while(positionsRead != 0);
// replace all asterisks with hyphens and capitalize
for(sequencePosition = sequenceBuffer;*sequencePosition;sequencePosition++) {
if((*sequencePosition > 96) && (*sequencePosition < 123)) *sequencePosition -= 32;
if(*sequencePosition == 42) *sequencePosition = 45;
}
// TODO: is this right?
m_Contigs[currentContig].m_Consensus = new char[m_Contigs[currentContig].m_ConsensusLength+2];
m_Contigs[currentContig].m_ConsensusQuality = new char[m_Contigs[currentContig].m_ConsensusLength+2];
// TODO: do we really need to clear the entire base and quality sequence
memset(m_Contigs[currentContig].m_Consensus,0,m_Contigs[currentContig].m_ConsensusLength+2);
memset(m_Contigs[currentContig].m_ConsensusQuality,0,m_Contigs[currentContig].m_ConsensusLength+2);
sequencePosition = m_Contigs[currentContig].m_Consensus + 1;
memcpy(sequencePosition,sequenceBuffer,m_Contigs[currentContig].m_ConsensusLength);
//printf("Consensus: %s.\n",sequencePosition);
// find the BQ line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
if(strncmp(buffer,"BQ",2) != 0) {
printf("Not BQ.\n");
exit(1);
}
//printf("BQ buffer: %s.\n",buffer);
// read in the entire consensus quality sequence
sequencePosition = sequenceBuffer;
do {
in.getline(sequencePosition,SEQ_BUFFER_SIZE);
positionsRead = (unsigned int)strlen(sequencePosition);
sequencePosition += positionsRead;
} while(positionsRead != 0);
// convert the qualities to values
sequencePosition = m_Contigs[currentContig].m_ConsensusQuality + 1;
consensusPosition = m_Contigs[currentContig].m_Consensus + 1;
token = strtok(sequenceBuffer," ");
for(;*consensusPosition;sequencePosition++) {
// hop over all gaps
while(*consensusPosition == 45) {
*sequencePosition = 0;
consensusPosition++;
sequencePosition++;
}
*sequencePosition = atoi(token);
// increment the consensus position
consensusPosition++;
// stop if this is the end of the consensus
if(!*consensusPosition) break;
// extract the next token
token = strtok(NULL," ");
if(!token) {
printf("ERROR: No more tokens. Current consensus: %c\n",*consensusPosition);
exit(1);
}
}
// find the AF line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
if(strncmp(buffer,"AF",2) != 0) {
printf("Not AF.\n");
exit(1);
}
//printf("AF buffer: %s.\n",buffer);
// create our contig sequences and prepare a masking array
m_ContigSequences[currentContig] = new CSequence[m_NumContigSeqs[currentContig]];
m_ContigSeqsMask[currentContig] = new bool[m_NumContigSeqs[currentContig]];
for(unsigned int i=0;i<m_NumContigSeqs[currentContig];i++) m_ContigSeqsMask[currentContig][i] = false;
// read all of the AF positions
unsigned int contigSeqCounter = 0;
do {
if(strncmp(buffer,"BS",2) == 0) break;
// skip past the AF token
token = strtok(buffer," ");
// extract the sequence name
token = strtok(NULL," ");
unsigned int nameLen = (unsigned int)strlen(token);
m_ContigSequences[currentContig][contigSeqCounter].m_Name = new char[nameLen+1];
strncpy(m_ContigSequences[currentContig][contigSeqCounter].m_Name,token,nameLen);
m_ContigSequences[currentContig][contigSeqCounter].m_Name[nameLen] = 0;
//printf("Sequence name: %s\n",m_ContigSequences[currentContig][contigSeqCounter].m_Name);
// extract the complement direction
token = strtok(NULL," ");
if(*token == 'C') m_ContigSequences[currentContig][contigSeqCounter].m_IsReverseComplement = true;
//if(m_ContigSequences[currentContig][contigSeqCounter].m_IsReverseComplement) printf("REVERSE.\n");
// extract offset
token = strtok(NULL," ");
m_ContigSequences[currentContig][contigSeqCounter++].m_Offset = atoi(token);
//printf("Offset: %d\n",atoi(token));
// get new line
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) != 0);
//for(unsigned int i=0;i<m_NumContigSeqs[currentContig];i++) printf("Name: %s %d\n",m_ContigSequences[currentContig][i].m_Name,m_ContigSequences[currentContig][i].m_Offset);
// skip over BS
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) > 0);
// find next line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
//
// READ SEQUENCES FROM ACE FILE
//
int numSeqsRead = 0;
while(strncmp(buffer,"RD",2) == 0) {
// skip past the RD token
token = strtok(buffer," ");
// check the sequence name
token = strtok(NULL," ");
if(strcmp(token,m_ContigSequences[currentContig][numSeqsRead].m_Name) != 0) {
printf("ERROR: Not the same sequence name. Token: %s (%s)\n",token,m_ContigSequences[currentContig][numSeqsRead].m_Name);
exit(1);
}
// extract the # of sequence bases
token = strtok(NULL," ");
unsigned int numSeqBases = atoi(token);
//printf("# of bases in sequence: %u\n",numSeqBases);
// pick the larger of the two: consensus length or sequence size
unsigned int allocationSize;
if((numSeqBases+1) > (m_Contigs[currentContig].m_ConsensusLength+2)) allocationSize = numSeqBases + 1;
else allocationSize = m_Contigs[currentContig].m_ConsensusLength + 2;
// initialize our sequence arrays
m_ContigSequences[currentContig][numSeqsRead].m_Bases = new char[allocationSize];
m_ContigSequences[currentContig][numSeqsRead].m_Qualities = new char[allocationSize];
m_ContigSequences[currentContig][numSeqsRead].m_NumBases = numSeqBases;
unsigned int filenameLength = (unsigned int)strlen(m_ContigSequences[currentContig][numSeqsRead].m_Name)+6;
m_ContigSequences[currentContig][numSeqsRead].m_Filename = new char[filenameLength+1];
sprintf(m_ContigSequences[currentContig][numSeqsRead].m_Filename,"%s.phd.1",m_ContigSequences[currentContig][numSeqsRead].m_Name);
//printf("Filename: %s\n",m_ContigSequences[currentContig][numSeqsRead].m_Filename);
// read in the entire sequence
sequencePosition = sequenceBuffer;
do {
in.getline(sequencePosition,SEQ_BUFFER_SIZE);
positionsRead = (unsigned int)strlen(sequencePosition);
sequencePosition += positionsRead;
} while(positionsRead != 0);
// replace all asterisks with hyphens and capitalize
for(sequencePosition = sequenceBuffer;*sequencePosition;sequencePosition++) {
if((*sequencePosition > 96) && (*sequencePosition < 123)) *sequencePosition -= 32;
if(*sequencePosition == 42) *sequencePosition = 45;
}
// copy the sequence bases to the sequence object
// TODO: Maybe we should do this directly in the sequence object?
//printf("numSeqBases: %u, base length: %u\n",numSeqBases,m_Contigs[currentContig].m_ConsensusLength+2);
strncpy(m_ContigSequences[currentContig][numSeqsRead].m_Bases,sequenceBuffer,numSeqBases);
//printf("first base: %c\n",m_ContigSequences[currentContig][numSeqsRead].m_Bases[0]);
// evaluate the QA tag
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"QA",2) != 0);
// skip past the QA token
token = strtok(buffer," ");
// extract the quality clip start
token = strtok(NULL," ");
int qualClipStart = atoi(token);
// extract the quality clip end
token = strtok(NULL," ");
int qualClipEnd = atoi(token);
// extract the alignment clip start
token = strtok(NULL," ");
int alignClipStart = atoi(token);
// extract the alignment clip end
token = strtok(NULL," ");
int alignClipEnd = atoi(token);
// analyze sequence clip starting and ending points
if(qualClipStart > alignClipStart) m_ContigSequences[currentContig][numSeqsRead].m_ClipStart = qualClipStart;
else m_ContigSequences[currentContig][numSeqsRead].m_ClipStart = alignClipStart;
if(qualClipEnd < alignClipEnd) m_ContigSequences[currentContig][numSeqsRead].m_ClipEnd = qualClipEnd;
else m_ContigSequences[currentContig][numSeqsRead].m_ClipEnd = alignClipEnd;
// mask the sequence if it has low quality
if(((qualClipStart == -1) && (qualClipEnd == -1)) || ((alignClipStart == -1) && (alignClipEnd == -1))) {
//printf("MASKING: %s.\n",m_ContigSequences[currentContig][numSeqsRead].m_Name);
m_ContigSeqsMask[currentContig][numSeqsRead] = true;
m_NumContigSeqsMasked[currentContig]++;
} else {
// check to see if file exists
sprintf(absoluteFilename,"%s%cphd_dir%c%s",dirPrefix,OS_SLASH,OS_SLASH,m_ContigSequences[currentContig][numSeqsRead].m_Filename);
ifstream seq(absoluteFilename);
// if our sequence is missing, remove it
if(seq.fail()) {
printf("- MISSING: %s\n",absoluteFilename);
m_ContigSeqsMask[currentContig][numSeqsRead] = true;
m_NumContigSeqsMasked[currentContig]++;
}
}
// increment our sequence counter
numSeqsRead++;
// find next sequence
if(numSeqsRead == m_NumContigSeqs[currentContig]) break;
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"RD",2) != 0);
}
//printf("Sequences read: %d.\n",numSeqsRead);
} // end contig for loop
in.close();
} else {
// This is most likely an old style ace file then
in.close();
printf("ERROR: AS tag not found. Trying to load as old style ace file.\n");
BuildOldAceContigs(dirPrefix,ace);
}
}
// Loads the specified contig from the ace file
void CAce::BuildOldAceContigs(char* dirPrefix, char* ace) {
char buffer[BUFFER_SIZE];
char sequenceBuffer[SEQ_BUFFER_SIZE];
char absoluteFilename[FILENAME_SIZE];
char* consensusPosition;
char* sequencePosition;
char* token;
unsigned int positionsRead;
printf("Loading %s.\n",ace);
sprintf(absoluteFilename,"%s%cedit_dir%c%s",dirPrefix,OS_SLASH,OS_SLASH,ace);
//printf("Absolute filename: %s\n",absoluteFilename);
ifstream in(absoluteFilename);
// if our file is missing, bomb
if(in.fail()) {
printf("ERROR: Could not open the old version ace file: %s\n",absoluteFilename);
exit(1);
}
// find the first non-empty line
// TODO: might have to check for EOF here
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
// check to see if this is a valid old style ace file
token = strtok(buffer," ");
if(strncmp(token,"DNA",3) != 0) {
printf("ERROR: This is neither an ace file nor an old version ace file.\n");
exit(1);
}
// mark this as an old version ace object
m_IsOldVersion = true;
//
// first pass (count contigs)
//
unsigned int numContigSeqs = 0;
unsigned int numDnaTags = 0;
while(!in.eof()) {
in.getline(buffer,BUFFER_SIZE);
if(strncmp(buffer,"Assembled_from*",15) == 0) numContigSeqs++;
if(strncmp(buffer,"DNA",3) == 0) numDnaTags++;
}
m_NumContigs = numDnaTags - numContigSeqs + 1;
printf("Contigs: %u.\n",m_NumContigs);
// initialize main variables
m_Contigs = new CContig[m_NumContigs];
m_NumContigSeqs = new unsigned int[m_NumContigs];
m_NumContigSeqsMasked = new unsigned int[m_NumContigs];
m_ContigSequences = new CSequence*[m_NumContigs];
m_ContigSeqsMask = new bool*[m_NumContigs];
for(unsigned int i=0;i<m_NumContigs;i++) {
m_ContigSequences[i] = NULL;
m_ContigSeqsMask[i] = NULL;
m_NumContigSeqsMasked[i] = 0;
}
//
// second pass (count sequences per contig)
//
// go back to the beginning of the file
in.clear();
in.seekg(0,ios_base::beg);
for(unsigned char currentContig=0;currentContig<m_NumContigs;currentContig++) {
// find the next DNA tag (contig)
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"DNA",3) != 0);
// find next sequence tag
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"Sequence",8) != 0);
// count the number of Assembled_from* tags
unsigned int numContigSeqs = 0;
do {
in.getline(buffer,BUFFER_SIZE);
if(strncmp(buffer,"Assembled_from*",15) == 0) numContigSeqs++;
} while(strncmp(buffer,"Base_segment",12) != 0);
printf("Contig sequences: %u\n",numContigSeqs);
// initialize sequence related arrays
m_NumContigSeqs[currentContig] = numContigSeqs;
m_ContigSequences[currentContig] = new CSequence[numContigSeqs];
m_ContigSeqsMask[currentContig] = new bool[numContigSeqs];
for(unsigned int i=0;i<numContigSeqs;i++) m_ContigSeqsMask[currentContig][i] = false;
// skip over the sequences
unsigned int skippedSeqs = 0;
do {
in.getline(buffer,BUFFER_SIZE);
if(strncmp(buffer,"DNA",3) == 0) skippedSeqs++;
} while(skippedSeqs < numContigSeqs);
}
//
// third pass (read content)
//
// go back to the beginning of the file
in.clear();
in.seekg(0,ios_base::beg);
for(unsigned char currentContig=0;currentContig<m_NumContigs;currentContig++) {
// find the next DNA tag (contig)
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"DNA",3) != 0);
// extract the contig name
token = strtok(buffer," ");
token = strtok(NULL," ");
unsigned int nameLen = (unsigned int)strlen(token);
m_Contigs[currentContig].m_Name = new char[nameLen+1];
strncpy(m_Contigs[currentContig].m_Name,token,nameLen);
m_Contigs[currentContig].m_Name[nameLen] = 0;
//printf("Contig name: %s.\n",m_Contigs[currentContig].m_Name);
// read in the entire consensus sequence
sequencePosition = sequenceBuffer;
do {
in.getline(sequencePosition,SEQ_BUFFER_SIZE);
positionsRead = (unsigned int)strlen(sequencePosition);
sequencePosition += positionsRead;
} while(positionsRead != 0);
// replace all asterisks with hyphens and capitalize
for(sequencePosition = sequenceBuffer;*sequencePosition;sequencePosition++) {
if((*sequencePosition > 96) && (*sequencePosition < 123)) *sequencePosition -= 32;
if(*sequencePosition == 42) *sequencePosition = 45;
}
// extract the consensus length
m_Contigs[currentContig].m_ConsensusLength = (unsigned int)strlen(sequenceBuffer);
//printf("Consensus length: %d.\n",m_Contigs[currentContig].m_ConsensusLength);
// TODO: is this right?
m_Contigs[currentContig].m_Consensus = new char[m_Contigs[currentContig].m_ConsensusLength+2];
m_Contigs[currentContig].m_ConsensusQuality = new char[m_Contigs[currentContig].m_ConsensusLength+2];
// TODO: do we really need to clear the entire base and quality sequence
memset(m_Contigs[currentContig].m_Consensus,0,m_Contigs[currentContig].m_ConsensusLength+2);
memset(m_Contigs[currentContig].m_ConsensusQuality,0,m_Contigs[currentContig].m_ConsensusLength+2);
sequencePosition = m_Contigs[currentContig].m_Consensus + 1;
memcpy(sequencePosition,sequenceBuffer,m_Contigs[currentContig].m_ConsensusLength);
//printf("Consensus: %s.\n",sequencePosition);
// find the BaseQuality line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
if(strncmp(buffer,"BaseQuality",11) != 0) {
printf("Not BaseQuality.\n");
exit(1);
}
//printf("BaseQuality buffer: %s.\n",buffer);
// read in the entire consensus quality sequence
sequencePosition = sequenceBuffer;
do {
in.getline(sequencePosition,SEQ_BUFFER_SIZE);
positionsRead = (unsigned int)strlen(sequencePosition);
sequencePosition += positionsRead;
} while(positionsRead != 0);
//printf("BaseQuality: %s.\n",sequenceBuffer);
// convert the qualities to values
sequencePosition = m_Contigs[currentContig].m_ConsensusQuality + 1;
consensusPosition = m_Contigs[currentContig].m_Consensus + 1;
token = strtok(sequenceBuffer," ");
for(;*consensusPosition;sequencePosition++) {
// hop over all gaps
while(*consensusPosition == 45) {
*sequencePosition = 0;
consensusPosition++;
sequencePosition++;
}
*sequencePosition = atoi(token);
// increment the consensus position
consensusPosition++;
// stop if this is the end of the consensus
if(!*consensusPosition) break;
// extract the next token
token = strtok(NULL," ");
if(!token) {
printf("ERROR: No more tokens. Current consensus: %c\n",*consensusPosition);
exit(1);
}
}
// find the Sequence line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
if(strncmp(buffer,"Sequence",8) != 0) {
printf("Not Sequence.\n");
exit(1);
}
//printf("Sequence buffer: %s.\n",buffer);
// read all of the Assembled_from* positions
unsigned int contigSeqCounter = 0;
do {
if(strncmp(buffer,"Base_segment",12) == 0) break;
// only evaluate the Assembled_from* tags
if(strncmp(buffer,"Assembled_from*",15) == 0) {
// skip past the Assembled_from* token
token = strtok(buffer," ");
// extract the sequence name
token = strtok(NULL," ");
unsigned int nameLen = (unsigned int)strlen(token);
m_ContigSequences[currentContig][contigSeqCounter].m_Name = new char[nameLen+1];
strncpy(m_ContigSequences[currentContig][contigSeqCounter].m_Name,token,nameLen);
m_ContigSequences[currentContig][contigSeqCounter].m_Name[nameLen] = 0;
//printf("Sequence name: %s.\n",m_ContigSequences[currentContig][contigSeqCounter].m_Name);
// extract the complement direction
sequencePosition = token + nameLen - 5;
if(strncmp(sequencePosition,".comp",5) == 0) m_ContigSequences[currentContig][contigSeqCounter].m_IsReverseComplement = true;
//if(m_ContigSequences[currentContig][contigSeqCounter].m_IsReverseComplement) printf("REVERSE.\n");
// extract offset
token = strtok(NULL," ");
m_ContigSequences[currentContig][contigSeqCounter++].m_Offset = atoi(token);
//printf("Offset: %d\n",atoi(token)-1);
}
// get new line
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) != 0);
//for(unsigned int i=0;i<m_NumContigSeqs[currentContig];i++) printf("Name: %s %d\n",m_ContigSequences[currentContig][i].m_Name,m_ContigSequences[currentContig][i].m_Offset);
// skip over Base_segment
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) > 0);
// find next line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strlen(buffer) == 0);
//
// READ SEQUENCES FROM ACE FILE
//
int numSeqsRead = 0;
while(strncmp(buffer,"DNA",3) == 0) {
// skip past the DNA token
token = strtok(buffer," ");
// check the sequence name
token = strtok(NULL," ");
if(strcmp(token,m_ContigSequences[currentContig][numSeqsRead].m_Name) != 0) {
printf("ERROR: Not the same sequence name. Token: %s (%s)\n",token,m_ContigSequences[currentContig][numSeqsRead].m_Name);
exit(1);
}
// set the sequence filename
int nameLength = (int)strlen(m_ContigSequences[currentContig][numSeqsRead].m_Name);
strncpy(buffer,m_ContigSequences[currentContig][numSeqsRead].m_Name,BUFFER_SIZE);
if(m_ContigSequences[currentContig][numSeqsRead].m_IsReverseComplement) nameLength -= 5;
buffer[nameLength] = 0;
unsigned int filenameLength = nameLength + 6;
m_ContigSequences[currentContig][numSeqsRead].m_Filename = new char[filenameLength+1];
sprintf(m_ContigSequences[currentContig][numSeqsRead].m_Filename,"%s.phd.1",buffer);
//printf("Name: %s, filename: %s\n",m_ContigSequences[currentContig][numSeqsRead].m_Name,m_ContigSequences[currentContig][numSeqsRead].m_Filename);
// read in the entire sequence
sequencePosition = sequenceBuffer;
do {
in.getline(sequencePosition,SEQ_BUFFER_SIZE);
positionsRead = (unsigned int)strlen(sequencePosition);
sequencePosition += positionsRead;
} while(positionsRead != 0);
// replace all asterisks with hyphens and capitalize
for(sequencePosition = sequenceBuffer;*sequencePosition;sequencePosition++) {
if((*sequencePosition > 96) && (*sequencePosition < 123)) *sequencePosition -= 32;
if(*sequencePosition == 42) *sequencePosition = 45;
}
// extract the # of sequence bases
unsigned int numSeqBases = (unsigned int)strlen(sequenceBuffer);
//printf("# of bases in sequence: %u\n",numSeqBases);
// pick the larger of the two: consensus length or sequence size
unsigned int allocationSize;
if((numSeqBases+1) > (m_Contigs[currentContig].m_ConsensusLength+2)) allocationSize = numSeqBases + 1;
else allocationSize = m_Contigs[currentContig].m_ConsensusLength + 2;
// initialize the base array
m_ContigSequences[currentContig][numSeqsRead].m_Bases = new char[allocationSize];
m_ContigSequences[currentContig][numSeqsRead].m_Qualities = new char[allocationSize];
m_ContigSequences[currentContig][numSeqsRead].m_NumBases = numSeqBases;
// TODO: temporary
memset(m_ContigSequences[currentContig][numSeqsRead].m_Bases,'_',sizeof(char)*m_Contigs[currentContig].m_ConsensusLength+2);
// copy the sequence bases to the sequence object
strncpy(m_ContigSequences[currentContig][numSeqsRead].m_Bases,sequenceBuffer,numSeqBases);
//printf("first base: %c, second base: %c, third base: %c\n",m_ContigSequences[currentContig][numSeqsRead].m_Bases[0],m_ContigSequences[currentContig][numSeqsRead].m_Bases[1],m_ContigSequences[currentContig][numSeqsRead].m_Bases[2]);
//printf("Bases: %s\n",m_ContigSequences[currentContig][numSeqsRead].m_Bases);
// evaluate the Clipping* tag
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"Clipping*",9) != 0);
// skip past the Clipping* token
token = strtok(buffer," ");
// extract the clip start
token = strtok(NULL," ");
m_ContigSequences[currentContig][numSeqsRead].m_ClipStart = atoi(token);
// extract the clip end
token = strtok(NULL," ");
m_ContigSequences[currentContig][numSeqsRead].m_ClipEnd = atoi(token);
// check to see if file exists
sprintf(absoluteFilename,"%s%cphd_dir%c%s",dirPrefix,OS_SLASH,OS_SLASH,m_ContigSequences[currentContig][numSeqsRead].m_Filename);
ifstream seq(absoluteFilename);
// if our sequence is missing, remove it
if(seq.fail()) {
printf("- MISSING: %s\n",absoluteFilename);
m_ContigSeqsMask[currentContig][numSeqsRead] = true;
m_NumContigSeqsMasked[currentContig]++;
}
// increment our sequence counter
numSeqsRead++;
// find next sequence
if(numSeqsRead == m_NumContigSeqs[currentContig]) break;
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"DNA",3) != 0);
}
printf("Sequences read: %d.\n",numSeqsRead);
} // end contig for loop
in.close();
}
// Loads the specified contig from the ace file
void CAce::LoadPhdFiles(char* dirPrefix) {
char qualityBuffer[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
char absoluteFilename[FILENAME_SIZE];
char* qualityPosition;
char* token;
unsigned int seqLength;
// return if we have no contigs to load
if(m_NumContigs == 0) {
printf("ERROR: Cannot load sequences for cluster with no contigs.\n");
exit(1);
}
// return if we have no sequences to load
if(!m_ContigSequences) {
printf("ERROR: Contig sequences not properly initialized.\n");
exit(1);
}
for(unsigned int currentContig=0;currentContig<m_NumContigs;currentContig++) {
//printf("Loading sequences for %s.\n",m_Contigs[currentContig].m_Name);
for(unsigned int i=0;i<m_NumContigSeqs[currentContig];i++) {
// if the sequence is masked, skip sequence
if(m_ContigSeqsMask[currentContig][i]) continue;
//printf("- Loading sequence %u: %s.\n",i+1,m_ContigSequences[currentContig][i].m_Name);
// construct our absolute filename
sprintf(absoluteFilename,"%s%cphd_dir%c%s",dirPrefix,OS_SLASH,OS_SLASH,m_ContigSequences[currentContig][i].m_Filename);
ifstream in(absoluteFilename);
// if we can't open the file, the prefiltering has failed
if(in.fail()) {
printf("ERROR: Could not open %s.\n",absoluteFilename);
exit(1);
}
// initialize our variables
qualityPosition = qualityBuffer;
seqLength = 0;
// find next BEGIN_DNA line
do {
in.getline(buffer,BUFFER_SIZE);
} while(strncmp(buffer,"BEGIN_DNA",9) != 0);
// keep going until we hit END_DNA
in.getline(buffer,BUFFER_SIZE);
do {
// skip over the base
token = strtok(buffer," ");
// extract the quality
token = strtok(NULL," ");
*qualityPosition = atoi(token);
// increment positions
qualityPosition++;
// get next line
in.getline(buffer,BUFFER_SIZE);
seqLength++;
} while(strncmp(buffer,"END_DNA",7) != 0);
// close the sequence file
in.close();
// spit out an error if our buffer size is too small
if(seqLength > BUFFER_SIZE) {
printf("ERROR: Sequence being parsed is larger than the allocated buffer size: %u (%u).\n",seqLength,BUFFER_SIZE);
exit(1);
}
// if the sequence is reverse complement, reverse it
if(m_ContigSequences[currentContig][i].m_IsReverseComplement) ReverseSequence(qualityBuffer,seqLength);
char* bases = m_ContigSequences[currentContig][i].m_Bases;
char* qualities = m_ContigSequences[currentContig][i].m_Qualities;
unsigned int numBases = m_ContigSequences[currentContig][i].m_NumBases;
unsigned int qualityCounter = 0;
unsigned int baseCounter = 0;
while((qualityCounter < numBases) && (baseCounter < numBases)) {
while((bases[baseCounter] == '-') && (baseCounter < numBases)) qualities[baseCounter++] = 0;
qualities[baseCounter++] = qualityBuffer[qualityCounter++];
}
// trim the sequence
//printf("Sequence length: %u, clip start: %u, clip end: %u\n",storedSeqLength,m_ContigSequences[currentContig][i].m_ClipStart,m_ContigSequences[currentContig][i].m_ClipEnd);
// the trimming seems to work perfectly
for(int j=0;j<m_ContigSequences[currentContig][i].m_ClipStart-1;j++) {
m_ContigSequences[currentContig][i].m_Bases[j] = 0;
m_ContigSequences[currentContig][i].m_Qualities[j] = 0;
}
for(unsigned int j=m_ContigSequences[currentContig][i].m_ClipEnd;j<numBases;j++) {
m_ContigSequences[currentContig][i].m_Bases[j] = 0;
m_ContigSequences[currentContig][i].m_Qualities[j] = 0;
}
// debugging
//for(unsigned int j=0;j<20;j++) printf("%c",bases[j]);
//printf("\n");
} // end contig seqs for loop
} // end contig for loop
}
// Reverses a sequence (for reverse complements)
void CAce::ReverseSequence(char* str, unsigned int length) {
unsigned int end = length - 1;
unsigned int start = 0;
while(start<end) {
str[start] ^= str[end];
str[end] ^= str[start];
str[start++] ^= str[end--];
}
}
// Aligns all of the sequences in the ace file
void CAce::AlignSequences(void) {
// bomb if we're trying to align an aligned cluster
if(m_IsAligned) {
printf("ERROR: Cluster is already aligned.\n");
exit(1);
}
for(unsigned int currentContig=0;currentContig<m_NumContigs;currentContig++) {
unsigned int consensusLength = m_Contigs[currentContig].m_ConsensusLength;
//printf("Aligning %s\n",m_Contigs[currentContig].m_Name);
for(unsigned int i=0;i<m_NumContigSeqs[currentContig];i++) {
// if the sequence is masked, skip sequence
if(m_ContigSeqsMask[currentContig][i]) continue;
// align the sequence
if(m_IsOldVersion) m_ContigSequences[currentContig][i].AlignOldVersion(consensusLength);
else m_ContigSequences[currentContig][i].Align(consensusLength);
} // // end contig seqs for loop
} // // end contig for loop
m_IsAligned = true;
}
// Dumps all of the sequences in the ace file
void CAce::DumpSequences(void) {
char dashBuffer[SEQ_BUFFER_SIZE];
for(unsigned int currentContig=0;currentContig<m_NumContigs;currentContig++) {
unsigned int consensusLength = m_Contigs[currentContig].m_ConsensusLength + 1;
printf("%s\n",m_Contigs[currentContig].m_Name);
memset(dashBuffer,'=',sizeof(char)*consensusLength);
dashBuffer[consensusLength] = 0;
printf("================%s\n",dashBuffer);
printf("%16s",m_Contigs[currentContig].m_Name);
for(unsigned int j=0;j<consensusLength;j++) printf("%c",m_Contigs[currentContig].m_Consensus[j]);
printf("\n");
printf("================%s\n",dashBuffer);
for(unsigned int i=0;i<m_NumContigSeqs[currentContig];i++) {