forked from vidjil/vidjil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvidjil.cpp
1675 lines (1292 loc) · 54.2 KB
/
vidjil.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
/*
This file is part of Vidjil <http://www.vidjil.org>
Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 by Bonsai bioinformatics
at CRIStAL (UMR CNRS 9189, Université Lille) and Inria Lille
Contributors:
Mathieu Giraud <[email protected]>
Mikaël Salson <[email protected]>
Marc Duez <[email protected]>
"Vidjil" 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.
"Vidjil" 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 "Vidjil". If not, see <http://www.gnu.org/licenses/>
*/
//$$ #include
#include<algorithm>
#include<utility>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "core/tools.h"
#include "core/json.h"
#include "core/germline.h"
#include "core/kmerstore.h"
#include "core/fasta.h"
#include "core/segment.h"
#include "core/windows.h"
#include "core/cluster-junctions.h"
#include "core/dynprog.h"
#include "core/read_score.h"
#include "core/read_chooser.h"
#include "core/compare-all.h"
#include "core/mkdir.h"
#include "core/labels.h"
#include "core/list_utils.h"
#include "core/windowExtractor.h"
#include "lib/json.hpp"
#include "vidjil.h"
// RELEASE_TAG may be defined in the "release.h" file.
// If RELEASE_TAG is undefined, the version will be the git hash.
// #define RELEASE_TAG "2013.04"
#include "release.h"
// GIT_VERSION should be defined in "git-version.h", created by "create-git-version-h.sh", to be used outside of releases
#include "git-version.h"
#define VIDJIL_JSON_VERSION "2016b"
//$$ #define (mainly default options)
#define DEFAULT_MULTI_GERMLINE_PATH "germline/"
#define DEFAULT_MULTI_GERMLINE_FILE "germlines.data"
#define DEFAULT_READ_HEADER_SEPARATOR " "
#define DEFAULT_READS "./data/Stanford_S22.fasta"
#define DEFAULT_MIN_READS_CLONE 5
#define DEFAULT_MAX_REPRESENTATIVES 100
#define DEFAULT_MAX_CLONES 100
#define DEFAULT_RATIO_READS_CLONE 0.0
#define NO_LIMIT "all"
#define COMMAND_WINDOWS "windows"
#define COMMAND_CLONES "clones"
#define COMMAND_SEGMENT "segment"
#define COMMAND_GERMLINES "germlines"
enum { CMD_WINDOWS, CMD_CLONES, CMD_SEGMENT, CMD_GERMLINES } ;
#define DEFAULT_OUT_DIR "./out/"
// Fixed filenames/suffixes
#define CLONES_FILENAME ".vdj.fa"
#define CLONE_FILENAME "clone.fa-"
#define WINDOWS_FILENAME ".windows.fa"
#define SEGMENTED_FILENAME ".segmented.vdj.fa"
#define UNSEGMENTED_FILENAME ".unsegmented.vdj.fa"
#define UNSEGMENTED_DETAIL_FILENAME ".fa"
#define AFFECTS_FILENAME ".affects"
#define EDGES_FILENAME ".edges"
#define COMP_FILENAME "comp.vidjil"
#define JSON_SUFFIX ".vidjil"
// "tests/data/leukemia.fa"
#define DEFAULT_K 0
#define DEFAULT_W 50
#define DEFAULT_SEED ""
#define DEFAULT_DELTA_MIN -10
#define DEFAULT_DELTA_MIN_D 0
#define DEFAULT_MAX_AUDITIONED 2000
#define DEFAULT_RATIO_REPRESENTATIVE 0.5
#define DEFAULT_EPSILON 0
#define DEFAULT_MINPTS 10
#define DEFAULT_CLUSTER_COST Cluster
#define DEFAULT_SEGMENT_COST VDJ
#define DEFAULT_TRIM 0
#define MAX_CLONES_FOR_SIMILARITY 20
// warn
#define WARN_MAX_CLONES 100
#define WARN_PERCENT_SEGMENTED 40
// display
#define WIDTH_NB_READS 7
#define WIDTH_NB_CLONES 3
using namespace std ;
using json = nlohmann::json;
//$$ options: usage
extern char *optarg;
extern int optind, optopt, opterr;
void usage(char *progname, bool advanced)
{
cerr << "Usage: " << progname << " [options] <reads.fa/.fq/.gz>" << endl << endl;
cerr << "Command selection" << endl
<< " -c <command>"
<< "\t" << COMMAND_CLONES << " \t locus detection, window extraction, clone gathering (default command, most efficient, all outputs)" << endl
<< " \t\t" << COMMAND_WINDOWS << " \t locus detection, window extraction" << endl
<< " \t\t" << COMMAND_SEGMENT << " \t detailed V(D)J designation (not recommended)" << endl
<< " \t\t" << COMMAND_GERMLINES << " \t statistics on k-mers in different germlines" << endl
<< endl ;
if (advanced)
cerr << "Input" << endl
<< " -# <string> separator for headers in the reads file (default: '" << DEFAULT_READ_HEADER_SEPARATOR << "')" << endl
<< endl ;
cerr << "Germline databases (at least one -V/(-D)/-J, or -G, or -g option must be given for all commands except -c " << COMMAND_GERMLINES << ")" << endl
<< " -V <file> V germline multi-fasta file" << endl
<< " -D <file> D germline multi-fasta file (and resets -m and -w options), will segment into V(D)J components" << endl
<< " -J <file> J germline multi-fasta file" << endl
<< " -G <prefix> prefix for V (D) and J repertoires (shortcut for -V <prefix>V.fa -D <prefix>D.fa -J <prefix>J.fa) (basename gives germline code)" << endl
<< " -g <path> multiple locus/germlines. In the path <path>, takes 'germlines.data' to select locus and parameters" << endl
<< " Selecting '-g germline' processes TRA, TRB, TRG, TRD, IGH, IGK and IGL locus, possibly with some incomplete/unusal recombinations" << endl
<< " A different 'germlines.data' file can also be provided with -g <file>" << endl
<< endl
<< "Locus/recombinations" << endl
<< " -d try to detect several D (experimental)" << endl
<< " -i try to detect incomplete/unusual recombinations (locus with '+', must be used with -g)" << endl
<< " -2 try to detect unexpected recombinations (must be used with -g)" << endl
<< endl ;
if (advanced)
cerr << "Experimental options (do not use)" << endl
<< " -I ignore k-mers common to different germline systems (experimental, must be used with -g, do not use)" << endl
<< " -1 use a unique index for all germline systems (experimental, must be used with -g, do not use)" << endl
<< " -4 try to detect unexpected recombinations with translocations (experimental, must be used with -g, do not use)" << endl
<< " -! keep unsegmented reads as clones, taking for junction the complete sequence, to be used on very small datasets (for example -!AX 20)" << endl
<< endl
<< "Window prediction" << endl
#ifndef NO_SPACED_SEEDS
<< " (use either -s or -k option, but not both)" << endl
<< " (all these options, except -w, are overriden when using -g)" << endl
<< " -s <string> spaced seed used for the V/J affectation" << endl
<< " (default: #####-#####, ######-######, #######-#######, depends on germline)" << endl
#endif
<< " -k <int> k-mer size used for the V/J affectation (default: 10, 12, 13, depends on germline)" << endl
#ifndef NO_SPACED_SEEDS
<< " (using -k option is equivalent to set with -s a contiguous seed with only '#' characters)" << endl
#endif
<< " -w <int> w-mer size used for the length of the extracted window (default: " << DEFAULT_W << ")" << endl
<< " -e <float> maximal e-value for determining if a V-J segmentation can be trusted (default: " << THRESHOLD_NB_EXPECTED << ")" << endl
<< " -t <int> trim V and J genes (resp. 5' and 3' regions) to keep at most <int> nt (default: " << DEFAULT_TRIM << ") (0: no trim)" << endl
<< endl
<< "Labeled sequences (windows related to these sequences will be kept even if -r/-% thresholds are not reached)" << endl
<< " -W <sequence> label the given sequence" << endl
<< " -l <file> label a set of sequences given in <file>" << endl
<< " -F filter -- keep only the windows related to the labeled sequences" << endl
<< endl ;
cerr << "Limits to report a clone (or a window)" << endl
<< " -r <nb> minimal number of reads supporting a clone (default: " << DEFAULT_MIN_READS_CLONE << ")" << endl
<< " -% <ratio> minimal percentage of reads supporting a clone (default: " << DEFAULT_RATIO_READS_CLONE << ")" << endl
<< endl
<< "Limits to further analyze some clones" << endl
<< " -y <nb> maximal number of clones computed with a representative ('" << NO_LIMIT << "': no limit) (default: " << DEFAULT_MAX_REPRESENTATIVES << ")" << endl
<< " -z <nb> maximal number of clones to be analyzed with a full V(D)J designation ('" << NO_LIMIT << "': no limit, do not use) (default: " << DEFAULT_MAX_CLONES << ")" << endl
<< " -A reports and segments all clones (-r 0 -% 0 -y " << NO_LIMIT << " -z " << NO_LIMIT << "), to be used only on very small datasets (for example -AX 20)" << endl
<< " -x <nb> maximal number of reads to process ('" << NO_LIMIT << "': no limit, default), only first reads" << endl
<< " -X <nb> maximal number of reads to process ('" << NO_LIMIT << "': no limit, default), sampled reads" << endl
<< endl ;
if (advanced)
cerr << "Fine segmentation options (second pass)" << endl
<< " -f <string> use custom Cost for fine segmenter : format \"match, subst, indels, homo, del_end\" (default "<<VDJ<<" )"<< endl
<< " -m <int> minimal admissible delta between the end of the V and the start of the J (default: " << DEFAULT_DELTA_MIN << ") (default with -D: " << DEFAULT_DELTA_MIN_D << ")" << endl
<< " -E <float> maximal e-value for determining if a D segment can be trusted (default: " << THRESHOLD_NB_EXPECTED_D << ")" << endl
<< endl ;
cerr << "Clone analysis (second pass)" << endl
<< " -3 CDR3/JUNCTION detection (requires gapped V/J germlines)" << endl
<< endl ;
if (advanced)
cerr << "Additional clustering (experimental)" << endl
<< " -= <file> manual clustering -- a file used to force some specific edges" << endl
<< " -n <int> maximum distance between neighbors for automatic clustering (default " << DEFAULT_EPSILON << "). No automatic clusterisation if =0." << endl
<< " -N <int> minimum required neighbors for automatic clustering (default " << DEFAULT_MINPTS << ")" << endl
<< " -S generate and save comparative matrix for clustering" << endl
<< " -L load comparative matrix for clustering" << endl
<< " -C <string> use custom Cost for automatic clustering : format \"match, subst, indels, homo, del_end\" (default "<<Cluster<<" )"<< endl
<< endl ;
cerr << "Detailed output per read (generally not recommended, large files, but may be used for filtering, as in -uu -X 1000)" << endl
<< " -U output segmented reads (in " << SEGMENTED_FILENAME << " file)" << endl
<< " -u output unsegmented reads (in " << UNSEGMENTED_FILENAME << " file)" << endl
<< " -uu output unsegmented reads, gathered by unsegmentation cause, except for very short and 'too few V/J' reads (in *" << UNSEGMENTED_DETAIL_FILENAME << " files)" << endl
<< " -uuu output unsegmented reads, gathered by unsegmentation cause, all reads (in *" << UNSEGMENTED_DETAIL_FILENAME << " files) (use only for debug)" << endl
<< " -K output detailed k-mer affectation on all reads (in " << AFFECTS_FILENAME << " file) (use only for debug, for example -KX 100)" << endl
<< endl
<< "Output" << endl
<< " -o <dir> output directory (default: " << DEFAULT_OUT_DIR << ")" << endl
<< " -b <string> output basename (by default basename of the input file)" << endl
<< " -a output all sequences by cluster (" << CLONE_FILENAME << "*), to be used only on small datasets" << endl
<< " -v verbose mode" << endl
<< endl
<< " -h help" << endl
<< " -H help, including experimental and advanced options" << endl
<< "The full help is available in the doc/algo.org file."
<< endl
<< endl
<< "Examples (see doc/algo.org)" << endl
<< " " << progname << " -c clones -G germline/IGH -3 data/Stanford_S22.fasta" << endl
<< " " << progname << " -c clones -g germline -i -2 -3 data/Stanford_S22.fasta # (detect the locus for each read, including unusual/unexpected recombinations)" << endl
<< " " << progname << " -c windows -g germline -i -2 -uu -U data/Stanford_S22.fasta # (detect the locus, splits all the reads into large files)" << endl
<< " " << progname << " -c segment -G germline/IGH -3 data/Stanford_S22.fasta # (full analysis of each read, only for debug/testing)" << endl
<< " " << progname << " -c germlines data/Stanford_S22.fasta # (statistics on the k-mers)" << endl
;
exit(1);
}
int atoi_NO_LIMIT(char *optarg)
{
return strcmp(NO_LIMIT, optarg) ? atoi(optarg) : NO_LIMIT_VALUE ;
}
double atof_NO_LIMIT(char *optarg)
{
return strcmp(NO_LIMIT, optarg) ? atof(optarg) : NO_LIMIT_VALUE ;
}
int main (int argc, char **argv)
{
cout << "# Vidjil -- V(D)J recombinations analysis <http://www.vidjil.org/>" << endl
<< "# Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 by the Vidjil team" << endl
<< "# Bonsai bioinformatics at CRIStAL (UMR CNRS 9189, Université Lille) and Inria Lille" << endl
<< endl
<< "# Vidjil is free software, and you are welcome to redistribute it" << endl
<< "# under certain conditions -- see http://git.vidjil.org/blob/master/doc/LICENSE" << endl
<< "# No lymphocyte was harmed in the making of this software," << endl
<< "# however this software is for research use only and comes with no warranty." << endl
<< endl
<< "# Please cite http://biomedcentral.com/1471-2164/15/409 if you use Vidjil." << endl
<< endl ;
//////////////////////////////////
// Display version information or git log
string soft_version = "vidjil ";
#ifdef RELEASE_TAG
cout << "# version: vidjil " << RELEASE_TAG << endl ;
soft_version.append(RELEASE_TAG);
#else
cout << "# development version" << endl ;
#ifdef GIT_VERSION
cout << "# git: " << GIT_VERSION << endl ;
soft_version.append("dev ");
soft_version.append(GIT_VERSION);
#endif
#endif
//$$ options: defaults
string germline_system = "" ;
list <string> f_reps_V ;
list <string> f_reps_D ;
list <string> f_reps_J ;
list <pair <string, string>> multi_germline_paths_and_files ;
string read_header_separator = DEFAULT_READ_HEADER_SEPARATOR ;
string f_reads = DEFAULT_READS ;
string seed = DEFAULT_SEED ;
string f_basename = "";
string out_dir = DEFAULT_OUT_DIR;
string comp_filename = COMP_FILENAME;
int k = DEFAULT_K ;
int w = DEFAULT_W ;
int epsilon = DEFAULT_EPSILON ;
int minPts = DEFAULT_MINPTS ;
Cost cluster_cost = DEFAULT_CLUSTER_COST ;
Cost segment_cost = DEFAULT_SEGMENT_COST ;
bool detect_CDR3 = false;
int save_comp = 0;
int load_comp = 0;
int verbose = 0 ;
int command = CMD_CLONES;
int max_representatives = DEFAULT_MAX_REPRESENTATIVES ;
int max_clones = DEFAULT_MAX_CLONES ;
int min_reads_clone = DEFAULT_MIN_READS_CLONE ;
float ratio_reads_clone = DEFAULT_RATIO_READS_CLONE;
// int average_deletion = 4; // Average number of deletion in V or J
int max_reads_processed = NO_LIMIT_VALUE;
int max_reads_processed_sample = NO_LIMIT_VALUE;
float ratio_representative = DEFAULT_RATIO_REPRESENTATIVE;
unsigned int max_auditionned = DEFAULT_MAX_AUDITIONED;
// Admissible delta between left and right segmentation points
int delta_min = DEFAULT_DELTA_MIN ; // Kmer+Fine
int trim_sequences = DEFAULT_TRIM;
bool output_sequences_by_cluster = false;
bool output_segmented = false;
bool output_unsegmented = false;
bool output_unsegmented_detail = false;
bool output_unsegmented_detail_full = false;
bool output_affects = false;
bool keep_unsegmented_as_clone = false;
bool several_D = false;
bool multi_germline = false;
bool multi_germline_incomplete = false;
bool multi_germline_mark = false;
bool multi_germline_one_index_per_germline = true;
bool multi_germline_unexpected_recombinations_12 = false;
bool multi_germline_unexpected_recombinations_1U = false;
string forced_edges = "" ;
map <string, string> windows_labels ;
string windows_labels_file = "" ;
bool only_labeled_windows = false ;
char c ;
int options_s_k = 0 ;
double expected_value = THRESHOLD_NB_EXPECTED;
double expected_value_D = THRESHOLD_NB_EXPECTED_D;
//json which contains the Levenshtein distances
json jsonLevenshtein;
bool jsonLevenshteinComputed = false ;
//$$ options: getopt
while ((c = getopt(argc, argv, "A!x:X:hHadiI124g:G:V:D:J:k:r:vw:e:E:C:f:W:l:Fc:m:N:s:b:Sn:o:L%:y:z:uUK3=:t:#:")) != EOF)
switch (c)
{
case 'h':
usage(argv[0], false);
case 'H':
usage(argv[0], true);
case 'c':
if (!strcmp(COMMAND_CLONES,optarg))
command = CMD_CLONES;
else if (!strcmp(COMMAND_SEGMENT,optarg))
command = CMD_SEGMENT;
else if (!strcmp(COMMAND_WINDOWS,optarg))
command = CMD_WINDOWS;
else if (!strcmp(COMMAND_GERMLINES,optarg))
command = CMD_GERMLINES;
else {
cerr << "Unknwown command " << optarg << endl;
usage(argv[0], false);
}
break;
// Input
case '#':
read_header_separator = string(optarg);
break;
// Germline
case 'V':
f_reps_V.push_back(optarg);
germline_system = "custom" ;
break;
case 'D':
f_reps_D.push_back(optarg);
delta_min = DEFAULT_DELTA_MIN_D ;
break;
case 'J':
f_reps_J.push_back(optarg);
germline_system = "custom" ;
break;
case 'g':
multi_germline = true;
{
string arg = string(optarg);
struct stat buffer;
if (stat(arg.c_str(), &buffer) == 0)
{
if( buffer.st_mode & S_IFDIR )
{
// argument is a directory
multi_germline_paths_and_files.push_back(make_pair(arg, DEFAULT_MULTI_GERMLINE_FILE)) ;
}
else
{
multi_germline_paths_and_files.push_back(make_pair(extract_dirname(arg), extract_basename(arg, false)));
}
}
else
{
cerr << ERROR_STRING << "A path or a file must be given with -g" << endl ;
exit(1);
}
}
germline_system = "multi" ;
break;
case 'd':
several_D = true;
break;
case 'i':
multi_germline_incomplete = true;
break;
case 'I':
multi_germline_mark = true;
break;
case '1':
multi_germline_one_index_per_germline = false ;
break;
case '2':
multi_germline_unexpected_recombinations_12 = true ;
break;
case '4':
multi_germline_unexpected_recombinations_1U = true ;
break;
case 'G':
germline_system = string(optarg);
f_reps_V.push_back((germline_system + "V.fa").c_str()) ;
// Takes D only if it exists
{
struct stat buffer;
string putative_f_rep_D = germline_system + "D.fa" ;
if (stat(putative_f_rep_D.c_str(), &buffer) == 0)
{
f_reps_D.push_back(putative_f_rep_D.c_str()) ;
delta_min = DEFAULT_DELTA_MIN_D ;
}
}
f_reps_J.push_back((germline_system + "J.fa").c_str()) ;
germline_system = extract_basename(germline_system);
break;
// Algorithm
case 's':
#ifndef NO_SPACED_SEEDS
seed = string(optarg);
k = seed_weight(seed);
options_s_k++ ;
#else
cerr << "To enable the option -s, please compile without NO_SPACED_SEEDS" << endl;
#endif
break;
case 'k':
k = atoi(optarg);
seed = seed_contiguous(k);
options_s_k++ ;
break;
case 'w':
w = atoi(optarg);
break;
case 'm':
delta_min = atoi(optarg);
break;
case '!':
keep_unsegmented_as_clone = true;
break;
case 'e':
expected_value = atof_NO_LIMIT(optarg);
break;
case 'E':
expected_value_D = atof_NO_LIMIT(optarg);
break;
// Output
case 'o':
out_dir = optarg ;
break;
case 'b':
f_basename = optarg;
break;
case 'a':
output_sequences_by_cluster = true;
break;
case 't':
trim_sequences = atoi(optarg);
break;
case 'v':
verbose += 1 ;
break;
// Limits
case '%':
ratio_reads_clone = atof(optarg);
break;
case 'r':
min_reads_clone = atoi(optarg);
break;
case 'y':
max_representatives = atoi_NO_LIMIT(optarg);
break;
case 'z':
max_clones = atoi_NO_LIMIT(optarg);
if (max_representatives < max_clones)
max_representatives = max_clones ;
break;
case 'A': // --all
ratio_reads_clone = 0 ;
min_reads_clone = 1 ;
max_representatives = -1 ;
max_clones = -1 ;
break ;
case 'X':
max_reads_processed_sample = atoi_NO_LIMIT(optarg);
break;
case 'x':
max_reads_processed = atoi_NO_LIMIT(optarg);
break;
// Labels
case 'W':
windows_labels[string(optarg)] = string("-W");
break;
case 'l':
windows_labels_file = optarg;
break;
case 'F':
only_labeled_windows = true;
break;
// Clustering
case '=':
forced_edges = optarg;
break;
case 'n':
epsilon = atoi(optarg);
break;
case 'N':
minPts = atoi(optarg);
break;
case 'S':
save_comp=1;
break;
case 'L':
load_comp=1;
break;
case 'C':
cluster_cost=strToCost(optarg, Cluster);
break;
// Fine segmentation
case '3':
detect_CDR3 = true;
break;
case 'f':
segment_cost=strToCost(optarg, VDJ);
break;
case 'u':
output_unsegmented = output_unsegmented_detail_full ; // -uuu
output_unsegmented_detail_full = output_unsegmented_detail; // -uu
output_unsegmented_detail = true; // -u
break;
case 'U':
output_segmented = true;
break;
case 'K':
output_affects = true;
break;
}
//$$ options: post-processing+display
if (!germline_system.size() && (command != CMD_GERMLINES))
{
cerr << ERROR_STRING << "At least one germline must be given with -V/(-D)/-J, or -G, or -g." << endl ;
exit(1);
}
if (options_s_k > 1)
{
cerr << ERROR_STRING << "Use at most one -s or -k option." << endl ;
exit(1);
}
string out_seqdir = out_dir + "/seq/" ;
if (verbose)
cout << "# verbose " << verbose << endl ;
if (optind == argc)
{
cout << "# using default sequence file: " << f_reads << endl ;
}
else if (optind+1 == argc)
{
f_reads = argv[optind] ;
}
else
{
cerr << ERROR_STRING << "Wrong number of arguments." << endl ;
exit(1);
}
size_t min_cover_representative = (size_t) (min_reads_clone < (int) max_auditionned ? min_reads_clone : max_auditionned) ;
// Default seeds
#ifndef NO_SPACED_SEEDS
if (k == DEFAULT_K)
{
if (germline_system.find("TRA") != string::npos)
seed = SEED_S13 ;
else if ((germline_system.find("TRB") != string::npos)
|| (germline_system.find("IGH") != string::npos))
seed = SEED_S12 ;
else // TRD, TRG, IGK, IGL, custom, multi
seed = SEED_S10 ;
k = seed_weight(seed);
}
#else
{
cerr << ERROR_STRING << "Vidjil was compiled with NO_SPACED_SEEDS: please provide a -k option." << endl;
exit(1) ;
}
#endif
#ifndef NO_SPACED_SEEDS
// Check seed buffer
if (seed.size() >= MAX_SEED_SIZE)
{
cerr << ERROR_STRING << "Seed size is too large (MAX_SEED_SIZE)." << endl ;
exit(1);
}
#endif
if (w < seed_weight(seed))
{
cerr << ERROR_STRING << "Too small -w. The window size should be at least equal to the seed size (" << seed_weight(seed) << ")." << endl;
exit(1);
}
// Check that out_dir is an existing directory or creates it
const char *out_cstr = out_dir.c_str();
if (mkpath(out_cstr, 0755) == -1) {
cerr << ERROR_STRING << "Directory creation: " << out_dir << endl; perror("");
exit(2);
}
const char *outseq_cstr = out_seqdir.c_str();
if (mkpath(outseq_cstr, 0755) == -1) {
cerr << ERROR_STRING << "Directory creation: " << out_seqdir << endl; perror("");
exit(2);
}
// Compute basename if not given as an option
if (f_basename == "") {
f_basename = extract_basename(f_reads);
}
out_dir += "/" ;
/// Load labels ;
load_into_map(windows_labels, windows_labels_file, "-l");
switch(command) {
case CMD_WINDOWS: cout << "Extracting windows" << endl;
break;
case CMD_CLONES: cout << "Analysing clones" << endl;
break;
case CMD_SEGMENT: cout << "Segmenting V(D)J" << endl;
break;
case CMD_GERMLINES: cout << "Discovering germlines" << endl;
break;
}
cout << "Command line: ";
for (int i=0; i < argc; i++) {
cout << argv[i] << " ";
}
cout << endl;
//////////////////////////////////
// Display time and date
time_t rawtime;
struct tm *timeinfo;
char time_buffer[80];
time (&rawtime );
timeinfo = localtime (&rawtime);
strftime (time_buffer, 80,"%F %T", timeinfo);
cout << "# " << time_buffer << endl ;
//////////////////////////////////
// Warning for non-optimal use
if (max_clones == NO_LIMIT_VALUE || max_clones > WARN_MAX_CLONES)
{
cout << endl
<< "* WARNING: vidjil was run with '-A' option or with a large '-z' option" << endl ;
}
if (command == CMD_SEGMENT)
{
cout << endl
<< "* WARNING: vidjil was run with '-c segment' option" << endl ;
}
if (max_clones == NO_LIMIT_VALUE || max_clones > WARN_MAX_CLONES || command == CMD_SEGMENT)
{
cout << "* Vidjil's purpose is to efficiently extract windows overlapping the CDR3" << endl
<< "* to gather reads into clones ('-c clones')." << endl
<< "* Computing accurate V(D)J designations for many sequences ('-c segment' or large '-z' values)" << endl
<< "* is slow and should be done only on small datasets or for testing purposes." << endl
<< "* More information is provided in the 'doc/algo.org' file." << endl
<< endl ;
}
/////////////////////////////////////////
// JSON OUTPUT //
/////////////////////////////////////////
string f_json = out_dir + f_basename + JSON_SUFFIX ;
ostringstream stream_cmdline;
for (int i=0; i < argc; i++) stream_cmdline << argv[i] << " ";
json j = {
{"vidjil_json_version", VIDJIL_JSON_VERSION},
{"samples", {
{"number", 1},
{"original_names", {f_reads}},
{"run_timestamp", {time_buffer}},
{"producer", {soft_version}},
{"commandline", {stream_cmdline.str()}}
}}
};
/////////////////////////////////////////
// LOAD GERMLINES //
/////////////////////////////////////////
if (command == CMD_GERMLINES)
{
multi_germline = true ;
multi_germline_one_index_per_germline = false ;
if (multi_germline_paths_and_files.size() == 0)
multi_germline_paths_and_files.push_back(make_pair(DEFAULT_MULTI_GERMLINE_PATH, DEFAULT_MULTI_GERMLINE_FILE));
}
MultiGermline *multigermline = new MultiGermline(multi_germline_one_index_per_germline);
{
cout << "Load germlines and build Kmer indexes" << endl ;
if (multi_germline)
{
for (pair <string, string> path_file: multi_germline_paths_and_files)
multigermline->build_from_json(path_file.first, path_file.second, GERMLINES_REGULAR, trim_sequences);
}
else
{
// Custom germline
Germline *germline;
germline = new Germline(germline_system, 'X',
f_reps_V, f_reps_D, f_reps_J,
delta_min, trim_sequences);
germline->new_index(seed);
multigermline->insert(germline);
}
}
cout << endl ;
if (!multi_germline_one_index_per_germline) {
multigermline->build_with_one_index(seed, true);
}
if (multi_germline_unexpected_recombinations_12 || multi_germline_unexpected_recombinations_1U) {
if (!multigermline->index) {
multigermline->build_with_one_index(seed, false);
}
}
if (multi_germline_unexpected_recombinations_12) {
Germline *pseudo = new Germline("xxx", 'x', -10, trim_sequences);
pseudo->seg_method = SEG_METHOD_MAX12 ;
pseudo->index = multigermline->index ;
multigermline->germlines.push_back(pseudo);
}
if (multi_germline_unexpected_recombinations_1U) {
Germline *pseudo_u = new Germline("xxx", 'x', -10, trim_sequences);
pseudo_u->seg_method = SEG_METHOD_MAX1U ;
// TODO: there should be more up/downstream regions for the 'yyy' germline. And/or smaller seeds ?
pseudo_u->index = multigermline->index ;
multigermline->germlines.push_back(pseudo_u);
}
// Should come after the initialization of regular (and possibly pseudo) germlines
if (multi_germline_incomplete) {
multigermline->one_index_per_germline = true; // Starting from now, creates new indexes
for (pair <string, string> path_file: multi_germline_paths_and_files)
multigermline->build_from_json(path_file.first, path_file.second, GERMLINES_INCOMPLETE, trim_sequences);
}
if (multi_germline_mark)
multigermline->mark_cross_germlines_as_ambiguous();
cout << "Germlines loaded" << endl ;
cout << *multigermline ;
cout << endl ;
// Number of reads for e-value computation
unsigned long long nb_reads_for_evalue = (expected_value > NO_LIMIT_VALUE) ? nb_sequences_in_fasta(f_reads, true) : 1 ;
//////////////////////////////////
//$$ Read sequence files
int only_nth_read = 1 ;
if (max_reads_processed_sample != NO_LIMIT_VALUE)
{
only_nth_read = nb_sequences_in_fasta(f_reads) / max_reads_processed_sample;
if (only_nth_read == 0)
only_nth_read = 1 ;
max_reads_processed = max_reads_processed_sample ;
if (only_nth_read > 1)
cout << "Processing every " << only_nth_read
<< (only_nth_read == 2 ? "nd" : (only_nth_read == 3 ? "rd" : "th"))
<< " read" << endl ;
}
OnlineFasta *reads;
try {
reads = new OnlineFasta(f_reads, 1, read_header_separator, max_reads_processed, only_nth_read);
} catch (const invalid_argument e) {
cerr << ERROR_STRING << "Vidjil cannot open reads file " << f_reads << ": " << e.what() << endl;
exit(1);
}
out_dir += "/";
//////////////////////////////://////////
// DISCOVER GERMLINES //
/////////////////////////////////////////
if (command == CMD_GERMLINES)
{
map <char, int> stats_kmer, stats_max;
IKmerStore<KmerAffect> *index = multigermline->index ;
// Initialize statistics, with two additional categories
index->labels.push_back(make_pair(KmerAffect::getAmbiguous(), FASTA_AMBIGUOUS));
index->labels.push_back(make_pair(KmerAffect::getUnknown(), FASTA_UNKNOWN));
for (list< pair <KmerAffect, Fasta> >::const_iterator it = index->labels.begin(); it != index->labels.end(); ++it)
{
char key = affect_char(it->first.affect) ;
stats_kmer[key] = 0 ;
stats_max[key] = 0 ;
}
// init forbidden for .max()
set<KmerAffect> forbidden;
forbidden.insert(KmerAffect::getAmbiguous());
forbidden.insert(KmerAffect::getUnknown());
// Loop through all reads
int nb_reads = 0 ;
int total_length = 0 ;
int s = index->getS();
while (reads->hasNext())
{
reads->next();
nb_reads++;
string seq = reads->getSequence().sequence;
total_length += seq.length() - s + 1;
KmerAffectAnalyser *kaa = new KmerAffectAnalyser(*index, seq);