@@ -297,33 +297,34 @@ private void createQual(Element parent, boolean isLongReadBam) {
297
297
298
298
private void createTLen (Element parent ) {
299
299
//ISIZE
300
- parent = XmlElementUtils .createSubElement (parent , XmlUtils .READGROUPS );
301
- for ( Entry <String , ReadGroupSummary > entry : rgSummaries .entrySet ()) {
302
- // output tLen inside pairSummary, eg. inward, f3f5
303
- entry . getValue ( ).pairTlen2Xml (XmlUtils .createReadGroupNode (parent , entry . getKey ()));
304
- }
300
+ parent = XmlElementUtils .createSubElement (parent , XmlUtils .READGROUPS );
301
+ List <String > sortedKeys = rgSummaries .keySet (). stream (). sorted (). toList ();
302
+ for ( String key : sortedKeys ) {
303
+ rgSummaries . get ( key ).pairTlen2Xml (XmlUtils .createReadGroupNode (parent , key ));
304
+ }
305
305
}
306
306
307
307
private void createRLENGTH (Element parent ) {
308
308
//Read length
309
309
parent = XmlElementUtils .createSubElement (parent , XmlUtils .READGROUPS );
310
- for ( Entry <String , ReadGroupSummary > entry : rgSummaries .entrySet ()) {
311
- // output ReadLength
312
- entry . getValue ( ).readLength2Xml (XmlUtils .createReadGroupNode (parent , entry . getKey () ));
310
+ List <String > sortedKeys = rgSummaries .keySet (). stream (). sorted (). toList ();
311
+ for ( String key : sortedKeys ) {
312
+ rgSummaries . get ( key ).readLength2Xml (XmlUtils .createReadGroupNode (parent , key ));
313
313
}
314
314
}
315
315
316
316
private void createCigar (Element parent ) {
317
- parent = XmlElementUtils .createSubElement (parent , XmlUtils .READGROUPS );
318
- for (Entry <String , ReadGroupSummary > entry : rgSummaries .entrySet ()) {
319
- Element ele = XmlUtils .createMetricsNode (XmlUtils .createReadGroupNode (parent , entry .getKey ()), null ,
320
- new Pair <String , Number >(ReadGroupSummary .READ_COUNT ,entry .getValue ().getCigarReadCount ()));
321
-
322
- // cigar string from reads including duplicateReads, notProperPairs and unmappedReads but excluding discardedReads (failed, secondary and supplementary).
323
- Map <String , AtomicLong > tallys = new TreeMap <>(new CigarStringComparator ());
324
- tallys .putAll ( entry .getValue ().getCigarCount ());
325
- XmlUtils .outputTallyGroup (ele ,XmlUtils .CIGAR , tallys , true , false );
326
- }
317
+ parent = XmlElementUtils .createSubElement (parent , XmlUtils .READGROUPS );
318
+ List <String > sortedKeys = rgSummaries .keySet ().stream ().sorted ().toList ();
319
+ for (String key : sortedKeys ) {
320
+ Element ele = XmlUtils .createMetricsNode (XmlUtils .createReadGroupNode (parent , key ), null ,
321
+ new Pair <String , Number >(ReadGroupSummary .READ_COUNT ,rgSummaries .get (key ).getCigarReadCount ()));
322
+
323
+ // cigar string from reads including duplicateReads, notProperPairs and unmappedReads but excluding discardedReads (failed, secondary and supplementary).
324
+ Map <String , AtomicLong > tallys = new TreeMap <>(new CigarStringComparator ());
325
+ tallys .putAll ( rgSummaries .get (key ).getCigarCount ());
326
+ XmlUtils .outputTallyGroup (ele ,XmlUtils .CIGAR , tallys , true , false );
327
+ }
327
328
}
328
329
329
330
private void createRNAME (Element parent ) {
@@ -371,12 +372,13 @@ private void createMAPQ(Element parent) {
371
372
372
373
private void createPOS (Element parent ) {
373
374
parent = XmlElementUtils .createSubElement (parent , XmlUtils .READGROUPS );
374
-
375
- for (String rg : rgSummaries .keySet ()) {
375
+
376
+ List <String > sortedKeys = rgSummaries .keySet ().stream ().sorted ().toList ();
377
+ for (String rg : sortedKeys ) {
376
378
long readCount = rNamePosition .values ().stream ().mapToLong (x -> x .getTotalCountByRg (rg )).sum ();
377
- Element ele = XmlUtils .createMetricsNode (XmlUtils .createReadGroupNode (parent , rg ) , null , new Pair <String , Number >(ReadGroupSummary .READ_COUNT , readCount ));
378
- rNamePosition .keySet ().stream ().sorted (new ReferenceNameComparator ()).forEach (ref ->
379
- XmlUtils .outputBins (ele , ref , rNamePosition .get (ref ).getCoverageByRg (rg ), PositionSummary .BUCKET_SIZE ));
379
+ Element ele = XmlUtils .createMetricsNode (XmlUtils .createReadGroupNode (parent , rg ) , null , new Pair <String , Number >(ReadGroupSummary .READ_COUNT , readCount ));
380
+ rNamePosition .keySet ().stream ().sorted (new ReferenceNameComparator ()).forEach (ref ->
381
+ XmlUtils .outputBins (ele , ref , rNamePosition .get (ref ).getCoverageByRg (rg ), PositionSummary .BUCKET_SIZE ));
380
382
}
381
383
}
382
384
@@ -463,7 +465,7 @@ public void parseRecord(final SAMRecord record) {
463
465
}
464
466
}
465
467
466
- private void summaryToXml (Element parent ) {
468
+ public void summaryToXml (Element parent ) {
467
469
Element summaryElement = XmlElementUtils .createSubElement (parent , XmlUtils .BAM_SUMMARY );
468
470
469
471
long discardReads = 0 ;
@@ -473,29 +475,30 @@ private void summaryToXml(Element parent) {
473
475
long noncanonicalBase = 0 ;
474
476
long trimBases = 0 ,overlappedBase = 0 , softClippedBase = 0 , hardClippedBase = 0 ;
475
477
long readCount = 0 , lostBase = 0 ; // baseCount = 0,
476
- Element rgsElement = XmlElementUtils .createSubElement (summaryElement , XmlUtils .READGROUPS );
477
- for (ReadGroupSummary summary : rgSummaries .values ()) {
478
+ Element rgsElement = XmlElementUtils .createSubElement (summaryElement , XmlUtils .READGROUPS );
479
+ List <String > sortedKeys = rgSummaries .keySet ().stream ().sorted ().toList ();
480
+ for (String key : sortedKeys ) {
478
481
try {
479
-
482
+ ReadGroupSummary summary = rgSummaries . get ( key );
480
483
Element rgEle = XmlUtils .createReadGroupNode (rgsElement , summary .getReadGroupId ());
481
484
summary .readSummary2Xml (rgEle );
482
- summary .pairSummary2Xml (rgEle );
485
+ summary .pairSummary2Xml (rgEle );
483
486
// presummary
484
487
lostBase += summary .getDuplicateBase () + summary .getUnmappedBase () + summary .getNotProperPairedBase ()
485
- + summary .getTrimmedBase () + summary .getOverlappedBase () + summary .getSoftClippedBase () + summary .getHardClippedBase ();
486
- maxBases += summary .getReadCount () * summary .getMaxReadLength ();
488
+ + summary .getTrimmedBase () + summary .getOverlappedBase () + summary .getSoftClippedBase () + summary .getHardClippedBase ();
489
+ maxBases += summary .getReadCount () * summary .getMaxReadLength ();
487
490
duplicateBase += summary .getDuplicateBase ();
488
491
unmappedBase += summary .getUnmappedBase ();
489
492
noncanonicalBase += summary .getNotProperPairedBase ();
490
493
trimBases += summary .getTrimmedBase ();
491
494
overlappedBase += summary .getOverlappedBase ();
492
495
softClippedBase += summary .getSoftClippedBase ();
493
496
hardClippedBase += summary .getHardClippedBase ();
494
-
497
+
495
498
discardReads += summary .getDiscardreads ();
496
499
readCount += summary .getReadCount ();
497
500
} catch (Exception e ) {
498
- logger .warn (e .getMessage ());
501
+ logger .warn (e .getMessage ());
499
502
}
500
503
}
501
504
@@ -578,6 +581,12 @@ public void setSamSequenceDictionary(SAMSequenceDictionary samSeqDictionary) {
578
581
public void setReadGroups (List <String > ids ) {
579
582
readGroupIds = Stream .concat (ids .stream (), readGroupIds .stream ()).collect (Collectors .toList ());
580
583
}
584
+
585
+ public void setReadGroupSummaries (List <String > ids ) {
586
+ for (String readGroupId : ids ) {
587
+ rgSummaries .put (readGroupId , new ReadGroupSummary (readGroupId , isLongReadBam ));
588
+ }
589
+ }
581
590
582
591
ConcurrentMap <String , PositionSummary > getRNamePosition () {
583
592
return rNamePosition ;
0 commit comments