forked from cmayer/BaitFisher-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbait-fisher.cpp
2199 lines (1874 loc) · 78.3 KB
/
bait-fisher.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
/* BaitFisher (version 1.2.7) a program for designing DNA target enrichment baits
* Copyright 2013-2016 by Christoph Mayer
*
* This source file is part of the BaitFisher-package.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
*
*
* For any enquiries send an Email to Christoph Mayer
*
* When publishing work that is based on the results please cite:
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
*
*/
#define NODEBUG
#include "faststring2.h"
#include <iostream>
#include "GFF-class.h"
#include "GFF-collection.h"
#include "mydir-unix.h"
#include <list>
#include "CFile/CFile2_1.h"
#include "CSequences2.h"
#include "CSequence_Mol2_1.h"
#include "CDnaString2.h"
#include <climits>
#include <ctime>
#include "CAligner/CAligner.h"
#include "scoring-matrices/CScoreMatrix.h"
#include "basic-DNA-RNA-AA-routines.h"
#include "CDistance_matrix.h"
#include "CSeqNameList.h"
#include "CTaxonNamesDictionary.h"
#include "CRequiredTaxon.h"
//#include "pipeline-step3.h"
#include "Csequence_cluster_and_center_sequence.h"
#include "DEBUG_STUFF.h"
#define PROGNAME "BaitFisher"
#define VERSION "1.2.7"
using namespace std;
#include "bait-fisher-helper.h"
// Verbosity rules:
// verbosity 0 -> only error messages that lead to exit() (goes to cerr)
// verbosity 1 -> warnings, ALERT (goes to cerr)
// verboisty 2 -> more warinings, e.g. skipping features
// verbosity 3 -> progress (goes to cout)
// verbosity 4 -> more progress (goes to cout)
// verbosity 6 -> per alignment/feautre success NOTES
// verbosity 8 -> per coordinate success NOTES
// verbosity >=20: DEGUB
// Default: 5
unsigned global_VERBOSITY = 0; // Will be initialized in parse_parameter_file. This value will never be used.
// Goal of this program:
//
int DEBUG_COUNT_NEW_CALLS_FEATURE_ALIGNMENT = 0;
int DEBUG_COUNT_NEW_CALLS_CBAIT_REGION_INFORMATION = 0;
int DEBUG_COUNT_NEW_CALLS_CSEQUENCE_LOCI_CLUSTER_COLLECTION = 0;
int DEBUG_COUNT_NEW_CALLS_MSA = 0;
int DEBUG_COUNT_NEW_ALIGNMENT_FROM_FILE = 0;
int DEBUG_COUNT_DELETE_CALLS_FEATURE_ALIGNMENT = 0;
int DEBUG_COUNT_DELETE_CALLS_CBAIT_REGION_INFORMATION = 0;
int DEBUG_COUNT_DELETE_CALLS_CSEQUENCE_LOCI_CLUSTER_COLLECTION = 0;
int DEBUG_COUNT_DELETE_CALLS_MSA = 0;
int DEBUG_COUNT_DELETE_ALIGNMENT_FROM_FILE = 0;
void print_DEBUG_COUNT (ostream &os, const char * comment)
{
os << "DEBUG: NEW/DELETE COUNTs: " << comment << endl;
os << "DEBUG: NUMBER OF ALIGNMENTS CREATED/DELETED: " << DEBUG_COUNT_NEW_ALIGNMENT_FROM_FILE << "/" << DEBUG_COUNT_DELETE_ALIGNMENT_FROM_FILE << endl;
os << "DEBUG: NUMBER OF FEATUREALIGNMENT C/D: " << DEBUG_COUNT_NEW_CALLS_FEATURE_ALIGNMENT << "/" << DEBUG_COUNT_DELETE_CALLS_FEATURE_ALIGNMENT << endl;
os << "DEBUG: MSA CREATED/DELETED " << DEBUG_COUNT_NEW_CALLS_MSA << "/" << DEBUG_COUNT_DELETE_CALLS_MSA << endl;
os << "DEBUG: CSEQUENCE_LOCI_CLUSTER_COLLECTION C/D: " << DEBUG_COUNT_NEW_CALLS_CSEQUENCE_LOCI_CLUSTER_COLLECTION << "/" << DEBUG_COUNT_DELETE_CALLS_CSEQUENCE_LOCI_CLUSTER_COLLECTION << endl;
os << "DEBUG: CBAIT_REGION_INFORMATION C/D: " << DEBUG_COUNT_NEW_CALLS_CBAIT_REGION_INFORMATION << "/" << DEBUG_COUNT_DELETE_CALLS_CBAIT_REGION_INFORMATION << endl;
}
void get_time_string(faststring &str)
{
time_t rawtime;
struct tm *timeinfo;
static char buffer[80];
std::time (&rawtime);
timeinfo = std::localtime(&rawtime);
std::strftime (buffer,80,"%F %T",timeinfo);
str = faststring(buffer);
}
template<class T>
int delete_pointers_in_vector(vector<T> &v)
{
int i, N, count=0;
N = v.size();
for (i=0; i<N; ++i)
{
if (v[i])
{
++count;
delete v[i];
}
}
v.clear();
return count;
}
// Add more checks!! Currently we do not check whether parameters occur twice, etc.
void parse_parameter_file(faststring & fname_parameter_file,
faststring & fname_gff,
faststring & fname_genome,
faststring & string_required_taxa,
faststring & dname_alignments,
faststring & genome_name,
unsigned short & bait_length,
double & cluster_threshold,
faststring & output_directory,
unsigned short & bait_offset,
unsigned short & bait_number,
bool & remove_reference_species_from_alignments,
bool & alignment_cutting,
faststring & gff_feature_name,
faststring & gff_record_attribute_name_for_geneID,
char & sequence_name_field_delimiter,
unsigned short & sequence_name_taxon_field_number,
unsigned short & sequence_name_geneID_field_number,
bool & sequence_name_tokenizer_overwritten,
char & center_computation_mode,
unsigned & verbosity
)
{
fname_gff.clear();
fname_genome.clear();
string_required_taxa.clear();
dname_alignments.clear();
genome_name.clear();
bait_length = 0;
cluster_threshold = -1;
output_directory.clear();
bait_offset = USHRT_MAX;
bait_number = USHRT_MAX;
remove_reference_species_from_alignments = false;
alignment_cutting = false;
sequence_name_field_delimiter = '|';
sequence_name_taxon_field_number = 2;
sequence_name_geneID_field_number = 3;
sequence_name_tokenizer_overwritten = false;
center_computation_mode = 1; // 0:exhaustive, 1:heuristic, 3: special comparison
verbosity = 5;
faststring line, pre, post;
ifstream is;
is.open(fname_parameter_file.c_str());
if (is.fail() )
{
cerr << "Error: Parameter file \"" << fname_parameter_file << "\" could not be opened. I guess it does not exist." << endl;
exit(-1);
}
getline(is, line);
while (!is.fail() )
{
line.shorten_to_first_occurrence_of('#');
line.removeSpacesBack();
line.removeSpacesFront();
if ( line.empty() )
{
getline(is, line);
continue;
}
if(line.divide_at_first_of(" \t",pre, post))
{
pre.removeSpacesBack();
post.removeSpacesFront();
}
else
{
pre = line;
post.clear();
}
if (pre == "gff-file-reference-genome")
{
fname_gff = post;
}
else if (pre == "reference-genome-file")
{
fname_genome = post;
}
else if (pre == "required-taxonomic-groups-string")
{
string_required_taxa = post;
}
else if (pre == "directory-transcript-files")
{
dname_alignments = post;
}
else if (pre == "reference-genome-name")
{
genome_name = post;
}
else if (pre == "bait-length")
{
bait_length = post.ToUnsigned();
}
else if (pre == "bait-offset")
{
bait_offset = post.ToUnsigned();
}
else if (pre == "bait-number")
{
bait_number = post.ToUnsigned();
}
else if (pre == "cluster-threshold")
{
cluster_threshold = post.ToDouble();
}
else if (pre == "output-directory")
{
output_directory = post;
}
else if (pre == "remove-reference-sequence")
{
post.ToLower();
if (post == "yes")
remove_reference_species_from_alignments = true;
else if (post == "no")
remove_reference_species_from_alignments = false;
else
{
cerr << "Error: Unknown option " << post << " for parameter " << pre << endl;
cerr << "The " << pre << " parameter will be ignored." << endl;
exit(-7);
}
}
else if (pre == "alignment-cutting")
{
post.ToLower();
if (post == "yes")
alignment_cutting = true;
else if (post == "no")
alignment_cutting = false;
else
{
cerr << "Error: Unknown option " << post << " for parameter " << pre << endl;
cerr << "The " << pre << " parameter will be ignored." << endl;
exit(-7);
}
}
else if (pre == "gff-feature-name")
{
gff_feature_name = post;
}
else if (pre == "gff-record-attribute-name-for-geneID")
{
gff_record_attribute_name_for_geneID = post;
}
else if (pre == "sequence-name-field-delimiter")
{
if (post.size()!=1)
{
cerr << "ERROR when parsing the parameter file: sequence-name-field-delimiter must be a single character, but I found "
<< sequence_name_field_delimiter << "." << endl;
exit(-1);
}
sequence_name_field_delimiter = post[0];
}
else if (pre == "sequence-name-taxon-field_number")
{
sequence_name_taxon_field_number = post.ToUnsigned();
}
else if (pre == "sequence-name-geneID-field_number")
{
sequence_name_geneID_field_number = post.ToUnsigned();
}
else if (pre == "verbosity")
{
verbosity = post.ToUnsigned();
}
else if (pre == "Hamming-center-sequence-computation-mode" || pre == "centroid-computation-mode")
{
if (post == "exhaustive")
{
center_computation_mode = 0;
}
else if (post == "heuristic")
{
center_computation_mode = 1;
}
else if (post == "comparison")
{
center_computation_mode = 3;
}
else
{
cerr << "Unknown option for parameter \"center-computation-mode\" in parameter file. Allowed options are: \"exhaustive\" and \"heuristic\"." << endl;
exit(-1);
}
}
else
{
cerr << "Error: Unknown parameter in parameter file: \"" << pre << "\"."<< endl;
exit(-1);
}
getline(is, line);
} // END while (!is.fail() )
// Basic consitency checking of parameters.
// Are all required parameters supplied?
if (alignment_cutting) // Then we require some parameters:
{
if (fname_gff.empty())
{
cerr << "Error: The parameter \"gff-file\" is required if alignment_cutting is set to yes. This parameter must be specified in the parameter file." << endl;
exit(-4);
}
if (fname_genome.empty() )
{
cerr << "Error: The parameter \"genome-file\" is required if alignment_cutting is set to yes. This parameter must be specified in the parameter file." << endl;
exit(-4);
}
if (genome_name.empty() )
{
cerr << "Error: The parameter \"genome-name\" is required if alignment_cutting is set to yes. This parameter must be specified in the parameter file." << endl;
exit(-4);
}
// Parameters with default values:
if (gff_feature_name.empty() )
gff_feature_name = "CDS";
if (gff_record_attribute_name_for_geneID.empty() )
gff_record_attribute_name_for_geneID = "Parent";
}
else // No alignment cutting
{
if (!fname_gff.empty())
{
cerr << "Error: The parameter \"gff-file\" is specified even though alignment_cutting is set to no. Please remove this parameter from the parameter file." << endl;
exit(-4);
}
if (!fname_genome.empty() )
{
cerr << "Error: The parameter \"genome-file\" is specified even though alignment_cutting is set to no. Please remove this parameter from the parameter file." << endl;
exit(-4);
}
if (!genome_name.empty() )
{
cerr << "Error: The parameter \"genome-name\" is specified even though alignment_cutting is set to no. Please remove this parameter from the parameter file." << endl;
exit(-4);
}
if (!gff_feature_name.empty() )
{
cerr << "Error: The parameter \"gff-feature-name\" is specified even though alignment_cutting is set to no. Please remove this parameter from the parameter file." << endl;
exit(-4);
}
if (!gff_record_attribute_name_for_geneID.empty() )
{
cerr << "Error: The parameter \"gff-record-attribute-name-for-geneID\" is specified even though alignment_cutting is set to no. Please remove this parameter from the parameter file." << endl;
exit(-4);
}
}
// if (string_required_taxa.empty() )
// {
// cerr << "Error: The required parameter \"required-taxonomic-groups-string\" is not specified in the parameter file." << endl;
// exit(-4);
// }
if (dname_alignments.empty() )
{
cerr << "Error: The required parameter \"directory-transcript-files\" is not specified in the parameter file." << endl;
exit(-4);
}
if (bait_length == 0)
{
cerr << "Error: Required parameter \"bait-length\" is not specified in the parameter file." << endl;
exit(-4);
}
if (cluster_threshold == -1)
{
cerr << "Error: Required parameter \"cluster-threshold\" is not specified in the parameter file." << endl;
exit(-4);
}
if ( output_directory.empty() )
{
cerr << "Error: Required parameter \"output-directory\" is not specified in the parameter file." << endl;
exit(-4);
}
if (bait_number == SHRT_MAX)
{
cerr << "Error: Required parameter \"bait-number\" is not specified in the parameter file." << endl;
exit(-4);
}
if (bait_number == 0)
{
cerr << "Error: Parameter \"bait-number\" has been set to 0 in the parameter file, which is not a valid value." << endl;
exit(-4);
}
if (bait_offset == USHRT_MAX)
{
cerr << "Error: Required parameter \"bait-offset\" is not specified in the parameter file." << endl;
exit(-4);
}
if (bait_offset == 0 && bait_number > 1)
{
cerr << "Error: The parameter \"bait-offset\" has been set to 0 in the parameter file, while the bait number is larger than 1. This does not make sense." << endl;
cerr << "Please specify a valid tiling design with bait number equal to 1 and bait offset equal to 0, or bait number larger than 1 and offset larger than 0." << endl;
exit(-4);
}
if (!alignment_cutting && string_required_taxa.empty() ) // In this case, sequence names do not need to contain the taxon name.
{
sequence_name_field_delimiter = '\0';
sequence_name_taxon_field_number = 1;
sequence_name_geneID_field_number = 1;
sequence_name_tokenizer_overwritten = true;
}
// The field numbers are stored 0-based. In the user interaction, numbers are 1-based.
--sequence_name_geneID_field_number;
--sequence_name_taxon_field_number;
}
void print_parameters(ostream & os,
faststring & fname_gff,
faststring & fname_genome,
faststring & string_required_taxa,
faststring & dname_alignments,
faststring & genome_name,
unsigned short bait_length,
double cluster_threshold,
faststring & output_directory,
unsigned short bait_offset,
unsigned short bait_number,
bool remove_reference_species_from_alignments,
bool alignment_cutting,
faststring & gff_feature_name,
faststring & gff_record_attribute_name_for_geneID,
char sequence_name_field_delimiter,
unsigned short sequence_name_taxon_field_number,
unsigned short sequence_name_geneID_field_number,
bool sequence_name_tokenizer_overwritten,
char center_computation_mode,
unsigned verbosity
)
{
os << "Welcome to the " << PROGNAME << " program, version " << VERSION << endl << endl
<< "This software and any of its components are copyright protected by the authors" << endl
<< "Christoph Mayer and Oliver Niehuis, Zoologisches Forschungsmuseum Alexander" << endl
<< "Koenig, Bonn, Germany. Copyright 2014." << endl
<< "This software is distributed as working material within a workshop given by" << endl
<< "Manuela Sann and Oliver Niehuis at the Australian National University, Canberra." << endl
<< "As long as it is not published, the software may only be used for the purposes of" << endl
<< "the workshop or with explicit permission of one of the authors." << endl;
os << "Redistribution as source code in part or in whole or in binary form is not permitted." << endl;
os << "The software comes without warranty of any kind." << endl << endl;
os << "PROGRAM has been started with the following parameters:" << endl;
os << "Alignment cutting " << (alignment_cutting ? "yes" : "no");
os << endl;
if (alignment_cutting)
{
os << "Name of gff file: " << fname_gff << endl;
os << "Name of genome file: " << fname_genome << endl;
os << "Name of genome: " << genome_name << endl;
os << "GFF feature to be excised from sequences: " << gff_feature_name << endl;
os << "GFF record attribute name for geneID: " << gff_record_attribute_name_for_geneID << endl;
}
if (string_required_taxa.empty())
os << "String with required taxonomic groups: " << "no taxa specified" << endl;
else
os << "String with required taxonomic groups: " << string_required_taxa << endl;
os << "Name of directory with transcriptome files: " << dname_alignments << endl;
os << "Cluster threshold: " << cluster_threshold << endl;
os << "Bait length: " << bait_length << endl;
os << "Bait offset (for multiple baits in region): " << bait_offset << endl;
os << "Bait number (for multiple baits in region): " << bait_number << endl;
os << "Output directory: " << output_directory << endl;
os << "Remove refernce species from alignment: " << (remove_reference_species_from_alignments ? "yes": "no") << endl;
if (sequence_name_tokenizer_overwritten)
{
os << "sequence_name_field_delimiter (default \"|\") " << "Not used since no required taxa specified and no aligment cutting." << endl;
// The field numbers are stored 0-based. In the user interaction, numbers are 1-based.
os << "Number of taxon field in sequence name: " << "Not used since no required taxa specified and no aligment cutting." << endl;
os << "Number of geneID field in sequence name: " << "Not used since no required taxa specified and no aligment cutting." << endl;
}
else
{
os << "sequence_name_field_delimiter (default \"|\") " << sequence_name_field_delimiter << endl;
// The field numbers are stored 0-based. In the user interaction, numbers are 1-based.
os << "Number of taxon field in sequence name: " << sequence_name_taxon_field_number+1 << endl;
os << "Number of geneID field in sequence name: " << sequence_name_geneID_field_number+1 << endl;
}
if (center_computation_mode == 0)
os << "Center computation mode: " << "exhaustive" << endl;
else if (center_computation_mode == 1)
os << "Center computation mode: " << "heuristic" << endl;
else
os << "Center computation mode: " << "comparison mode" << endl;
os << "Verbosity: " << verbosity << endl;
os << endl;
}
// Computes alignment and determines some characteristics:
// - alignment_score, alignment_length,
// - s1ins2s: starting pos of s1 in s2. If negative it is outside of s2 by the negative number (0 based)
// s1ins2e: position after last symbol of s1 in s2. If negative it is outside of s2 by the negative number (0 based)
// s2ins1s: similar to s1ins2s
// s2ins1e: similar to s2ins2e
inline void respective_sequence_positions(CScoreMatrix &DNAmat, faststring &s1, faststring &s2,
int &s1ins2s,
int &s1ins2e,
int &s2ins1s,
int &s2ins1e,
int &alignment_score,
int &alignment_length,
int verbosity)
{
// CScoreMatrix DNAmat("simpledna.mat"); // It is not efficient to open this file for many million short alignments.
short mode = 1; // local - free end gaps
std::ofstream log;
log.open("CAligner.log");
CAligner CA(s1.c_str(), s1.length(), s2.c_str(), s2.length(), DNAmat, mode, log);
faststring al;
align_coord a, e;
faststring l1, l2, l3;
CA.align();
alignment_score = CA.traceback(al, a, e);
CA.alignment_strings(l1, l2, l3, al);
if (verbosity >= 50)
{
cout << "Alignment score: " << alignment_score << endl;
cout << "Alignment:" << endl;
cout << l1 << endl;
cout << l3 << endl;
cout << l2 << endl;
}
CA.get_respective_start_end_coordinates(s2ins1s,
s2ins1e,
s1ins2s,
s1ins2e,
a,e,
s1.length(),
s2.length(),
al);
if (verbosity > 50)
{
std::cout << al << std::endl;
std::cout << "Score: " << alignment_score << std::endl;
std::cout << "Internal representation of start: "; a.print(std::cout); std::cout << std::endl;
std::cout << "Internal representation of end: "; e.print(std::cout); std::cout << std::endl;
// pretty_print_alignment_base0(stdout, l1, l2, l3, 100);
// pretty_print_alignment(stdout, l1, l2, l3, 100);
// std::cout << l1 << std::endl;
// std::cout << l3 << std::endl;
// std::cout << l2 << std::endl;
// std::cout << "Start: "; a.print(std::cout); std::cout << std::endl;
// std::cout << "End: "; e.print(std::cout); std::cout << std::endl;
std::cout << "Respective start/end (internal representation): "
<< "s2ins1s " << s2ins1s << " "
<< "s2ins1e " << s2ins1e << " "
<< "s1ins2s " << s1ins2s << " "
<< "s1ins2e " << s1ins2e << std::endl;
std::cout << "len s1 " << s1.length() << std::endl;
std::cout << "len s2 " << s2.length() << std::endl;
std::cout << "len al " << al.length() << std::endl;
}
alignment_length = al.length();
}
void find_coords(CScoreMatrix &DNAmat, CDnaString &s1, CDnaString &s2,
int &s1ins2s_best,
int &s1ins2e_best,
int &s2ins1s_best,
int &s2ins1e_best,
int &alignment_score_best,
int &alignment_length_best,
int verbosity)
{
// Coordinates in alignment, in px_y: x is first and second part, y is linker index.
char better;
int s1ins2s_tmp1;
int s1ins2e_tmp1;
int s2ins1s_tmp1;
int s2ins1e_tmp1;
int alignment_score_tmp1;
int alignment_length_tmp1;
int s1ins2s_tmp2;
int s1ins2e_tmp2;
int s2ins1s_tmp2;
int s2ins1e_tmp2;
int alignment_score_tmp2;
int alignment_length_tmp2;
// Try both: feature sequence and its reverse complement
respective_sequence_positions(DNAmat, s1, s2,
s1ins2s_tmp1, s1ins2e_tmp1, s2ins1s_tmp1, s2ins1e_tmp1, alignment_score_tmp1, alignment_length_tmp1,
verbosity);
CDnaString revcomp_s2;
revcomp_s2.setToReverseComplementOf(s2);
respective_sequence_positions(DNAmat, s1, revcomp_s2,
s1ins2s_tmp2, s1ins2e_tmp2, s2ins1s_tmp2, s2ins1e_tmp2, alignment_score_tmp2, alignment_length_tmp2,
verbosity);
if (alignment_score_tmp1 > alignment_score_tmp2)
{
s1ins2s_best = s1ins2s_tmp1;
s1ins2e_best = s1ins2e_tmp1;
s2ins1s_best = s2ins1s_tmp1;
s2ins1e_best = s2ins1e_tmp1;
alignment_score_best = alignment_score_tmp1;
alignment_length_best = alignment_length_tmp1;
better = 1;
}
else
{
s1ins2s_best = s1ins2s_tmp2;
s1ins2e_best = s1ins2e_tmp2;
s2ins1s_best = s2ins1s_tmp2;
s2ins1e_best = s2ins1e_tmp2;
alignment_score_best = alignment_score_tmp2;
alignment_length_best = alignment_length_tmp2;
better = 2;
}
if (verbosity >5)
{
cout << "Best alignment:" << endl;
cout << "Score: " << alignment_score_best << endl;
cout << "Direction: " << (better==1 ? "Same" : "RevComp") << endl;
}
}
bool check_full_coverage_and_required_taxa_in_range(CSequences2 *seqs, unsigned pos1, unsigned pos2,
const vector<unsigned> &unique_taxon_ids,
const CRequiredTaxa &required_groups_of_taxa)
{
unsigned N = seqs->GetTaxaNum();
unsigned i;
CSequence_Mol* seq;
set<unsigned> set_of_taxon_ids;
faststring taxon_name;
// Determine set of complete sequences (taxa) of this range.
// For each sequence in this range we check that it does not contains gaps or Ns.
// In the set_of_taxon_ids we store the unique taxon ID, not the sequence number.
for (i=0; i<N; ++i)
{
seq = seqs->get_seq_by_index(i);
// Has no gaps, Ns in range?
if (! seq->range_contains_gaps_or_Ns(pos1, pos2) )
{
// *seq is a good sequence in this range. (No gaps or Ns.)
set_of_taxon_ids.insert(unique_taxon_ids[i]);
}
}
// Checks whether there is any sequence left. In case of an empty required_taxa string
// this is an important check. Otherwise gap only regions could pass this test.
if (set_of_taxon_ids.empty())
{
if (global_VERBOSITY >= VERBOSITY_COORDINATES)
{
cout << "NOTE: Not a good bait window. No complete sequences in this range: " << pos1+1 << "-" << pos2
<< " Num seqs: " << set_of_taxon_ids.size() << endl;
}
return false;
}
if (required_groups_of_taxa.is_empty() )
{
if (global_VERBOSITY >= VERBOSITY_COORDINATES)
{
cout << "NOTE: Good bait window. Valid sequence range (no required taxa specified): " << pos1+1 << "-" << pos2 << " Num seqs: " << set_of_taxon_ids.size() << endl;
}
return true;
}
// Check that all required taxonomic groups are present in this feature:
if (required_groups_of_taxa.check_precense_of_all_groups_of_taxa(set_of_taxon_ids) )
{
if (global_VERBOSITY >= VERBOSITY_COORDINATES)
{
cout << "NOTE: Good bait window. All required taxonomic groups present in this range: " << pos1+1 << "-" << pos2 << " Num seqs: " << set_of_taxon_ids.size() << endl;
}
return true;
}
else
{
if (global_VERBOSITY >= VERBOSITY_COORDINATES)
{
cout << "NOTE: Not a good bait windows. NOT all required taxonomic groups present in this range." << pos1+1 << "-" << pos2 << " Num seqs: " << set_of_taxon_ids.size() << endl;
}
return false;
}
} // END check_full_coverage_and_required_taxa_in_range(...)
// Return true if at least one good bait region was found, otherwise it returns false.
bool handle_this_alignment(CSequences2 *alignment,
CTaxonNamesDictionary &taxonNamesDict,
CRequiredTaxa &required_groups_of_taxa,
unsigned short bait_length,
unsigned short number_of_baits_in_region,
unsigned short bait_offset,
faststring name, // gene name
unsigned short running_number,
unsigned short running_number_maximum,
double cluster_threshold,
ostream &os_result_file,
char delim,
unsigned short field_num,
char center_computation_mode,
const faststring &filename)
{
unsigned alignment_length = alignment->GetPosNum();
int region_length = (number_of_baits_in_region-1)*bait_offset+bait_length;
vector<unsigned> seqNum_to_unique_taxonID_this_alignment; // Used to translate the sequnce number in the current file to the unique taxon number.
vector<faststring> full_names_this_alignment;
vector<faststring> taxon_names_this_alignment;
set<faststring> taxon_set_this_alignment;
//DEBUG BREAK POINT - allows us to catch certain gene names for debugging purposes.
// {
// // os_result_file << "name: " << name << " " << running_number << endl;
//
// if (name == "NV18435" && running_number == 4)
// {
// cerr << "Found name: " << name << " " << running_number << endl;
// }
//
// }
//************************************************
// The following four arrays belong together.
//************************************************
// For each site in this msa is_valid_bait_window stores the information
// whether the site represents a good starting point and whether
// for the window starting here we need to do the clustering and determine
// the center sequence.
// For each site in the msa is_valid_bait_region stores the information
// whether we have a good bait region starting at this point.
// For each site cluster_loci_this_alignment stores the information about
// clusters and center sequences for the bait window starting at this point.
// If the window does not need to be computed, the pointer in this array is NULL.
faststring is_valid_bait_window;
faststring is_valid_bait_region;
vector<Csequence_loci_cluster_collection *> cluster_loci_this_alignment(alignment_length, NULL);
vector<CBait_region_information *> bait_region_info(alignment_length, NULL);
// TODO: currently, the is_valid_bait_window and is_valid_bait_region are filled up to the end.
// Last valid/possible index for is_valid_bait_window: alignment_length - bait_length
// Last valid/possible index for is_valid_bait_region: alignment_length - region_length
// Initialize is_valid_bait_window with fail character '-':
is_valid_bait_window.clear();
is_valid_bait_window.assign(alignment_length, '-');
is_valid_bait_region.clear();
is_valid_bait_region.assign(alignment_length, '-');
unsigned i;
for (i=alignment_length - bait_length+1; i< alignment_length; ++i)
is_valid_bait_window[i] = 'e'; // Indicates that this region extends beyond the end.
for (i=alignment_length - region_length+1; i< alignment_length; ++i)
is_valid_bait_region[i] = 'e'; // Indicates that this region extends beyond the end.
// cout << is_valid_bait_window.size() << endl;
// cout << is_valid_bait_window << endl;
// cout << is_valid_bait_region.size() << endl;
// cout << is_valid_bait_region << endl;
// Determine translation table: seqNum_to_unique_taxonID_this_alignment
// Clear the data for this alignment:
seqNum_to_unique_taxonID_this_alignment.clear();
full_names_this_alignment.clear();
// taxon_names_this_alignment.clear();
// taxon_set_this_alignment.clear();
alignment->get_sequence_names(full_names_this_alignment);
unsigned j;
faststring tmp_name;
// Determine vector that translates sequence number to unique_taxonIDs
for (j=0; j < full_names_this_alignment.size(); ++j)
{
tmp_name = extract_taxon_name(full_names_this_alignment[j], delim, field_num);
// taxon_names_this_alignment.push_back(sequence_to_taxon_name(full_names_this_alignment[j]));
// taxon_set_this_alignment.insert(taxon_names_this_alignment[j]);
// cout << taxon_names_this_alignment[j] << endl;
seqNum_to_unique_taxonID_this_alignment.push_back(taxonNamesDict.dictionary_get_index_of_taxonname(tmp_name));
// cout << j << "-->" << tmp_name << "->" << taxonNamesDict.dictionary_get_index_of_taxonname(tmp_name) << endl;
}
// Move through alignment sequence alignment and determine those parts which have a sufficient data coverage:
{
unsigned pos1, pos2;
bool is_valid_range;
pos1 = 0; // 0 based
pos2 = pos1 + bait_length;
// pos2 is the index after the last nucleotide in the bait.
// Thus pos2 is allowed to be equal to alignment_length, which specifies the index after
// the last nucleotide in the sequence.
while (pos2 <= alignment_length)
{
is_valid_range = check_full_coverage_and_required_taxa_in_range(alignment,
pos1, pos2,
seqNum_to_unique_taxonID_this_alignment,
required_groups_of_taxa);
if (is_valid_range)
is_valid_bait_window[pos1] = 'x'; // Indicates a valid bait window.
++pos1;
++pos2;
}
// cout << "Valid bait windows in alignment: " << endl << is_valid_bait_window << endl;
} // END Move through alignment sequence alignment
// cout << "Valid bait windows: " << endl;
// cout << is_valid_bait_window << endl;
unsigned num_good_regions=0;
// Determine good bait regions, i.e. with requested number of baits and starting points separated by an offset of bait_offset
{
// for all starting point of a bait region:
unsigned i, j, pos;
bool is_good_region;
// i is the start index of the current range.
for (i=0; i < alignment_length; ++i)
{
if (is_valid_bait_region[i] != 'e')
{
// cout << "Starting good: " << i << endl;
is_good_region = true;
// For all number_of_baits_in_region baits in one region with offset bait_offset
for (j=0, pos=i; j < number_of_baits_in_region; ++j, pos += bait_offset)
{
// Allowed values for is_valid_bait_window[pos] are 'c' and 'x'. Not allowed are: '-' and 'e'
// Some checks are redundant here: E.g. pos >= alignment_length and is_valid_bait_window[pos]=='e'
// should not be possilbe, since we check is_valid_bait_region[i] != 'e' before we get here.
// Doing these checks is more secure.
// TODO: In future versions check whether pos >= alignment_length and is_valid_bait_window[pos]=='e'
// are necessary.
if (pos >= alignment_length || is_valid_bait_window[pos] == '-' || is_valid_bait_window[pos]=='e')
{
// cout << "Not a good bait region: " << i << " " << pos << " " << is_valid_bait_window[pos] << endl;
is_good_region = false;
break; // This region is at best incomplete, so we mark it as bad.
}
}
if (is_good_region)
{
// cout << "Mark as good: " << i << endl;
++num_good_regions;
// Mark good region.
is_valid_bait_region[i] = 'x';
// cout << "Interm. " << i << is_valid_bait_region << endl;
// For all good bait regions we also allocate memory for the final colletion of the data:
bait_region_info[i] = new CBait_region_information(name, running_number, running_number_maximum, i, // bait_length, bait_offset,
number_of_baits_in_region);
// cout << "Intermediate is_valid_bait_region: " << endl << is_valid_bait_region << endl;
// For this valid bait region, we mark all windows as "need to be computed".
++DEBUG_COUNT_NEW_CALLS_CBAIT_REGION_INFORMATION;
for (j=0, pos=i; j < number_of_baits_in_region; ++j, pos += bait_offset)
{
if (pos >= alignment_length)
break;
is_valid_bait_window[pos] = 'c';
}
// cout << "Intermediate is_valid_bait_window: " << endl << is_valid_bait_window << endl;
if (global_VERBOSITY >= VERBOSITY_COORDINATES)
{
cout << "NOTE: Good bait region: " << i+1 << " " << i+1+region_length << endl;
}
}
else
{
if (global_VERBOSITY >= VERBOSITY_COORDINATES)
{
cout << "NOTE: Not a good bait region: " << i+1 << " " << i+1+region_length << endl;
}
}
} // END if (is_valid_bait_region[i] != 'e')
} // END for (i=0; i < alignment_length; ++i)
// cout << "Valid bait regions in feature: " << endl << is_valid_bait_region << endl;
// cout << "Bait windows that need to be computed" << endl << is_valid_bait_window << endl;
} // END Determine baits with requested number_of_baits_in_region good baits separated by an offset of bait_offset
// If we come here we have the following information:
// is_valid_bait_window contains a 'c' in all bait window starting indices that need to be looked at.
// is_valid_bait_region contains a 'x' in all bait region starting indices that we will explore,
// i.e. for which we have the necesarry successive bait windows.
// If this file has one or more regions that should be looked at:
// Cluster loci which need to be clustered.
// Compute center sequences.
if (num_good_regions > 0)
{
// OK - Where are we?
// We have extracted an feature. We determined the loci of possible
// bait regions in this feature. This number > 0.
// For all bait windows in this feature that belong to good bait regions,
// we cluster the sequences and compute an artificial center sequence for
// each cluster.
unsigned i;
unsigned num_taxa = alignment->GetTaxaNum();
// We need a recoded msa in order to compute artificial center sequences.
// We could do this for each window, but since the windows overlap, we
// do a lot of redudant work. Doing this per window we would only be on the positive
// side if there would be only very few windows in a long alignment.