-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathAlevinUtils.cpp
1313 lines (1199 loc) · 50.4 KB
/
AlevinUtils.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 "AlevinUtils.hpp"
#include "peglib.h"
namespace alevin {
namespace utils {
std::unordered_map<uint32_t,uint32_t> lenOffset({
{8, 0},
{9, pow(4,8)},
{10, 5*pow(4,8)},
{11, 21*pow(4,8)},
{12, 85*pow(4,8)}});
std::unordered_map<std::string, std::string> iupacMap ({
//https://github.com/brwnj/umitools/blob/master/umitools/umitools.py#L26
//cell-barcodes should follow IUPAC mapping as following:
{"A","A"},
{"T","T"},
{"C","C"},
{"G","G"},
{"R","GA"},
{"Y","TC"},
{"M","AC"},
{"K","GT"},
{"S","GC"},
{"W","AT"},
{"H","ACT"},
{ "B","GTC"},
{"V","GCA"},
{"D","GAT"},
{"N","GATC"}
});
std::unordered_map<uint32_t, std::string> ntMap({ {0,"A"}, {1,"C"},
{2,"T"}, {3,"G"}});
std::unordered_map<std::string, std::string> revNtMap({ {"C","G"}, {"A","T"},
{"G","C"}, {"T","A"} });
std::vector<std::string> nts{"A", "T", "C", "G"};
template <>
std::string* getReadSequence(apt::CITESeq& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
subseq.clear();
subseq = seq2.substr(protocol.featureStart,
protocol.featureLength);
return &subseq;
}
template <>
std::string* getReadSequence(apt::DropSeq& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::Chromium& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::ChromiumV3& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::CELSeq& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::CELSeq2& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::QuartzSeq2& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::Custom& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::CustomGeometry& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
bool ok = protocol.read_geo.extract_read(seq, seq2, subseq);
return &subseq;
//subseq = seq2;
}
template <>
std::string* getReadSequence(apt::Gemcode& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
template <>
std::string* getReadSequence(apt::InDrop& protocol,
std::string& seq,
std::string& seq2,
std::string& subseq){
(void)seq;
return &seq2;
}
// end of read extraction
template <>
bool extractUMI<apt::DropSeq>(std::string& read,
std::string& read2,
apt::DropSeq& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.barcodeLength + pt.umiLength) ?
(umi.assign(read, pt.barcodeLength, pt.umiLength), true) : false;
}
template <>
bool extractUMI<apt::CITESeq>(std::string& read,
std::string& read2,
apt::CITESeq& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.barcodeLength + pt.umiLength) ?
(umi.assign(read, pt.barcodeLength, pt.umiLength), true) : false;
}
template <>
bool extractUMI<apt::Chromium>(std::string& read,
std::string& read2,
apt::Chromium& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.barcodeLength + pt.umiLength) ?
(umi.assign(read, pt.barcodeLength, pt.umiLength), true) : false;
}
template <>
bool extractUMI<apt::ChromiumV3>(std::string& read,
std::string& read2,
apt::ChromiumV3& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.barcodeLength + pt.umiLength) ?
(umi.assign(read, pt.barcodeLength, pt.umiLength), true) : false;
}
template <>
bool extractUMI<apt::Gemcode>(std::string& read,
std::string& read2,
apt::Gemcode& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.barcodeLength + pt.umiLength) ?
(umi.assign(read, pt.barcodeLength, pt.umiLength), true) : false;
}
template <>
bool extractUMI<apt::Custom>(std::string& read,
std::string& read2,
apt::Custom& pt,
std::string& umi){
(void)read2;
if ( pt.end == BarcodeEnd::FIVE ) {
umi.assign(read, pt.barcodeLength, pt.umiLength);
} else if (pt.end == BarcodeEnd::THREE ) {
umi.assign(read, 0, pt.umiLength);
} else {
return false;
}
return true;
}
template <>
bool extractUMI<apt::CustomGeometry>(std::string& read1,
std::string& read2,
apt::CustomGeometry& pt,
std::string& umi){
return pt.umi_geo.extract_tag(read1, read2, umi);
}
template <>
bool extractUMI<apt::QuartzSeq2>(std::string& read,
std::string& read2,
apt::QuartzSeq2& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.barcodeLength + pt.umiLength) ?
(umi.assign(read, pt.barcodeLength, pt.umiLength), true) : false;
return true;
}
template <>
bool extractUMI<apt::CELSeq2>(std::string& read,
std::string& read2,
apt::CELSeq2& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.umiLength) ?
(umi.assign(read, 0, pt.umiLength), true) : false;
}
template <>
bool extractUMI<apt::CELSeq>(std::string& read,
std::string& read2,
apt::CELSeq& pt,
std::string& umi){
(void)read2;
return (read.length() >= pt.umiLength) ?
(umi.assign(read, 0, pt.umiLength), true) : false;
return true;
}
template <>
bool extractUMI<apt::InDrop>(std::string& read,
std::string& read2,
apt::InDrop& pt,
std::string& umi){
(void)read;
(void)read2;
std::cout<<"Incorrect call for umi extract";
exit(1);
}
template <>
bool extractBarcode<apt::DropSeq>(std::string& read,
std::string& read2,
apt::DropSeq& pt,
std::string& bc){
(void)read2;
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read, 0, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::CITESeq>(std::string& read,
std::string& read2,
apt::CITESeq& pt,
std::string& bc){
(void)read2;
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read, 0, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::ChromiumV3>(std::string& read,
std::string& read2,
apt::ChromiumV3& pt,
std::string& bc){
(void)read2;
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read,0, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::Chromium>(std::string& read,
std::string& read2,
apt::Chromium& pt,
std::string& bc){
(void)read2;
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read, 0, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::Gemcode>(std::string& read,
std::string& read2,
apt::Gemcode& pt,
std::string& bc){
(void)read2;
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read, 0, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::Custom>(std::string& read,
std::string& read2,
apt::Custom& pt,
std::string& bc){
(void)read2;
if (pt.end == BarcodeEnd::FIVE) {
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read, 0, pt.barcodeLength), true) : false;
} else if (pt.end == BarcodeEnd::THREE) {
return (read.length() >= (pt.umiLength + pt.barcodeLength)) ?
(bc.assign(read, pt.umiLength, pt.barcodeLength), true) : false;
} else {
return false;
}
}
template <>
bool extractBarcode<apt::CustomGeometry>(std::string& read1,
std::string& read2,
apt::CustomGeometry& pt,
std::string& bc){
return pt.bc_geo.extract_tag(read1, read2, bc);
}
template <>
bool extractBarcode<apt::QuartzSeq2>(std::string& read,
std::string& read2,
apt::QuartzSeq2& pt,
std::string& bc){
(void)read2;
return (read.length() >= pt.barcodeLength) ?
(bc.assign(read, 0, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::CELSeq2>(std::string& read,
std::string& read2,
apt::CELSeq2& pt,
std::string& bc){
(void)read2;
return (read.length() >= (pt.umiLength + pt.barcodeLength)) ?
(bc.assign(read, pt.umiLength, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::CELSeq>(std::string& read,
std::string& read2,
apt::CELSeq& pt,
std::string& bc){
(void)read2;
return (read.length() >= (pt.umiLength + pt.barcodeLength)) ?
(bc.assign(read, pt.umiLength, pt.barcodeLength), true) : false;
}
template <>
bool extractBarcode<apt::InDrop>(std::string& read,
std::string& read2,
apt::InDrop& pt, std::string& bc){
(void)read2;
std::string::size_type index = read.find(pt.w1);
if (index == std::string::npos){
return false;
}
bc = read.substr(0, index);
if(bc.size()<8 or bc.size()>12){
return false;
}
uint32_t offset = bc.size()+pt.w1.size();
bc += read.substr(offset, offset+8);
return true;
}
void getIndelNeighbors(
const std::string& barcodeSeq,
std::unordered_set<std::string>& neighbors){
size_t barcodeLength = barcodeSeq.size();
std::string newBarcode;
for (size_t i=0; i<barcodeLength; i++){
for(auto j: nts){
if (i != barcodeLength-1){
//insert
newBarcode = barcodeSeq;
newBarcode.insert(i, j);
newBarcode.erase(barcodeLength, 1);
neighbors.insert(newBarcode);
//deletion
newBarcode = barcodeSeq;
newBarcode.erase(i, 1);
newBarcode.insert(barcodeLength-1, j);
neighbors.insert(newBarcode);
}//endif
}//end-j-for
}//end-i-for
}
//void getIndelNeighbors(
// std::string& barcodeSeq,
// std::unordered_set<std::string>& neighbors){
// size_t barcodeLength = barcodeSeq.size();
// std::string newBarcode;
// for (size_t i=0; i<=barcodeLength; i++){
// for(auto j: nts){
// if(barcodeLength<12){
// //insert
// newBarcode = barcodeSeq;
// newBarcode.insert(i, j);
// neighbors.insert(newBarcode);
// }
// }//end-j-for
// if(barcodeLength>8 and i<barcodeLength){
// //deletion
// newBarcode = barcodeSeq;
// newBarcode.erase(i, 1);
// neighbors.insert(newBarcode);
// }
// }//end-i-for
//}
/*
Finds all 1 edit-distance neighbor of a given barcode.
*/
void findNeighbors(size_t seqSize,
const std::string& barcodeSeq,
std::unordered_set<std::string>& neighbors){
size_t barcodeLength { barcodeSeq.size() };
std::string newBarcode, nt;
if(barcodeLength > seqSize){
std::cout<<"Sequence-Size " << barcodeLength << "greater than specified "
<< seqSize <<".\nPlease report the issue on Github.\n" ;
exit(64);
}
for (size_t i=0; i<barcodeLength; i++){
nt = barcodeSeq[i];
for(auto j: nts){
if (nt == j){
continue;
}
//snp
newBarcode = barcodeSeq;
newBarcode.replace(i, 1, j);
neighbors.insert(newBarcode);
}
}//end-i-for
getIndelNeighbors(barcodeSeq,
neighbors);
}
void getTxpToGeneMap(spp::sparse_hash_map<uint32_t, uint32_t>& txpToGeneMap,
spp::sparse_hash_map<std::string, uint32_t>& geneIdxMap,
const std::string& t2gFileName,
const std::string& refNamesFile,
const std::string& refLengthFile,
const std::string& headerFile,
std::shared_ptr<spdlog::logger>& jointLog,
bool noTgMap){
size_t kSize, numDupTxps{0};
uint64_t numberOfDecoys, firstDecoyIndex;
spp::sparse_hash_map<std::string, std::vector<uint32_t>> txpIdxMap;
// reading in the binary file
std::vector<std::string> txpNames;
{
std::ifstream ctstream(refNamesFile);
cereal::BinaryInputArchive contigTableArchive(ctstream);
contigTableArchive(txpNames);
ctstream.close();
}
std::vector<uint32_t> txpLengths;
{
std::ifstream ctstream(refLengthFile);
cereal::BinaryInputArchive contigTableArchive(ctstream);
contigTableArchive(txpLengths);
ctstream.close();
}
{
std::ifstream hstream(headerFile);
cereal::JSONInputArchive headerArchive(hstream);
headerArchive( cereal::make_nvp("num_decoys", numberOfDecoys) );
headerArchive( cereal::make_nvp("first_decoy_index", firstDecoyIndex) );
headerArchive( cereal::make_nvp("k", kSize) );
hstream.close();
}
size_t numShort {0};
{
// kk this is tricky.
// firstDecoyIndex is the index of the first decoy *after*
// removing short transcripts but the reported mappings are
// in full vector i.e. including small transcripts
size_t i {0};
if (numberOfDecoys > 0) {
while ( i-numShort < firstDecoyIndex ) {
if (txpLengths[i] <= kSize) { numShort += 1; }
txpIdxMap[txpNames[i]].emplace_back(i);
i += 1;
}
} else {
for (size_t i=0; i < txpNames.size(); i++) {
if (txpLengths[i] <= kSize) { numShort += 1; }
txpIdxMap[txpNames[i]].emplace_back(i);
} // end-for
} // end else
} // end block for populating txpIdxMap
for (auto it: txpIdxMap) {
size_t bucketLen = it.second.size();
if (bucketLen > 1) { numDupTxps += (bucketLen - 1); }
}
jointLog->info("Found {} transcripts(+{} decoys, +{} short and +{}"
" duplicate names in the index)",
txpNames.size() - numberOfDecoys - numShort - numDupTxps ,
numberOfDecoys, numShort, numDupTxps);
if (noTgMap){ // mapping the protein name to itself
for (size_t i=0; i<txpNames.size(); i++) {
geneIdxMap[txpNames[i]] = i;
txpToGeneMap[i] = i;
}
} else {
std::ifstream t2gFile(t2gFileName);
uint32_t gid, geneCount{0};
std::vector<uint32_t> tids;
std::string tStr, gStr;
if(t2gFile.is_open()) {
while( not t2gFile.eof() ) {
t2gFile >> tStr >> gStr;
if(not txpIdxMap.contains(tStr) ){
continue;
}
tids = txpIdxMap[tStr];
if (geneIdxMap.contains(gStr)){
gid = geneIdxMap[gStr];
}
else{
gid = geneCount;
geneIdxMap[gStr] = gid;
geneCount++;
}
for (auto tid: tids) {
if (txpToGeneMap.find(tid) != txpToGeneMap.end() &&
txpToGeneMap[tid] != gid ) {
jointLog->warn("Dual txp to gene map for txp {}", txpNames[tid]);
}
txpToGeneMap[tid] = gid;
}
}
t2gFile.close();
}
} // done parsing txp to gene map file
jointLog->info( "Filled with {} txp to gene entries ", txpToGeneMap.size());
for ( auto it: txpIdxMap ) {
for (auto tid: it.second) {
if (tid < firstDecoyIndex + numShort &&
txpToGeneMap.find( tid ) == txpToGeneMap.end() ) {
jointLog->error( "ERROR: Can't find gene mapping for : {} w/ index {}",
it.first, tid );
jointLog->error( "ERROR: "
"Txp to Gene Map not found for {}"
" transcripts. Exiting",
txpIdxMap.size() - txpToGeneMap.size() - numberOfDecoys
);
jointLog->flush();
exit(1);
}
}
}
jointLog->info("Found all transcripts to gene mappings");
}
struct Desc {
uint8_t read_num;
std::vector<std::pair<size_t,size_t>> locs;
friend std::ostream& operator<<(std::ostream& os, const Desc& de);
};
std::ostream& operator<<(std::ostream& os, const Desc& de) {
os << "Geometry description :: [\n";
os << "\tRead Num : " << static_cast<int32_t>(de.read_num) << "\n\t[";
for (size_t i = 0; i < de.locs.size(); ++i) {
os << " (" << de.locs[i].first << ", " << de.locs[i].second << ")";
}
os << "\t]\n";
os << "]\n";
return os;
}
bool parse_geometry_desc(std::string& geo_string, bool can_be_unbounded, peg::parser& parser, std::vector<Desc>& d, std::shared_ptr<spdlog::logger> log, alevin::protocols::TagGeometry& tg) {
// the first element in the string must be
// the read number (currently, only 1 or 2 is )
// acceptable.
// currently, only the read geometry can be unbounded
// (i.e. can stretch to std::string::npos). If
// can_be_unbounded is false, then reject any parsed
// geometry tag containing `end`.
d.clear();
if ( parser.parse(geo_string.c_str()) ) {
/*
if (d.size() > 1) {
log->error("Though supported in the syntax, the current implementation \n"
"of custom tag geometry does not support having a tag \n"
"split over more than one read.");
return false;
}
*/
//auto desc = d[0];
//tg.read_num = desc.read_num;
for (auto& desc : d) {
auto rn = desc.read_num;
auto& tg_length = (rn == 1) ? tg.length1 : tg.length2;
auto& tg_largest_index = (rn == 1) ? tg.largest_index1 : tg.largest_index2;
auto& tg_substr_locs = (rn ==1) ? tg.substr_locs1 : tg.substr_locs2;
// the length of the tag
size_t tlen{0};
// the largest index the tag reaches
size_t largest_index{0};
for (auto& start_stop : desc.locs) {
size_t start = start_stop.first;
if (start <= 0) { log->error("tag string subscript must be strictly positive (tag geometry is 1-indexed)."); return false; }
size_t stop = start_stop.second;
// make sure we don't contain `end` if we can't
if (!can_be_unbounded and stop == std::string::npos) {
log->error("only the geometry of the read can be unbounded (can contain 'end'), "
"but it was specified for a cell barcode / umi.");
return false;
}
// does this piece reach to the end of the read
// i.e. is it a range of the form `X-end`?
bool reach_to_end = (stop == std::string::npos);
if (stop < start) {
log->error("geometry contained a range {}-{}; cannot have stop < start.", start, stop);
return false;
}
// internally, we start indexing from 0
start -= 1;
if (!reach_to_end) { stop -= 1; }
largest_index = (stop > largest_index) ? stop : largest_index;
size_t len = (reach_to_end) ? std::string::npos : (stop - start) + 1;
// if the current piece is unbounded, or any piece so far
// has been unbounded, then the tag length is unbounded
tlen = (reach_to_end or (tlen == std::string::npos)) ? len : tlen + len;
if (tg_substr_locs.size() >= alevin::protocols::num_tag_pieces) {
log->error("Currently, alevin does not support the tag (barcode / umi) being "
"split into more than {} pieces. If the current bound is a "
"problem for your protocol, please reach out on GitHub.",
alevin::protocols::num_tag_pieces);
return false;
}
tg_substr_locs.push_back(std::make_pair(start, len));
}
tg_length = tlen;
tg_largest_index = largest_index;
} // for (auto& desc : d)
return true;
} else {
log->error("parser failure!");
return false;
}
return false;
}
template <typename ProtocolT>
bool processAlevinOpts(AlevinOpts<ProtocolT>& aopt,
SalmonOpts& sopt, bool noTgMap,
boost::program_options::variables_map& vm){
namespace bfs = boost::filesystem;
namespace po = boost::program_options;
// mark in salmon options that we are running
// in alevin mode
sopt.alevinMode = true;
sopt.hardFilter = true;
if (sopt.initUniform) { aopt.initUniform = true; }
//Create outputDirectory
aopt.outputDirectory = vm["output"].as<std::string>() + "/alevin";
if (!bfs::exists(aopt.outputDirectory)) {
bool dirSuccess = boost::filesystem::create_directories(aopt.outputDirectory);
if (!dirSuccess) {
fmt::print(stderr,"\nCould not create output directory {}\nExiting Now.",
aopt.outputDirectory.string());
return false;
}
}
if (vm.count("mrna")){
aopt.mRnaFile = vm["mrna"].as<std::string>();
if (!bfs::exists(aopt.mRnaFile)) {
fmt::print(stderr,"\n mRna File {} does not exists\n Exiting Now",
aopt.mRnaFile.string());
return false;
}
}
if (vm.count("rrna")){
aopt.rRnaFile = vm["rrna"].as<std::string>();
if (!bfs::exists(aopt.rRnaFile)) {
fmt::print(stderr,"\nrRna File {} does not exists\n Exiting Now",
aopt.rRnaFile.string());
return false;
}
}
if (vm.count("whitelist")){
aopt.whitelistFile = vm["whitelist"].as<std::string>();
if (!bfs::exists(aopt.whitelistFile)) {
fmt::print(stderr,"\nWhitelist File {} does not exists\n Exiting Now",
aopt.whitelistFile.string());
return false;
}
}
if (vm.count("vbemPrior")){
aopt.vbemPriorFile = vm["vbemPrior"].as<std::string>();
aopt.useVBEM = true;
aopt.vbemNorm = vm["vbemNorm"].as<double>();
if (aopt.vbemNorm == 0.0) {
fmt::print(stderr,"\nVBEM Normalization Factor not provided");
return false;
}
if (!bfs::exists(aopt.vbemPriorFile)) {
fmt::print(stderr,"\nVBEM Prior File {} does not exists\n Exiting Now",
aopt.vbemPriorFile.string());
return false;
}
}
if (vm.count("hash")){
aopt.bfhFile = vm["hash"].as<std::string>();
if (!bfs::exists(aopt.bfhFile)) {
fmt::print(stderr,"\nBfh File {} does not exists\n Exiting Now",
aopt.bfhFile.string());
return false;
}
}
if (not noTgMap) {
if (vm.count("tgMap")){
aopt.geneMapFile = vm["tgMap"].as<std::string>();
if (!bfs::exists(aopt.geneMapFile)) {
fmt::print(stderr,"\nTranscript to Gene Map File {} does not exists\n Exiting Now",
aopt.geneMapFile.string());
return false;
}
}
else{
fmt::print(stderr,"\nTranscript to Gene Map File not provided\n Exiting Now");
return false;
}
}
//create logger
spdlog::set_async_mode(131072);
auto logPath = aopt.outputDirectory / "alevin.log";
auto fileSink = std::make_shared<spdlog::sinks::simple_file_sink_mt>(logPath.string(), true);
auto consoleSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
consoleSink->set_color(spdlog::level::warn, consoleSink->magenta);
std::vector<spdlog::sink_ptr> sinks{consoleSink, fileSink};
aopt.jointLog = spdlog::create("alevinLog", std::begin(sinks), std::end(sinks));
aopt.just_align = vm["rad"].as<bool>();
aopt.sketch_mode = vm["sketch"].as<bool>();
if (aopt.sketch_mode and !aopt.just_align) {
aopt.jointLog->info("currently, --sketch implies --rad. Running in "
"alignment-only mode (will write a RAD output).");
aopt.just_align = true;
}
aopt.quiet = vm["quiet"].as<bool>();
aopt.noEM = vm["noem"].as<bool>();
aopt.noDedup = vm["noDedup"].as<bool>();
aopt.noWhitelist = vm["noWhitelist"].as<bool>();
aopt.naiveEqclass = vm["naiveEqclass"].as<bool>();
aopt.noQuant = vm["noQuant"].as<bool>();
aopt.dumpfq = vm["dumpfq"].as<bool>();
aopt.dumpArborescences = vm["dumpArborescences"].as<bool>();
aopt.dumpfeatures = vm["dumpFeatures"].as<bool>();
aopt.dumpMtx = vm["dumpMtx"].as<bool>();
aopt.dumpBarcodeEq = vm["dumpBarcodeEq"].as<bool>();
aopt.dumpBFH = vm["dumpBfh"].as<bool>();
aopt.dumpUmiGraph = vm["dumpUmiGraph"].as<bool>();
aopt.dumpCellEq = vm["dumpCellEq"].as<bool>();
aopt.trimRight = vm["trimRight"].as<uint32_t>();
aopt.numBootstraps = vm["numCellBootstraps"].as<uint32_t>();
aopt.numGibbsSamples = vm["numCellGibbsSamples"].as<uint32_t>();
aopt.lowRegionMinNumBarcodes = vm["lowRegionMinNumBarcodes"].as<uint32_t>();
aopt.maxNumBarcodes = vm["maxNumBarcodes"].as<uint32_t>();
aopt.freqThreshold = vm["freqThreshold"].as<uint32_t>();
aopt.umiEditDistance = vm["umiEditDistance"].as<uint32_t>();
aopt.forceCells = vm["forceCells"].as<uint32_t>();
aopt.expectCells = vm["expectCells"].as<uint32_t>();
if (aopt.just_align) {
aopt.jointLog->info("The --rad flag was passed to alevin. The "
"reads will be selectively aligned and the output written to a RAD file."
"Arguments passed that correspond to other processing steps will be ignored");
if (aopt.sketch_mode) {
aopt.jointLog->info("The --sketch flag was passed; the alignment will be run "
"in sketch mode.");
sopt.mismatchSeedSkip = 7;
}
} else {
if (aopt.sketch_mode) {
aopt.jointLog->info("The --sketch flag is not meaningful without the "
"--rad flag. This flag will be ignored.");
}
}
if (aopt.umiEditDistance > 4 ) {
aopt.jointLog->error("Too high edit distance collapsing {}, expected <= 4",
aopt.umiEditDistance);
return false;
}
if(vm.count("iupac")){
aopt.iupac = vm["iupac"].as<std::string>();
}
if (sopt.numBootstraps>0) {
aopt.jointLog->error("Do you mean numCellBootstraps ?");
return false;
}
if (aopt.numGibbsSamples > 0 and aopt.numBootstraps > 0) {
aopt.jointLog->error("Either of --numCellGibbsSamples or --numCellBootstraps "
"can be used");
return false;
}
if ( aopt.numBootstraps > 0 and aopt.noEM ) {
aopt.jointLog->error("cannot perform bootstrapping with noEM option.");
return false;
}
aopt.keepCBFraction = vm["keepCBFraction"].as<double>();
if ( aopt.keepCBFraction > 0.0 ) {
if ( vm.count("whitelist") ) {
aopt.jointLog->error("keepCBFraction and whitelist cannot be used together");
aopt.jointLog->flush();
exit(1);
}
aopt.jointLog->warn("Force Cells to {} fraction of All possible CB."
"This is not recommended way to run the pipeline,"
"and it might slow the pipeline",
aopt.keepCBFraction);
} else if (noTgMap) { aopt.keepCBFraction = 1.0; }
if (not vm.count("threads")) {
auto tot_cores = std::thread::hardware_concurrency();
aopt.numThreads = std::max(1, static_cast<int>(tot_cores/4.0));
aopt.jointLog->warn("threads flag not specified, Using {}(25%) of total cores",
aopt.numThreads);
}
else{
aopt.numThreads = vm["threads"].as<uint32_t>();
} // things which needs to be updated for salmonOpts
// handling of customized barcode and umi geometry
bool have_custom_umi_geo = vm.count("umi-geometry");
bool have_custom_bc_geo = vm.count("bc-geometry");
bool have_custom_read_geo = vm.count("read-geometry");
// need both
bool have_any_custom_geo = have_custom_read_geo or have_custom_umi_geo or have_custom_bc_geo;
bool have_all_custom_geo = have_custom_read_geo and have_custom_umi_geo and have_custom_bc_geo;
if ( have_any_custom_geo and !have_all_custom_geo ) {
aopt.jointLog->error("If you are using either of the umi-geometry or \n"
"the barcode-geometry options, then you have to provide both.\n"
"Alternatively, you can use pre-defined single-cell protocol flags.\n"
"Exiting Now.");
return false;
}
// validate customized options for custom protocol
bool haveCustomEnd = vm.count("end");
bool haveCustomBC= vm.count("barcodeLength");
bool haveCustomUMI = vm.count("umiLength");
bool allCustom = (haveCustomEnd and haveCustomBC and haveCustomUMI);
bool noCustom = !(haveCustomEnd or haveCustomBC or haveCustomUMI);
if (!noCustom and have_any_custom_geo) {
aopt.jointLog->warn("Note: the use of --end, --barcodeLength and --umiLength \n"
"to describe the barcode and umi geometry incompatible \n"
"with the new options `--barcode-geometry` and `--umi-geometry`.\n"
"The former are deprecated, please adopt the new options instead\n.");
return false;
}
// These are all or nothing. Either the user must provide all 3
// or none of these options.
if (!(noCustom or allCustom)) {
aopt.jointLog->error("If you are using any one"
" of (end, umilength, barcodelength) flag\n"
"you have to provide all of them explicitly.\n"
"You can also use pre-defined single-cell protocols."
"Exiting Now.");
return false;
}
if (allCustom) {
uint32_t barEnd = vm["end"].as<uint32_t>();
uint32_t barcodeLength = vm["barcodeLength"].as<uint32_t>();
uint32_t umiLength = vm["umiLength"].as<uint32_t>();
aopt.jointLog->warn("Note: the use of --end, --barcodeLength and --umiLength "
"to describe the barcode and umi geometry is deprecated. "
"Please start using the `--barcode-geometry` and `--umi-geometry` "
"options instead.");
// validate that BC and UMI lengths are OK
uint32_t maxBC{20};
uint32_t maxUMI{20};
if (barcodeLength < 1 or barcodeLength > maxBC) {
aopt.jointLog->error("Barcode length ({}) was not in the required length range [1, {}].\n"
"Exiting now.", barcodeLength, maxBC);
return false;
}
if (umiLength < 1 or umiLength > maxUMI) {
aopt.jointLog->error("UMI length ({}) was not in the required length range [1, {}].\n"
"Exiting now.", umiLength, maxUMI);
return false;
}
// validate that protocol end is OK and set it
if (barEnd == 3) {
aopt.protocol.end = BarcodeEnd::THREE;
}
else if (barEnd == 5) {
aopt.protocol.end = BarcodeEnd::FIVE;
} else{
aopt.jointLog->error("Wrong value for Barcode-end of read -> {}.\n"
"Please provide `5` for barcodes "
"starting at 5' end or `3` for barcode starting "
"at 3' end.\nExiting now.", barEnd);
return false;
}
// If all validation passed, then set the appropriate variables.
aopt.protocol.barcodeLength = barcodeLength;
aopt.protocol.umiLength = umiLength;
// Since we passed a custom UMI length and need to update the value here.
if (umiLength != alevin::types::AlevinUMIKmer::k()) {
alevin::types::AlevinUMIKmer::k(umiLength);
aopt.jointLog->info("A custom protocol (END, BC length, UMI length) = ({}, {}, {}) "
"is being used. Updating UMI k-mer length accordingly.",
barEnd, barcodeLength, umiLength);
}
} else if (have_all_custom_geo) {
std::string bc_geo_string = vm["bc-geometry"].as<std::string>();
std::string umi_geo_string = vm["umi-geometry"].as<std::string>();
std::string read_geo_string = vm["read-geometry"].as<std::string>();
// This describes the parsing expression grammar that
// we use for describing barcode geometry.
peg::parser parser(R"(
DescriptionList <- Description (','Description){0,1}
Description <- ReadNumber'['NumberRangeList']'
ReadNumber <- [1,2]
Number <- [0-9]+
End <- 'end'
NumberRange <- (Number '-' Number) / (Number '-' End)
NumberRangeList <- NumberRange (','NumberRange)*
)");
if (!(bool)parser) {
aopt.jointLog->error("Failed to instantiate the tag geometry parser.\n"
"This should not happen. Please report this issue on GitHub.");
return false;
}
// The variable to hold the temporary parsing information
std::vector<Desc> d;
parser["ReadNumber"] = [&](const peg::SemanticValues& sv) {
d.push_back({0,{}}); d.back().read_num = std::stoi(sv.token());
};
parser["Number"] = [&](const peg::SemanticValues& sv) {
return static_cast<size_t>(std::stoull(sv.token()));
};
parser["NumberRange"] = [&](const peg::SemanticValues& sv) {
switch (sv.choice()) {
case 0:
{