-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMitoGeneExtractor.cpp
2005 lines (1704 loc) · 68.6 KB
/
MitoGeneExtractor.cpp
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 <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <ctime>
#include <iomanip>
#include "faststring2.h"
#include "CSequences2.h"
#include "CSequence_Mol2_1.h"
#include "Ctriple.h"
#include "CFile/CFile2_1.h"
#include "CDnaString2.h"
#include "global-types-and-parameters_MitoGeneExtractor.h"
#include "statistic_functions.h"
#include "fastq.h"
#include "Cfastq-sequences.h"
/// #include <utility>
using namespace std;
typedef pair<unsigned, unsigned> Key;
typedef map< Key , unsigned> Mymap;
// Verbosity rules:
// verbosity 0 -> only error messages that lead to exit() (goes to cerr).
// Welcome is printed to cout. Parameters are printed to cout.
// verbosity 1 -> warnings, ALERT (goes to cerr)
// verbosity 2 -> more warnings, e.g. skipping features
// verbosity 3 -> Basic progress (goes to cout)
// verbosity 4 -> more progress (goes to cout)
// verbosity >=20: DEGUB
// Default: 1
// What the exonerate manual says about coordinates:
// The coordinates are the coordinates in between the bases:
// A C G T
// 0 1 2 3 4
// This is equivalent to a 0 based system in which the end is indicated by the
// next index after the range.
//bool add_partial_codons_at_ends = true;
//unsigned length_constaint = 2000;
unsigned num_WARNINGS = 0;
void append_fasta_files_to_exonerate_input_file(FILE *ofp, vector<string> global_input_dna_fasta_filenames)
{
for (vector<string>::size_type i=0; i < global_input_dna_fasta_filenames.size(); ++i)
{
FILE* ifp; // input file pointer
unsigned nread;
ifp = fopen(global_input_dna_fasta_filenames[i].c_str(), "r");
if (!ifp)
{
cerr << "The fasta input file " << global_input_dna_fasta_filenames[i] << "\ncould not be opened. "
"Maybe it does not exist. Exiting.\n";
good_bye_and_exit(-27);
}
char const_buf [1000000];
while ((nread = fread(const_buf, sizeof(char), sizeof(const_buf), ifp)) > 0)
{
fwrite(const_buf, sizeof(char), nread, ofp);
}
fclose(ifp);
}
}
void append_fastq_files_to_exonerate_input_file(FILE *ofp, vector<string> global_input_dna_fastq_filenames)
{
for (vector<string>::size_type i=0; i < global_input_dna_fastq_filenames.size(); ++i)
{
CFile infile;
infile.ffopen(global_input_dna_fastq_filenames[i]);
if (infile.fail())
{
cerr << "The fastq input file " << global_input_dna_fastq_filenames[i] << "\ncould not be opened. "
"Maybe it does not exist. Exiting.\n";
good_bye_and_exit(-27);
}
fastq_sequences fq_in_collection;
fq_in_collection.read_fastq(infile, global_input_dna_fastq_filenames[i].c_str(), 1);
infile.close();
/*
unsigned N_seq_in_fq_file = fq_in_collection.size();
if (global_verbosity >= 1)
{
cout << "Found " << N_seq_in_fq_file << " sequences in input file " << global_input_dna_fastq_filenames[i]
<< endl;
}
*/
// fq_in.print(cout);
CSequences2 *pseqs = new CSequences2(CSequence_Mol::dna);
fq_in_collection.add_sequences_to_CSequences_object(pseqs);
pseqs->ExportSequences(ofp, 'f', UINT_MAX);
delete pseqs;
}
}
struct stats_for_given_target
{
// All numbers will be for this specific target
unsigned skipped_double_vulgar;
unsigned skipped_G;
unsigned skipped_F;
unsigned skipped_relative_score;
unsigned skipped_no_G;
unsigned not_considered;
unsigned not_best_score_for_query;
unsigned good_hits_added_to_alignment;
unsigned hits_in_vulgar_file;
stats_for_given_target():skipped_double_vulgar(0), skipped_G(0),
skipped_F(0), skipped_relative_score(0),
skipped_no_G(0), not_considered(0),
not_best_score_for_query(0),
good_hits_added_to_alignment(0),
hits_in_vulgar_file(0)
{}
void increment_skipped_double_vulgar()
{
++skipped_double_vulgar;
}
void increment_skipped_G()
{
++skipped_G;
}
void increment_skipped_F()
{
++skipped_F;
}
void increment_skipped_relative_score()
{
++skipped_relative_score;
}
void increment_skipped_no_G()
{
++skipped_no_G;
}
void increment_not_considered()
{
++not_considered;
}
void increment_not_best_score()
{
++not_best_score_for_query;
}
void increment_good_hits_added_to_alignment()
{
++good_hits_added_to_alignment;
}
void increment_hits_in_vulgar_file()
{
++hits_in_vulgar_file;
}
};
/*
void copy_file(const char * sourcename, const char * destname, const char * out_mode)
{
FILE* ifp; // input file pointer
FILE* ofp; // output file pointer
unsigned nread;
ifp = fopen(sourcename, "r");
ofp = fopen(destname, out_mode);
char const_buf [100000];
while ((nread = fread(const_buf, sizeof(char), sizeof(const_buf), ifp)) > 0)
{
fwrite(const_buf, sizeof(char), nread, ofp);
}
fclose(ifp);
fclose(ofp);
}
*/
inline void add_or_count(Mymap &m, Key k)
{
if (m.find(k) == m.end())
m.insert(make_pair(k,1));
else
++m[k];
}
void print_Mymap(ostream &os, Mymap &m)
{
Mymap::iterator it, it_end;
it = m.begin();
it_end = m.end();
while (it != it_end)
{
os << "(" << it->first.first << "," << it->first.second << "):" << it->second << endl;
++it;
}
}
inline void add_or_count(std::map<faststring, unsigned> &m, faststring &x)
{
std::map<faststring,unsigned>::iterator it;
it = m.find(x);
if (it == m.end() )
{
m[x] = 1;
}
else
{
++it->second;
}
}
void print_DNA_profile(ostream &os, unsigned **profile, unsigned N_sites)
{
os << "pos\tA\tC\tG\tT\t-\tambig\ttilde\n";
for (unsigned i=0; i<N_sites; ++i)
{
os << i+1 << '\t' << profile[i][0] << '\t' << profile[i][1] << '\t' << profile[i][2]
<< '\t' << profile[i][3] << '\t' << profile[i][4] << '\t' << profile[i][5] << '\t' << profile[i][6] << '\n';
}
}
// IMPORTANT: Reads/sequences are targets.
// Reference sequences are queries.
class vulgar
{
public:
static map<faststring, short> queryID2Index;
faststring queryID; // In this program, this is the AA-COI consensus seq. exonerate aligned against.
short queryIndex; // Every query sequence (reference sequence) has a query number.
// This avoids many lookups for index.
unsigned query_start; // 0 based numbers.
unsigned query_end; // The first position after the specified range.
char query_strand;
faststring targetID; // In this program, they are the reads.
unsigned target_start; // 0 based numbers.
unsigned target_end; // The first position after the specified range.
char target_strand;
unsigned score;
vector<Ctriple<char, unsigned, unsigned> > attributes;
// Frequent labels:
bool bool_has_M;
bool bool_has_G;
bool bool_has_F;
// Rare labels:
bool bool_has_5;
bool bool_has_3;
bool bool_has_C;
bool bool_has_N;
bool bool_has_I;
bool bool_has_S;
bool bool_has_non_M;
public:
static short add_query_to_query2short_map_or_get_index(faststring q)
{
map<faststring, short>::iterator find_it = queryID2Index.find(q);
if (find_it != queryID2Index.end())
return find_it->second;
unsigned s = queryID2Index.size();
queryID2Index[q] = s;
return s;
}
short get_index_in_query2short_map(faststring q)
{
map<faststring, short>::iterator find_it = queryID2Index.find(q);
if (find_it != queryID2Index.end())
return find_it->second;
else
{
cerr << "Reference sequence appears in Exonerate output, but not in "
"reference sequences. This should not be possible.\n";
good_bye_and_exit(-127);
return 0; // Only introduced to silence warnings about no return for non-void function.
}
}
private:
void parse(faststring &str)
{
bool_has_M = false;
bool_has_G = false;
bool_has_F = false;
bool_has_5 = false;
bool_has_3 = false;
bool_has_C = false;
bool_has_N = false;
bool_has_I = false;
bool_has_S = false;
bool_has_non_M = false;
attributes.clear();
vector<faststring> l(19);
split(l, str);
if (l.size() < 13) // 1+9+3
{
cerr << "ERROR: Bad vulgar string encountered with a wrong number of elements: " << str << endl;
good_bye_and_exit(-4);
}
if (l[0] != "vulgar:")
{
cerr << "ERROR: Bad vulgar string: The vulgar string is expected to start with \"vulgar:\" but found: " << str << endl;
good_bye_and_exit(-4);
}
// It is a convention in exonerate that the protein sequence is the query and the DNA sequence the target:
queryID = l[1];
query_start = l[2].ToUnsigned();
query_end = l[3].ToUnsigned();
query_strand = l[4][0];
targetID = l[5];
target_start = l[6].ToUnsigned();
target_end = l[7].ToUnsigned();
target_strand = l[8][0];
score = l[9].ToUnsigned();
queryIndex = get_index_in_query2short_map(queryID);
int m = l.size()-10;
if (m%3 != 0)
{
cerr << "ERROR: Bad vulgar string encountered. The number of fields must "
"by 10+3*n where n is an integer. But the number is "
<< m << "." << endl;
cerr << " Vulgar string: " << str << endl;
good_bye_and_exit(-5);
}
unsigned k = 10;
Ctriple<char, unsigned, unsigned> t('\0', 0, 0);
while (k < l.size())
{
char c = l[k][0]; ++k;
unsigned num1 = l[k].ToUnsigned(); ++k;
unsigned num2 = l[k].ToUnsigned(); ++k;
if (c == 'M')
bool_has_M = true;
else if (c == 'G')
{
bool_has_G = true;
bool_has_non_M = true;
}
else if (c == 'F')
{
bool_has_F = true;
bool_has_non_M = true;
}
else if (c == '5')
{
bool_has_5 = true;
bool_has_non_M = true;
}
else if (c == '3')
{
bool_has_3 = true;
bool_has_non_M = true;
}
else if (c == 'C')
{
bool_has_C = true;
bool_has_non_M = true;
}
else if (c == 'N')
{
bool_has_N = true;
bool_has_non_M = true;
}
else if (c == 'I')
{
bool_has_I = true;
bool_has_non_M = true;
}
else if (c == 'S')
{
bool_has_S = true;
bool_has_non_M = true;
}
t = Ctriple<char, unsigned, unsigned>(c, num1, num2);
attributes.push_back(t);
}
}
/* Does not seem to make sense to combine F and G. There is a simple and consistent interpretation of the two independent of the other.
void combine_FG_attributes()
{
unsigned i, N=attributes.size();
for (i=1; i<N; ++i)
{
if (attributes[i-1].first() == 'G' && attributes[i].first() == 'F')
{
if (attributes[i-1].second()>0 && attributes[i-1].third() == 0 && (attributes[i].second()==0 && attributes[i-1].third() > 0) )
{
Ctriple<char, unsigned, unsigned> t('\0', 0, 0);
t = Ctriple<char, unsigned, unsigned>('f', attributes[i-1].second(), attributes[i].third());
attributes[i-1] = t;
attributes.erase(attributes.begin()+i);
// Do we have to adapt i after removing an element. In this case no, since we will not combine anything with the newly inserted feature 'f'.
}
else
{
cout << "WARNING: Unexpected combination of lengths in successive G and F attribute in vulgar line:\n";
print(cout);
}
}
else if (attributes[i-1].first() == 'F' && attributes[i].first() == 'G')
{
if (attributes[i-1].second()==0 && attributes[i-1].third() >0 && (attributes[i].second()>0 && attributes[i].third() == 0) )
{
Ctriple<char, unsigned, unsigned> t('\0', 0, 0);
t = Ctriple<char, unsigned, unsigned>('f', attributes[i].second(), attributes[i-1].third());
attributes[i-1] = t;
attributes.erase(attributes.begin()+i);
// Do we have to adapt i after removing an element. In this case no, since we will not combine anything with the newly inserted feature 'f'.
}
else
{
cout << "WARNING: Unexpected combination of lengths in successive F and G attributes in vulgar line:\n";
print(cout);
}
}
}
}
*/
public:
void print(ostream &os) const
{
os << "vulgar: "
<< queryID << " "
// << queryIndex << " "
<< query_start << " "
<< query_end << " "
<< query_strand << " "
<< targetID << " "
<< target_start << " "
<< target_end << " "
<< target_strand << " "
<< score << " ";
for (unsigned i=0; i<attributes.size(); ++i)
{
os << attributes[i].first() << " ";
os << attributes[i].second() << " ";
os << attributes[i].third() << " ";
}
}
vulgar(faststring &str)
{
parse(str);
// combine_FG_attributes();
}
const faststring & get_queryID() const
{
return queryID;
}
short get_queryIndex() const
{
return queryIndex;
}
const faststring & get_targetID() const
{
return targetID;
}
faststring get_hitkey() const
{
return queryID + "##" + targetID;
}
bool has_F() const
{
return bool_has_F;
}
bool has_G() const
{
return bool_has_G;
}
bool has_M() const
{
return bool_has_M;
}
bool has_non_M() const
{
return bool_has_non_M;
}
unsigned num_attributes() const
{
return attributes.size();
}
unsigned get_query_start() const
{
return query_start;
}
unsigned get_target_start() const
{
return target_start;
}
unsigned get_target_end() const
{
return target_end;
}
bool is_revcomp() const
{
return (target_strand == '-');
}
// Divides the exonerate score by the length of the hit and returns the result.
double relative_score() const
{
double dist;
if (target_end > target_start)
dist = target_end - target_start;
else
dist = target_start - target_end;
if (dist == 0)
return 0;
return score/dist;
}
unsigned get_score()
{
return score;
}
};
// Global variable definition for static member variable of vulgar class:
map<faststring, short> vulgar::queryID2Index;
bool fileExists(const char *filename)
{
ifstream is(filename);
if (is.fail())
return false;
else
return true;
}
inline unsigned size_keyset_multimap(multimap<faststring, vulgar *> &mm)
{
set<pair<faststring, faststring> > s;
multimap<faststring, vulgar *>::iterator it = mm.begin();
while (it != mm.end())
{
vulgar &vul = *(it->second);
s.insert(make_pair(vul.queryID, vul.targetID));
++it;
}
return s.size();
}
inline unsigned number_of_entries_targetID(multimap<faststring, vulgar *> &mm, faststring targetID)
{
return mm.count(targetID);
}
inline unsigned number_of_entries_targetID_and_queryID(multimap<faststring, vulgar *> &mm,
faststring targetID,
faststring &quereyID)
{
pair<multimap<faststring, vulgar *>::iterator, multimap<faststring, vulgar *>::iterator> it_bounds_pair;
multimap<faststring, vulgar *>::iterator it, it_end;
it_bounds_pair = mm.equal_range(targetID);
it = it_bounds_pair.first;
it_end = it_bounds_pair.second;
unsigned count = 0;
while (it != it_end)
{
if ( (*(it->second)).get_queryID() == quereyID )
++count;
++it;
}
return count;
}
// Determines the aligned sequence newseqDNA
void determine_alignment_string(CSequence_Mol* theSeq, const vulgar &vul, unsigned query_prot_length,
CDnaString &newseqDNA, unsigned seq_count,
map<pair<unsigned, unsigned>, unsigned> &map_of_gap_sites_in_queries,
map<pair<unsigned, unsigned>, unsigned> &map_of_insertion_sites_suggested_in_reference)
{
// The query prot sequence start coordinate sets the number of gaps before:
unsigned gaps_before = vul.query_start*3;
unsigned start_in_seqDNA, end_in_seqDNA;
CDnaString seqDNA;
if (global_verbosity >= 100)
{
cerr << "determine_alignment_string was called with the following parameters:\n"
<< "query_prot_length: " << query_prot_length << endl;
cerr << "Reference (query): " << vul.get_queryID() << endl;
cerr << "Reference (target): " << vul.get_targetID() << endl;
}
unsigned length_this_nuc_seq = theSeq->length();
// Compute the rev comp once, so we do not have to handle so many different cases.
if (vul.is_revcomp())
{
seqDNA.setToReverseComplementOf( theSeq->getSeq_faststring());
/*
unsigned len = vul.get_target_start() - vul.get_target_end();
CDnaString tmp = theSeq->getPartialSeq(vul.get_target_end(),len);
seqDNA.setToReverseComplementOf( tmp.c_str() );
*/
start_in_seqDNA = length_this_nuc_seq - vul.get_target_start();
end_in_seqDNA = length_this_nuc_seq - vul.get_target_end();
}
else
{
// See coordinate system above. The coordinates are 0 based. The end is the index after the range.
seqDNA = theSeq->getSeq_faststring();
/*
unsigned len = vul.get_target_end() - vul.get_target_start();
//->getPartialSeq(vul.get_target_start(),len);
*/
start_in_seqDNA = vul.get_target_start();
end_in_seqDNA = vul.get_target_end();
}
/* // TODO: What do we do with this code. Add options!!!
if (vul.num_attributes() != 1) // if we have no non-M labels we expect only one attribute in the list of attributes.
{
cerr << "ERROR: Unexpected number of attributes for exonerate output: ";
vul.print(cerr);
cerr << "\n";
good_bye_and_exit(-19);
}
*/
// Problem: exonerate only aligns complete codons to amino acids
// Test for consistent bases in partial codons:
// Exonerate aligns the reads to the amino acid sequence. Only complete codons are considered as alignment matches.
// So using the coordinates obtained from exonerate, a partial codon is removed in 2/3 of the cases at the beginning as well as at the end.
//
// We might want to add unmatched bp before and after, e.g. for partial codons or in order to see how much they differ from other sequences.
// In order not to intervene with multiple attributes, we treat that case
// that we add unmatched residues before and after as special cases.
// If would only expect to find a single M attribute, the logic would be much simpler.
faststring add_seq_before;
faststring add_seq_after;
// global_num_bp_beyond_exonerate_alignment_if_at_start_or_end = 6;
// Code can still be simplified !!!
// Handling recomp is done by computing the reverse complement already for the template sequence.
// This makes the following code simpler! The final step, i.e. not to distinguishing the two cases any more could now be taken.
if (global_num_bp_beyond_exonerate_alignment_if_at_start_or_end > 0)
{
if (gaps_before > 0)
{
if (global_verbosity >= 1000)
{
cout << endl;
cout << "Sequence: " << seq_count << endl;
cout << "gaps_before: " << gaps_before << endl;
}
if (vul.is_revcomp())
{
unsigned add_length_beginning = 0;
unsigned new_start_in_seqDNA;
if (global_verbosity >= 1000)
cout << "Number add_length_beginning: " << add_length_beginning << endl;
if (start_in_seqDNA > 0) // Could be 0, and nothing should be done
{
add_length_beginning = start_in_seqDNA;
if (global_num_bp_beyond_exonerate_alignment_if_at_start_or_end < add_length_beginning)
add_length_beginning = global_num_bp_beyond_exonerate_alignment_if_at_start_or_end;
if (gaps_before < add_length_beginning)
add_length_beginning = gaps_before;
new_start_in_seqDNA = start_in_seqDNA - add_length_beginning;
if (add_length_beginning > 0)
add_seq_before = seqDNA.substr(new_start_in_seqDNA, add_length_beginning);
}
}
else // Not revcomp:
{
unsigned add_length_beginning = 0;
unsigned new_start_in_seqDNA;
// We could also handle the case:
// vul.get_target_start() > global_num_bp_beyond_exonerate_alignment_if_at_end
// not a condition any more.
if ( start_in_seqDNA > 0) // Could be == 0.
{
add_length_beginning = start_in_seqDNA;
if (global_num_bp_beyond_exonerate_alignment_if_at_start_or_end < add_length_beginning)
add_length_beginning = global_num_bp_beyond_exonerate_alignment_if_at_start_or_end;
if (gaps_before < add_length_beginning)
add_length_beginning = gaps_before;
new_start_in_seqDNA = start_in_seqDNA - add_length_beginning;
if (add_length_beginning > 0)
add_seq_before = seqDNA.substr(new_start_in_seqDNA, add_length_beginning);
}
if (0)
{
cerr << "DEBUG: Information on sequence data printed in lower case before the matching sequence:\n";
cerr << "Sequence name: " << theSeq->getName() << endl;
cerr << "Starting coordinate of + strand: " << new_start_in_seqDNA << endl;
cerr << "Length: " << add_length_beginning << endl;
cerr << "String: " << add_seq_before << endl;
// faststring seq_before_start = add_partial_condons_before;
// cout << endl;
// cout << "key: " << key << endl;
// seq_before_start += "|";
// seq_before_start += theSeq->getPartialSeq(vul.get_target_start(), 12);
// faststring aa_at_seq_before_start = seqs_target.get_seq_by_index(0)->getPartialSeq(vul.query_start-1, 5);
// cout << "gaps_before: " << gaps_before << " vul.get_target_start(): " << vul.get_target_start() << endl;
// cout << "Sequence before start: " << seq_before_start << endl;
// cout << "AA at this posiiton: " << aa_at_seq_before_start << endl;
}
} // END Not revcomp
if (global_verbosity >= 1000)
cout << "Add partial codon front \"" << add_seq_before << "\" might have length 0." << endl;
if (global_verbosity >= 50)
cout << "Add front length: " << add_seq_before.length() << " " << theSeq->getName() << endl;
} // END if (gaps_before > 0)
} // END if (global_num_bp_beyond_exonerate_alignment_if_at_end > 0)
// cout << "Add gaps before: " << gaps_before << endl;
gaps_before -= add_seq_before.length();
newseqDNA = faststring('-', gaps_before);
// Debug code:
add_seq_before.ToLower();
if (add_seq_before.size() > 0)
newseqDNA += add_seq_before;
unsigned DNA_pos = start_in_seqDNA;
unsigned amino_pos = vul.query_start; // Only needed to store insertions with respect to reference.
unsigned count = gaps_before + add_seq_before.length();
// For all vulgar features: Usually only one: The M feature.
for (unsigned i=0; i < vul.attributes.size(); ++i)
{
/* These hits should never be handled here:
if (global_relative_score_threshold && vul.relative_score() < global_relative_score_threshold)
{
cerr << "NOTE: Exonerate hit skipped due to low relative alignment score: "
<< vul.relative_score()
<< " " << vul.get_targetID() << endl;
}
// cout << "Working on vulgar with target_strand: " << vul.target_strand << endl;
else */
if (vul.attributes[i].first() == 'M')
{
// cout << "Working on attribute i: " << i << " with " << endl;
if (!vul.is_revcomp())
{
// cout << "non-revcomp" << endl;
newseqDNA += seqDNA.substr(DNA_pos, vul.attributes[i].third());
DNA_pos += vul.attributes[i].third();
count += vul.attributes[i].third();
amino_pos += vul.attributes[i].second();
}
else
{
// cout << "revcomp" << endl;
// CDnaString revcompDNA = setToReverseComplementOf(seqDNA);
newseqDNA += seqDNA.substr(DNA_pos, vul.attributes[i].third());
DNA_pos += vul.attributes[i].third();
count += vul.attributes[i].third();
amino_pos += vul.attributes[i].second();
}
}
else if (vul.attributes[i].first() == 'F')
{
// Handling frameshift alignments is difficult. Often a 'F' and 'G' attribute appear together and can only
// be interpreted together.
if (global_include_frameshift_alignments)
{
// Frameshift rules:
// (I) If there is no G left or right, then the F nucleotides have to be removed with respect to the AA sequence.
// (II) If there is a G left or right, then the F nucleotides have to be added but the codon is not complete.
// ***** There is a remaining problem: How do we know whether the nucleotides are left or right
// ***** justified in the frameshift codon?
// ***** In the case (II) we have a G to the left or to the right. This might indicate the justification of the nucleotides.
// if (vul.attributes[i].third() <= 3)
{
cout << "Hello. F" << endl;
// cout << "Working on attribute i: " << i << " with " << endl;
if (!vul.is_revcomp())
{
if (vul.attributes[i].second() > 0)
{
amino_pos += vul.attributes[i].second();
++num_WARNINGS;
if (global_verbosity >= 1)
cerr << "WARNING: Unhandled rare case: F x y with x > 0." << endl;
}
else // second() == 0, => third() > 0
{
++num_WARNINGS;
if (global_verbosity >= 1)
cerr << "NOTE: Partial codons removed. F 0 x." << endl;
DNA_pos += vul.attributes[i].third();
}
}
else
{
if (vul.attributes[i].second() > 0)
{
amino_pos += vul.attributes[i].second();
++num_WARNINGS;
if (global_verbosity >= 1)
cerr << "WARNING: Unhandled rare case: F x y with x > 0." << endl;
}
else // second() == 0, => third() > 0
{
++num_WARNINGS;
if (global_verbosity >= 1)
cerr << "NOTE: Partial codons removed. F 0 x." << endl;
DNA_pos += vul.attributes[i].third();
}
}
}
}
}
else if (vul.attributes[i].first() == 'G')
{
// Hits with gaps and without global_include_gap_alignments are not considered at all, so we do not need this check here.
// if (global_include_gap_alignments)
{
// Gap in nucleotide sequence:
if (vul.attributes[i].second() > 0)
{
unsigned gap_chars = vul.attributes[i].second()*3;
newseqDNA += faststring('~', gap_chars);
count += gap_chars;
add_or_count(map_of_gap_sites_in_queries, make_pair(3*amino_pos, 3*vul.attributes[i].second() ));
amino_pos += vul.attributes[i].second();
}
else if (vul.attributes[i].third() > 0)
{
// This case is difficult to handle. It requires to add gaps in the reference sequence.
// Currently, this case is handled by removing the additional bases in the sequence added to the alignment.
++num_WARNINGS;
if (global_verbosity >= 1)
cout << "WARNING: Rare gap case: G 0 x. Additional bases are skipped." << endl;
DNA_pos += vul.attributes[i].third();
add_or_count(map_of_insertion_sites_suggested_in_reference, make_pair(3*amino_pos, 3*vul.attributes[i].third()));
}
}
}
else
{
cerr << "ERROR: Unexpected attribute in vulgar string: " << vul.attributes[i].first() << endl;
cerr << "Normally, a \'grep \" " << vul.attributes[i].first()
<< " \" vulgar-file-name \' should find the offending attribute in the vulgar file. " << endl;
good_bye_and_exit(-21);
}
} // END for (unsigned i=0; i < vul.attributes.size(); ++i)
if (query_prot_length*3 < count)
{
cerr << "ERROR: count is larger than length of target in nucleotides. "
"This internal error should be reported.\n"
"Vulgar hit for this problematic sequence:\n";
vul.print(cerr);
cerr << endl;
cerr << "query_prot_length " << query_prot_length << endl;
cerr << "query_prot_length*3 " << query_prot_length*3 << endl;
cerr << "count (nuc-length) " << count << endl;
good_bye_and_exit(-131);
}
// Now work on gaps after and add - after:
unsigned gaps_after = query_prot_length*3 - count;
if (global_verbosity >= 40)
cout << "DEBUG: " << theSeq->getName() << " gaps_after: " << gaps_after << endl;
if (global_num_bp_beyond_exonerate_alignment_if_at_start_or_end > 0)
{
if (gaps_after > 0)
{
if (global_verbosity >= 1000)
{
cout << endl;
cout << "Sequence: " << seq_count << endl;
cout << "gaps_after: " << gaps_after << endl;
}
if (vul.is_revcomp())
{
// Default values for start and length of extracted region:
unsigned add_length_end = length_this_nuc_seq - end_in_seqDNA;
// vul.get_target_end();
// Where in seqDNA do we start to extract the extra bases
unsigned new_start_in_seqDNA = end_in_seqDNA;
if (global_verbosity >= 1000)
cout << "Number potential_add_at_end_of_inserted_revcomp_seq: " << add_length_end << endl;
if (add_length_end > 0) // Could be 0, and nothing should be done
{
if (add_length_end > gaps_after)
{
add_length_end = gaps_after;
}
if (add_length_end > global_num_bp_beyond_exonerate_alignment_if_at_start_or_end)
{
add_length_end= global_num_bp_beyond_exonerate_alignment_if_at_start_or_end;
}
if (add_length_end > 0)
add_seq_after = seqDNA.substr(new_start_in_seqDNA, add_length_end);
// CDnaString tmp1 = theSeq->getPartialSeq(0, add_at_end);
CDnaString tmp2;
// tmp2.setToReverseComplementOf(tmp1);
// add_partial_condons_after = tmp2.c_str();
// faststring seq_before_start = add_partial_condons_before = theSeq->getPartialSeq(0, vul.get_target_start());
// cout << "Add after: " << add_partial_condons_after << endl;
} // END if (len_at_start_of_seq > 0 && (len_at_start_of_seq <= global_num_bp_beyond_exonerate_alignment_if_at_end) )
}
else // Not vul.is_revcomp()
{
unsigned add_length_end = length_this_nuc_seq - end_in_seqDNA;
// length_this_nuc_seq - vul.get_target_end();