/
FieldExtractors.java
1137 lines (1022 loc) · 53.5 KB
/
FieldExtractors.java
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
/*
* Copyright (c) 2014-2019, Bolotin Dmitry, Chudakov Dmitry, Shugay Mikhail
* (here and after addressed as Inventors)
* All Rights Reserved
*
* Permission to use, copy, modify and distribute any part of this program for
* educational, research and non-profit purposes, by non-profit institutions
* only, without fee, and without a written agreement is hereby granted,
* provided that the above copyright notice, this paragraph and the following
* three paragraphs appear in all copies.
*
* Those desiring to incorporate this work into commercial products or use for
* commercial purposes should contact MiLaboratory LLC, which owns exclusive
* rights for distribution of this program for commercial purposes, using the
* following email address: licensing@milaboratory.com.
*
* IN NO EVENT SHALL THE INVENTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
* SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
* ARISING OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE INVENTORS HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE INVENTORS HAS
* NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
* MODIFICATIONS. THE INVENTORS MAKES NO REPRESENTATIONS AND EXTENDS NO
* WARRANTIES OF ANY KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
* PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY
* PATENT, TRADEMARK OR OTHER RIGHTS.
*/
package com.milaboratory.mixcr.export;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.milaboratory.core.Range;
import com.milaboratory.core.alignment.Alignment;
import com.milaboratory.core.io.sequence.SequenceRead;
import com.milaboratory.core.mutations.Mutations;
import com.milaboratory.core.mutations.MutationsUtil;
import com.milaboratory.core.sequence.AminoAcidSequence;
import com.milaboratory.core.sequence.NSequenceWithQuality;
import com.milaboratory.core.sequence.NucleotideSequence;
import com.milaboratory.core.sequence.TranslationParameters;
import com.milaboratory.mixcr.assembler.ReadToCloneMapping;
import com.milaboratory.mixcr.basictypes.Clone;
import com.milaboratory.mixcr.basictypes.VDJCAlignments;
import com.milaboratory.mixcr.basictypes.VDJCHit;
import com.milaboratory.mixcr.basictypes.VDJCObject;
import com.milaboratory.util.GlobalObjectMappers;
import gnu.trove.iterator.TObjectFloatIterator;
import gnu.trove.map.hash.TObjectFloatHashMap;
import io.repseq.core.GeneFeature;
import io.repseq.core.GeneType;
import io.repseq.core.ReferencePoint;
import io.repseq.core.SequencePartitioning;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class FieldExtractors {
static final String NULL = "";
private static final DecimalFormat SCORE_FORMAT = new DecimalFormat("#.#");
static Field[] descriptors = null;
public synchronized static Field[] getFields() {
if (descriptors == null) {
List<Field> descriptorsList = new ArrayList<>();
// Number of targets
descriptorsList.add(new PL_O("-targets", "Export number of targets", "Number of targets", "numberOfTargets") {
@Override
protected String extract(VDJCObject object) {
return Integer.toString(object.numberOfTargets());
}
});
// Best hits
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "Hit",
"Export best " + l + " hit", "Best " + l + " hit", "best" + l + "Hit") {
@Override
protected String extract(VDJCObject object) {
VDJCHit bestHit = object.getBestHit(type);
if (bestHit == null)
return NULL;
return bestHit.getGene().getName();
}
});
}
// Best gene
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "Gene",
"Export best " + l + " hit gene name (e.g. TRBV12-3 for TRBV12-3*00)", "Best " + l + " gene", "best" + l + "Gene") {
@Override
protected String extract(VDJCObject object) {
VDJCHit bestHit = object.getBestHit(type);
if (bestHit == null)
return NULL;
return bestHit.getGene().getGeneName();
}
});
}
// Best family
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "Family",
"Export best " + l + " hit family name (e.g. TRBV12 for TRBV12-3*00)", "Best " + l + " family", "best" + l + "Family") {
@Override
protected String extract(VDJCObject object) {
VDJCHit bestHit = object.getBestHit(type);
if (bestHit == null)
return NULL;
return bestHit.getGene().getFamilyName();
}
});
}
// Best hit score
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "HitScore",
"Export score for best " + l + " hit", "Best " + l + " hit score", "best" + l + "HitScore") {
@Override
protected String extract(VDJCObject object) {
VDJCHit bestHit = object.getBestHit(type);
if (bestHit == null)
return NULL;
return String.valueOf(bestHit.getScore());
}
});
}
// All hits
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "HitsWithScore",
"Export all " + l + " hits with score", "All " + l + " hits with score", "all" + l + "HitsWithScore") {
@Override
protected String extract(VDJCObject object) {
VDJCHit[] hits = object.getHits(type);
if (hits.length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
sb.append(hits[i].getGene().getName())
.append("(").append(SCORE_FORMAT.format(hits[i].getScore()))
.append(")");
if (i == hits.length - 1)
break;
sb.append(",");
}
return sb.toString();
}
});
}
// All hits without score
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "Hits",
"Export all " + l + " hits", "All " + l + " Hits", "all" + l + "Hits") {
@Override
protected String extract(VDJCObject object) {
VDJCHit[] hits = object.getHits(type);
if (hits.length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
sb.append(hits[i].getGene().getName());
if (i == hits.length - 1)
break;
sb.append(",");
}
return sb.toString();
}
});
}
// All gene names
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new StringExtractor("-" + Character.toLowerCase(l) + "Genes",
"Export all " + l + " gene names (e.g. TRBV12-3 for TRBV12-3*00)", "All " + l + " genes", "all" + l + "Genes", type) {
@Override
String extractStringForHit(VDJCHit hit) {
return hit.getGene().getGeneName();
}
});
}
// All families
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new StringExtractor("-" + Character.toLowerCase(l) + "Families",
"Export all " + l + " gene family anmes (e.g. TRBV12 for TRBV12-3*00)", "All " + l + " families", "all" + l + "Families", type) {
@Override
String extractStringForHit(VDJCHit hit) {
return hit.getGene().getFamilyName();
}
});
}
// Best alignment
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "Alignment",
"Export best " + l + " alignment", "Best " + l + " alignment", "best" + l + "Alignment") {
@Override
protected String extract(VDJCObject object) {
VDJCHit bestHit = object.getBestHit(type);
if (bestHit == null)
return NULL;
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
Alignment<NucleotideSequence> alignment = bestHit.getAlignment(i);
if (alignment == null)
sb.append(NULL);
else
sb.append(alignment.toCompactString());
if (i == object.numberOfTargets() - 1)
break;
sb.append(",");
}
return sb.toString();
}
});
}
// All alignments
for (final GeneType type : GeneType.values()) {
char l = type.getLetter();
descriptorsList.add(new PL_O("-" + Character.toLowerCase(l) + "Alignments",
"Export all " + l + " alignments", "All " + l + " alignments", "all" + l + "Alignments") {
@Override
protected String extract(VDJCObject object) {
VDJCHit[] hits = object.getHits(type);
if (hits.length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int j = 0; ; ++j) {
for (int i = 0; ; i++) {
Alignment<NucleotideSequence> alignment = hits[j].getAlignment(i);
if (alignment == null)
sb.append(NULL);
else
sb.append(alignment.toCompactString());
if (i == object.numberOfTargets() - 1)
break;
sb.append(',');
}
if (j == hits.length - 1)
break;
sb.append(';');
}
return sb.toString();
}
});
}
descriptorsList.add(new FeatureExtractors.NSeqExtractor("-nFeature", "Export nucleotide sequence of specified gene feature", "N. Seq. ", "nSeq") {
@Override
public String convert(NSequenceWithQuality seq) {
return seq.getSequence().toString();
}
});
descriptorsList.add(new FeatureExtractors.NSeqExtractor("-qFeature", "Export quality string of specified gene feature", "Qual. ", "qual") {
@Override
public String convert(NSequenceWithQuality seq) {
return seq.getQuality().toString();
}
});
descriptorsList.add(new FeatureExtractors.WithHeader("-aaFeature", "Export amino acid sequence of specified gene feature",
1, new String[]{"AA. Seq. "}, new String[]{"aaSeq"}) {
@Override
protected String extractValue(VDJCObject object, GeneFeature[] parameters) {
GeneFeature geneFeature = parameters[parameters.length - 1];
NSequenceWithQuality feature = object.getFeature(geneFeature);
if (feature == null)
return NULL;
int targetId = object.getTargetContainingFeature(geneFeature);
TranslationParameters tr = targetId == -1 ?
TranslationParameters.FromLeftWithIncompleteCodon
: object.getPartitionedTarget(targetId).getPartitioning().getTranslationParameters(geneFeature);
if (tr == null)
return NULL;
return AminoAcidSequence.translate(feature.getSequence(), tr).toString();
}
});
descriptorsList.add(new FeatureExtractors.WithHeader("-nFeatureImputed",
"Export nucleotide sequence of specified gene feature using letters from germline (marked lowercase) for uncovered regions",
1, new String[]{"N. Inc. Seq. "}, new String[]{"nSeqImputed"}) {
@Override
protected String extractValue(VDJCObject object, GeneFeature[] parameters) {
GeneFeature geneFeature = parameters[parameters.length - 1];
VDJCObject.CaseSensitiveNucleotideSequence feature = object.getIncompleteFeature(geneFeature);
if (feature == null)
return NULL;
return feature.toString();
}
});
descriptorsList.add(new FeatureExtractors.WithHeader("-aaFeatureImputed",
"Export amino acid sequence of specified gene feature using letters from germline (marked lowercase) for uncovered regions",
1, new String[]{"AA. Inc. Seq. "}, new String[]{"aaSeqImputed"}) {
@Override
protected String extractValue(VDJCObject object, GeneFeature[] parameters) {
GeneFeature geneFeature = parameters[parameters.length - 1];
VDJCObject.CaseSensitiveNucleotideSequence feature = object.getIncompleteFeature(geneFeature);
if (feature == null)
return NULL;
String aaStr = feature.toAminoAcidString();
if (aaStr == null)
return NULL;
return aaStr;
}
});
// descriptorsList.add(new FeatureExtractorDescriptor("-aaFeatureFromLeft", "Export amino acid sequence of " +
// "specified gene feature starting from the leftmost nucleotide (differs from -aaFeature only for " +
// "sequences which length are not multiple of 3)", "AA. Seq.", "aaSeq") {
// @Override
// public String convert(NSequenceWithQuality seq) {
// return AminoAcidSequence.translate(seq.getSequence(), FromLeftWithoutIncompleteCodon).toString();
// }
// });
//
// descriptorsList.add(new FeatureExtractorDescriptor("-aaFeatureFromRight", "Export amino acid sequence of " +
// "specified gene feature starting from the rightmost nucleotide (differs from -aaFeature only for " +
// "sequences which length are not multiple of 3)", "AA. Seq.", "aaSeq") {
// @Override
// public String convert(NSequenceWithQuality seq) {
// return AminoAcidSequence.translate(seq.getSequence(), FromRightWithoutIncompleteCodon).toString();
// }
// });
descriptorsList.add(new FeatureExtractors.NSeqExtractor("-minFeatureQuality", "Export minimal quality of specified gene feature", "Min. qual. ", "minQual") {
@Override
public String convert(NSequenceWithQuality seq) {
return "" + seq.getQuality().minValue();
}
});
descriptorsList.add(new FeatureExtractors.NSeqExtractor("-avrgFeatureQuality", "Export average quality of specified gene feature", "Mean. qual. ", "meanQual") {
@Override
public String convert(NSequenceWithQuality seq) {
return "" + seq.getQuality().meanValue();
}
});
descriptorsList.add(new FeatureExtractors.NSeqExtractor("-lengthOf", "Export length of specified gene feature.", "Length of ", "lengthOf") {
@Override
public String convert(NSequenceWithQuality seq) {
return "" + seq.size();
}
});
descriptorsList.add(new FeatureExtractors.MutationsExtractor("-nMutations",
"Extract nucleotide mutations for specific gene feature; relative to germline sequence.", 1,
new String[]{"N. Mutations in "}, new String[]{"nMutations"}) {
@Override
String convert(Mutations<NucleotideSequence> mutations, NucleotideSequence seq1,
NucleotideSequence seq2, TranslationParameters tr) {
return mutations.encode(",");
}
});
descriptorsList.add(new FeatureExtractors.MutationsExtractor("-nMutationsRelative",
"Extract nucleotide mutations for specific gene feature relative to another feature.", 2,
new String[]{"N. Mutations in ", " relative to "}, new String[]{"nMutationsIn", "Relative"}) {
@Override
String convert(Mutations<NucleotideSequence> mutations, NucleotideSequence seq1,
NucleotideSequence seq2, TranslationParameters tr) {
return mutations.encode(",");
}
});
final class AAMutations extends FeatureExtractors.MutationsExtractor {
AAMutations(String command, String description, int nArgs, String[] hPrefix, String[] sPrefix) {
super(command, description, nArgs, hPrefix, sPrefix);
}
@Override
String convert(Mutations<NucleotideSequence> mutations, NucleotideSequence seq1,
NucleotideSequence seq2, TranslationParameters tr) {
if (tr == null) return "-";
Mutations<AminoAcidSequence> aaMuts = MutationsUtil.nt2aa(seq1, mutations, tr);
if (aaMuts == null)
return "-";
return aaMuts.encode(",");
}
}
descriptorsList.add(new AAMutations("-aaMutations",
"Extract amino acid mutations for specific gene feature", 1,
new String[]{"AA. Mutations in "}, new String[]{"aaMutations"}));
descriptorsList.add(new AAMutations("-aaMutationsRelative",
"Extract amino acid mutations for specific gene feature relative to another feature.", 2,
new String[]{"AA. Mutations in ", " relative to "}, new String[]{"aaMutationsIn", "Relative"}));
final class MutationsDetailed extends FeatureExtractors.MutationsExtractor {
MutationsDetailed(String command, String description, int nArgs, String[] hPrefix, String[] sPrefix) {
super(command, description, nArgs, hPrefix, sPrefix);
}
@Override
String convert(Mutations<NucleotideSequence> mutations, NucleotideSequence seq1, NucleotideSequence seq2, TranslationParameters tr) {
if (tr == null) return "-";
MutationsUtil.MutationNt2AADescriptor[] descriptors = MutationsUtil.nt2aaDetailed(seq1, mutations, tr, 10);
if (descriptors == null)
return "-";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < descriptors.length; i++) {
sb.append(descriptors[i]);
if (i == descriptors.length - 1)
break;
sb.append(",");
}
return sb.toString();
}
}
String detailedMutationsFormat =
"Format <nt_mutation>:<aa_mutation_individual>:<aa_mutation_cumulative>, where <aa_mutation_individual> is an expected amino acid " +
"mutation given no other mutations have occurred, and <aa_mutation_cumulative> amino acid mutation is the observed amino acid " +
"mutation combining effect from all other. WARNING: format may change in following versions.";
descriptorsList.add(new MutationsDetailed("-mutationsDetailed",
"Detailed list of nucleotide and corresponding amino acid mutations. " + detailedMutationsFormat, 1,
new String[]{"Detailed mutations in "}, new String[]{"mutationsDetailedIn"}));
descriptorsList.add(new MutationsDetailed("-mutationsDetailedRelative",
"Detailed list of nucleotide and corresponding amino acid mutations written, positions relative to specified gene feature. " + detailedMutationsFormat, 2,
new String[]{"Detailed mutations in ", " relative to "}, new String[]{"mutationsDetailedIn", "Relative"}));
descriptorsList.add(new ExtractReferencePointPosition(true));
descriptorsList.add(new ExtractReferencePointPosition(false));
descriptorsList.add(new ExtractDefaultReferencePointsPositions());
descriptorsList.add(new PL_A("-readId", "Export id of read corresponding to alignment (deprecated)", "Read id", "readId") {
@Override
protected String extract(VDJCAlignments object) {
return "" + object.getMinReadId();
}
@Override
public FieldExtractor<VDJCAlignments> create(OutputMode outputMode, String[] args) {
System.out.println("WARNING: -readId is deprecated. Use -readIds");
return super.create(outputMode, args);
}
});
descriptorsList.add(new PL_A("-readIds", "Export id(s) of read(s) corresponding to alignment", "Read id", "readId") {
@Override
protected String extract(VDJCAlignments object) {
long[] readIds = object.getReadIds();
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
sb.append(readIds[i]);
if (i == readIds.length - 1)
return sb.toString();
sb.append(",");
}
}
});
descriptorsList.add(new PL_C("-cloneId", "Unique clone identifier", "Clone ID", "cloneId") {
@Override
protected String extract(Clone object) {
return "" + object.getId();
}
});
descriptorsList.add(new PL_C("-count", "Export clone count", "Clone count", "cloneCount") {
@Override
protected String extract(Clone object) {
return "" + object.getCount();
}
});
descriptorsList.add(new PL_C("-fraction", "Export clone fraction", "Clone fraction", "cloneFraction") {
@Override
protected String extract(Clone object) {
return "" + object.getFraction();
}
});
descriptorsList.add(new ExtractSequence(VDJCObject.class, "-targetSequences",
"Export aligned sequences (targets), separated with comma",
"Target sequences", "targetSequences"));
descriptorsList.add(new ExtractSequenceQuality(VDJCObject.class, "-targetQualities",
"Export aligned sequence (target) qualities, separated with comma",
"Target sequence qualities", "targetQualities"));
descriptorsList.add(new PL_A("-descrR1", "Export description line from initial .fasta or .fastq file (deprecated)", "Description R1", "descrR1") {
@Override
protected String extract(VDJCAlignments object) {
List<SequenceRead> reads = object.getOriginalReads();
if (reads == null)
throw new IllegalArgumentException("Error for option \'-descrR1\':\n" +
"No description available for read: either re-run align action with -OsaveOriginalReads=true option " +
"or don't use \'-descrR1\' in exportAlignments");
return reads.get(0).getRead(0).getDescription();
}
@Override
public FieldExtractor<VDJCAlignments> create(OutputMode outputMode, String[] args) {
System.out.println("WARNING: -descrR1 is deprecated. Use -descrsR1");
return super.create(outputMode, args);
}
});
descriptorsList.add(new PL_A("-descrR2", "Export description line from initial .fasta or .fastq file (deprecated)", "Description R2", "descrR2") {
@Override
protected String extract(VDJCAlignments object) {
List<SequenceRead> reads = object.getOriginalReads();
if (reads == null)
throw new IllegalArgumentException("Error for option \'-descrR1\':\n" +
"No description available for read: either re-run align action with -OsaveOriginalReads=true option " +
"or don't use \'-descrR1\' in exportAlignments");
SequenceRead read = reads.get(0);
if (read.numberOfReads() < 2)
throw new IllegalArgumentException("Error for option \'-descrR2\':\n" +
"No description available for second read: your input data was single-end");
return read.getRead(1).getDescription();
}
@Override
public FieldExtractor<VDJCAlignments> create(OutputMode outputMode, String[] args) {
System.out.println("WARNING: -descrR2 is deprecated. Use -descrsR2");
return super.create(outputMode, args);
}
});
descriptorsList.add(new PL_A("-descrsR1", "Export description lines from initial .fasta or .fastq file " +
"for R1 reads (only available if -OsaveOriginalReads=true was used in align command)", "Descriptions R1", "descrsR1") {
@Override
protected String extract(VDJCAlignments object) {
List<SequenceRead> reads = object.getOriginalReads();
if (reads == null)
throw new IllegalArgumentException("Error for option \'-descrR1\':\n" +
"No description available for read: either re-run align action with -OsaveOriginalReads option " +
"or don't use \'-descrR1\' in exportAlignments");
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
sb.append(reads.get(i).getRead(0).getDescription());
if (i == reads.size() - 1)
return sb.toString();
sb.append(",");
}
}
});
descriptorsList.add(new PL_A("-descrsR2", "Export description lines from initial .fastq file " +
"for R2 reads (only available if -OsaveOriginalReads=true was used in align command)", "Descriptions R2", "descrsR2") {
@Override
protected String extract(VDJCAlignments object) {
List<SequenceRead> reads = object.getOriginalReads();
if (reads == null)
throw new IllegalArgumentException("Error for option \'-descrR1\':\n" +
"No description available for read: either re-run align action with -OsaveOriginalReads option " +
"or don't use \'-descrR1\' in exportAlignments");
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
SequenceRead read = reads.get(i);
if (read.numberOfReads() < 2)
throw new IllegalArgumentException("Error for option \'-descrsR2\':\n" +
"No description available for second read: your input data was single-end");
sb.append(read.getRead(1).getDescription());
if (i == reads.size() - 1)
return sb.toString();
sb.append(",");
}
}
});
descriptorsList.add(new PL_A("-readHistory", "Export read history", "Read history", "readHistory") {
@Override
protected String extract(VDJCAlignments object) {
try {
return GlobalObjectMappers.toOneLine(object.getHistory());
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
}
}
});
descriptorsList.add(new PL_A("-cloneId", "To which clone alignment was attached (make sure using .clna file as input for exportAlignments)", "Clone ID", "cloneId") {
@Override
protected String extract(VDJCAlignments object) {
return "" + object.getCloneIndex();
}
});
descriptorsList.add(new PL_A("-cloneIdWithMappingType", "To which clone alignment was attached with additional info on mapping type (make sure using .clna file as input for exportAlignments)", "Clone mapping", "cloneMapping") {
@Override
protected String extract(VDJCAlignments object) {
int ci = object.getCloneIndex();
ReadToCloneMapping.MappingType mt = object.getMappingType();
return "" + ci + ":" + mt;
}
});
// descriptorsList.add(alignmentsToClone("-cloneId", "To which clone alignment was attached.", false));
// descriptorsList.add(alignmentsToClone("-cloneIdWithMappingType", "To which clone alignment was attached with additional info on mapping type.", true));
// descriptorsList.add(new AbstractField<Clone>(Clone.class, "-readIds", "Read IDs aggregated by clone.") {
// @Override
// public FieldExtractor<Clone> create(OutputMode outputMode, String[] args) {
// return new CloneToReadsExtractor(outputMode, args[0]);
// }
//
// @Override
// public String metaVars() {
// return "<index_file>";
// }
// });
for (final GeneType type : GeneType.values()) {
String c = Character.toLowerCase(type.getLetter()) + "IdentityPercents";
descriptorsList.add(new PL_O("-" + c, type.getLetter() + " alignment identity percents",
type.getLetter() + " alignment identity percents", c) {
@Override
protected String extract(VDJCObject object) {
VDJCHit[] hits = object.getHits(type);
if (hits == null || hits.length == 0)
return NULL;
StringBuilder sb = new StringBuilder();
sb.append("");
for (int i = 0; ; i++) {
sb.append(hits[i].getIdentity());
if (i == hits.length - 1)
return sb.toString();
sb.append(",");
}
}
});
}
for (final GeneType type : GeneType.values()) {
String c = Character.toLowerCase(type.getLetter()) + "BestIdentityPercent";
descriptorsList.add(new PL_O("-" + c, type.getLetter() + " best alignment identity percent",
type.getLetter() + "best alignment identity percent", c) {
@Override
protected String extract(VDJCObject object) {
VDJCHit hit = object.getBestHit(type);
if (hit == null)
return NULL;
return Float.toString(hit.getIdentity());
}
});
}
descriptorsList.add(new PL_O("-chains", "Chains", "Chains", "chains") {
@Override
protected String extract(VDJCObject object) {
return object.commonChains().toString();
}
});
descriptorsList.add(new PL_O("-topChains", "Top chains", "Top chains", "topChains") {
@Override
protected String extract(VDJCObject object) {
return object.commonTopChains().toString();
}
});
descriptors = descriptorsList.toArray(new Field[descriptorsList.size()]);
}
return descriptors;
}
public static boolean hasField(String name) {
for (Field field : getFields())
if (name.equalsIgnoreCase(field.getCommand()))
return true;
return false;
}
public static FieldExtractor parse(OutputMode outputMode, Class clazz, String[] args) {
for (Field field : getFields())
if (field.canExtractFrom(clazz) && args[0].equalsIgnoreCase(field.getCommand()))
return field.create(outputMode, Arrays.copyOfRange(args, 1, args.length));
throw new IllegalArgumentException("Not a valid options: " + Arrays.toString(args));
}
public static ArrayList<String>[] getDescription(Class clazz) {
ArrayList<String>[] description = new ArrayList[]{new ArrayList(), new ArrayList()};
for (Field field : getFields())
if (field.canExtractFrom(clazz)) {
description[0].add(field.getCommand() + " " + field.metaVars());
description[1].add(field.getDescription());
}
return description;
}
private static ArrayList<String>[] getDescriptionsForSpecificClassOnly(boolean clones) {
ArrayList<String>[] description = new ArrayList[]{new ArrayList(), new ArrayList()};
for (Field field : getFields()) {
boolean c;
if (clones)
c = field.canExtractFrom(Clone.class) && !field.canExtractFrom(VDJCAlignments.class);
else
c = field.canExtractFrom(VDJCAlignments.class) && !field.canExtractFrom(Clone.class);
if (c) {
description[0].add(field.getCommand() + " " + field.metaVars());
description[1].add(field.getDescription());
}
}
return description;
}
static ArrayList<String>[] getDescriptionSpecificForClones() {
return getDescriptionsForSpecificClassOnly(true);
}
static ArrayList<String>[] getDescriptionSpecificForAlignments() {
return getDescriptionsForSpecificClassOnly(false);
}
static ArrayList<String>[] getDescriptionSpecificForClass(Class clazz) {
if (clazz.equals(VDJCObject.class))
return getDescription(clazz);
return getDescriptionsForSpecificClassOnly(clazz.equals(Clone.class));
}
/* Some typedefs */
static abstract class PL_O extends FieldParameterless<VDJCObject> {
PL_O(String command, String description, String hHeader, String sHeader) {
super(VDJCObject.class, command, description, hHeader, sHeader);
}
}
static abstract class PL_A extends FieldParameterless<VDJCAlignments> {
PL_A(String command, String description, String hHeader, String sHeader) {
super(VDJCAlignments.class, command, description, hHeader, sHeader);
}
}
static abstract class PL_C extends FieldParameterless<Clone> {
PL_C(String command, String description, String hHeader, String sHeader) {
super(Clone.class, command, description, hHeader, sHeader);
}
}
static abstract class WP_O<P> extends FieldWithParameters<VDJCObject, P> {
protected WP_O(String command, String description, int nArguments) {
super(VDJCObject.class, command, description, nArguments);
}
}
/***************************************************/
private static class ExtractSequence extends FieldParameterless<VDJCObject> {
private ExtractSequence(Class targetType, String command, String description, String hHeader, String sHeader) {
super(targetType, command, description, hHeader, sHeader);
}
@Override
protected String extract(VDJCObject object) {
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
sb.append(object.getTarget(i).getSequence());
if (i == object.numberOfTargets() - 1)
break;
sb.append(",");
}
return sb.toString();
}
}
private static class ExtractSequenceQuality extends FieldParameterless<VDJCObject> {
private ExtractSequenceQuality(Class targetType, String command, String description, String hHeader, String sHeader) {
super(targetType, command, description, hHeader, sHeader);
}
@Override
protected String extract(VDJCObject object) {
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
sb.append(object.getTarget(i).getQuality());
if (i == object.numberOfTargets() - 1)
break;
sb.append(",");
}
return sb.toString();
}
}
private static class ExtractReferencePointPosition extends WP_O<ReferencePoint> {
private final boolean inReference;
protected ExtractReferencePointPosition(boolean inReference) {
super(inReference ? "-positionInReferenceOf" : "-positionOf",
"Export position of specified reference point inside " + (inReference ? "reference" : "target") + "sequences " +
"(clonal sequence / read sequence).", 1);
this.inReference = inReference;
}
@Override
protected ReferencePoint getParameters(String[] string) {
if (string.length != 1)
throw new RuntimeException("Wrong number of parameters for " + getCommand());
return ReferencePoint.parse(string[0]);
}
@Override
protected String getHeader(OutputMode outputMode, ReferencePoint parameters) {
return choose(outputMode, "Position " + (inReference ? "in reference" : "") + " of ",
inReference ? "-positionInReferenceOf" : "-positionOf") +
ReferencePoint.encode(parameters, true);
}
@Override
protected String extractValue(VDJCObject object, ReferencePoint refPoint) {
StringBuilder sb = new StringBuilder();
for (int i = 0; ; i++) {
int positionInTarget = object.getPartitionedTarget(i).getPartitioning().getPosition(refPoint);
if (inReference) {
VDJCHit hit = object.getBestHit(refPoint.getGeneType());
if (hit != null) {
Alignment<NucleotideSequence> al = hit.getAlignment(i);
if (al != null)
positionInTarget = al.convertToSeq1Position(positionInTarget);
}
}
sb.append(positionInTarget);
if (i == object.numberOfTargets() - 1)
break;
sb.append(",");
}
return sb.toString();
}
@Override
public String metaVars() {
return "<reference_point>";
}
}
static class ExtractDefaultReferencePointsPositions extends PL_O {
public ExtractDefaultReferencePointsPositions() {
super("-defaultAnchorPoints", "Outputs a list of default reference points (like CDR2Begin, FR4End, etc. " +
"see documentation for the full list and formatting)", "Ref. points", "refPoints");
}
@Override
protected String extract(VDJCObject object) {
StringBuilder sb = new StringBuilder();
VDJCHit bestVHit = object.getBestHit(GeneType.Variable);
VDJCHit bestDHit = object.getBestHit(GeneType.Diversity);
VDJCHit bestJHit = object.getBestHit(GeneType.Joining);
for (int i = 0; ; i++) {
SequencePartitioning partitioning = object.getPartitionedTarget(i).getPartitioning();
for (int j = 0; ; j++) {
ReferencePoint refPoint = ReferencePoint.DefaultReferencePoints[j];
// Processing special cases for number of deleted / P-segment nucleotides
if (refPoint.equals(ReferencePoint.VEnd))
sb.append(trimmedPosition(bestVHit, i, ReferencePoint.VEndTrimmed, ReferencePoint.VEnd));
else if (refPoint.equals(ReferencePoint.DBegin))
sb.append(trimmedPosition(bestDHit, i, ReferencePoint.DBeginTrimmed, ReferencePoint.DBegin));
else if (refPoint.equals(ReferencePoint.DEnd))
sb.append(trimmedPosition(bestDHit, i, ReferencePoint.DEndTrimmed, ReferencePoint.DEnd));
else if (refPoint.equals(ReferencePoint.JBegin))
sb.append(trimmedPosition(bestJHit, i, ReferencePoint.JBeginTrimmed, ReferencePoint.JBegin));
else {
// Normal points
int referencePointPosition = partitioning.getPosition(refPoint);
if (referencePointPosition >= 0)
sb.append(referencePointPosition);
}
if (j == ReferencePoint.DefaultReferencePoints.length - 1)
break;
sb.append(":");
}
if (i == object.numberOfTargets() - 1)
break;
sb.append(",");
}
return sb.toString();
}
public static String trimmedPosition(VDJCHit hit, int targetId, ReferencePoint trimmedPoint, ReferencePoint boundaryPoint) {
if (!trimmedPoint.isAttachedToAlignmentBound())
throw new IllegalArgumentException();
// No hit - no point
if (hit == null)
return "";
Alignment<NucleotideSequence> alignment = hit.getAlignment(targetId);
// If alignment is not defined for this target
if (alignment == null)
return "";
ReferencePoint ap = trimmedPoint.getActivationPoint();
Range seq1Range = alignment.getSequence1Range();
if (ap != null) {
// Point is valid only if activation point is reached
int apPositionInSeq1 = hit.getGene().getPartitioning().getRelativePosition(hit.getAlignedFeature(), ap);
if (apPositionInSeq1 < 0 ||
trimmedPoint.isAttachedToLeftAlignmentBound() ?
seq1Range.getFrom() > apPositionInSeq1 :
seq1Range.getTo() <= apPositionInSeq1)
return "";
}
int bpPositionInSeq1 = hit.getGene().getPartitioning().getRelativePosition(hit.getAlignedFeature(), boundaryPoint);
// Just in case
if (bpPositionInSeq1 < 0)
return "";
return Integer.toString(
trimmedPoint.isAttachedToLeftAlignmentBound() ?
bpPositionInSeq1 - seq1Range.getFrom() :
seq1Range.getTo() - bpPositionInSeq1);
}
}
// private static AbstractField<VDJCAlignments> alignmentsToClone(
// final String command, final String description, final boolean printMapping) {
// return new AbstractField<VDJCAlignments>(VDJCAlignments.class, command, description) {
// @Override
// public FieldExtractor<VDJCAlignments> create(OutputMode outputMode, String[] args) {
// return new AlignmentToCloneExtractor(outputMode, args[0], printMapping);
// }
//
// @Override
// public String metaVars() {
// return "<index_file>";
// }
// };
// }
// private static final class AlignmentToCloneExtractor
// implements FieldExtractor<VDJCAlignments>, Closeable {
// private final OutputMode outputMode;
// private final AlignmentsToClonesMappingContainer container;
// private final OutputPort<ReadToCloneMapping> byAls;
// private final boolean printMapping;
// private final Iterator<ReadToCloneMapping> mappingIterator;
// private ReadToCloneMapping currentMapping = null;
//
// public AlignmentToCloneExtractor(OutputMode outputMode, String indexFile, boolean printMapping) {
// try {
// this.outputMode = outputMode;
// this.printMapping = printMapping;
// this.container = AlignmentsToClonesMappingContainer.open(indexFile);
// this.byAls = this.container.createPortByAlignments();
// this.mappingIterator = CUtils.it(byAls).iterator();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
//
// @Override
// public String getHeader() {
// if (printMapping)
// return choose(outputMode, "Clone mapping", "cloneMapping");
// else
// return choose(outputMode, "Clone Id", "cloneId");
// }
//
// @Override
// public String extractValue(VDJCAlignments object) {
// if (currentMapping == null && mappingIterator.hasNext())
// currentMapping = mappingIterator.next();
//
// if (currentMapping == null)
// throw new IllegalArgumentException("Wrong number of records in index.");
//
// while (currentMapping.getAlignmentsId() < object.getAlignmentsIndex() && mappingIterator.hasNext())
// currentMapping = mappingIterator.next();
// if (currentMapping.getAlignmentsId() != object.getAlignmentsIndex())
// return printMapping ? Dropped.toString().toLowerCase() : NULL;
//
// int cloneIndex = currentMapping.getCloneIndex();
// ReadToCloneMapping.MappingType mt = currentMapping.getMappingType();
// if (currentMapping.isDropped())
// return printMapping ? mt.toString().toLowerCase() : NULL;
// return printMapping ? Integer.toString(cloneIndex) + ":" + mt.toString().toLowerCase() : Integer.toString(cloneIndex);