@@ -297,33 +297,34 @@ private void createQual(Element parent, boolean isLongReadBam) {
297297
298298 private void createTLen (Element parent ) {
299299 //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+ }
305305 }
306306
307307 private void createRLENGTH (Element parent ) {
308308 //Read length
309309 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 ));
313313 }
314314 }
315315
316316 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+ }
327328 }
328329
329330 private void createRNAME (Element parent ) {
@@ -371,12 +372,13 @@ private void createMAPQ(Element parent) {
371372
372373 private void createPOS (Element parent ) {
373374 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 ) {
376378 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 ));
380382 }
381383 }
382384
@@ -463,7 +465,7 @@ public void parseRecord(final SAMRecord record) {
463465 }
464466 }
465467
466- private void summaryToXml (Element parent ) {
468+ public void summaryToXml (Element parent ) {
467469 Element summaryElement = XmlElementUtils .createSubElement (parent , XmlUtils .BAM_SUMMARY );
468470
469471 long discardReads = 0 ;
@@ -473,29 +475,30 @@ private void summaryToXml(Element parent) {
473475 long noncanonicalBase = 0 ;
474476 long trimBases = 0 ,overlappedBase = 0 , softClippedBase = 0 , hardClippedBase = 0 ;
475477 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 ) {
478481 try {
479-
482+ ReadGroupSummary summary = rgSummaries . get ( key );
480483 Element rgEle = XmlUtils .createReadGroupNode (rgsElement , summary .getReadGroupId ());
481484 summary .readSummary2Xml (rgEle );
482- summary .pairSummary2Xml (rgEle );
485+ summary .pairSummary2Xml (rgEle );
483486 // presummary
484487 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 ();
487490 duplicateBase += summary .getDuplicateBase ();
488491 unmappedBase += summary .getUnmappedBase ();
489492 noncanonicalBase += summary .getNotProperPairedBase ();
490493 trimBases += summary .getTrimmedBase ();
491494 overlappedBase += summary .getOverlappedBase ();
492495 softClippedBase += summary .getSoftClippedBase ();
493496 hardClippedBase += summary .getHardClippedBase ();
494-
497+
495498 discardReads += summary .getDiscardreads ();
496499 readCount += summary .getReadCount ();
497500 } catch (Exception e ) {
498- logger .warn (e .getMessage ());
501+ logger .warn (e .getMessage ());
499502 }
500503 }
501504
@@ -578,6 +581,12 @@ public void setSamSequenceDictionary(SAMSequenceDictionary samSeqDictionary) {
578581 public void setReadGroups (List <String > ids ) {
579582 readGroupIds = Stream .concat (ids .stream (), readGroupIds .stream ()).collect (Collectors .toList ());
580583 }
584+
585+ public void setReadGroupSummaries (List <String > ids ) {
586+ for (String readGroupId : ids ) {
587+ rgSummaries .put (readGroupId , new ReadGroupSummary (readGroupId , isLongReadBam ));
588+ }
589+ }
581590
582591 ConcurrentMap <String , PositionSummary > getRNamePosition () {
583592 return rNamePosition ;
0 commit comments