Body: <p>I use the next versions:</p>
  <pre><code>gatk   4.4.0.0
  minimap2   2.26-r1175
  samtools   1.19
  </code></pre>
  <p>I have a fastq file. And try to implement gatk MergeBamAlignment to generate a new bam-file:</p>
  <pre><code>java -jar gatk.jar FastqToSam --FASTQ .fastq --OUTPUT u.bam
  --SAMPLE_NAME sample1 -RG readgroup -PL ILLUMINA
  minimap2 -a hg38.fa .fastq -o A.sam
  java -jar gatk.jar SortSam -I A.sam -O A.sort.bam -SO queryname
  java -jar gatk.jar SortSam -I u.bam -O u.sort.bam -SO queryname
  java -jar gatk.jar MergeBamAlignment -O m.bam -R hg38.fa -UNMAPPED_BAM u.bam -ALIGNED_BAM A.sort.bam
  </code></pre>
  <p>But I receive error on the output of MergeBamAlignment:</p>
  <pre><code>INFO    2023-12-21 19:20:41     SamAlignmentMerger      Processing SAM file(s): [./myprogs/data/NR.Aligned.sort.bam] 
  
  [Thu Dec 21 19:20:42 2023] picard.sam.MergeBamAlignment done. 
  
  Elapsed time: 0.02 minutes.
  
  Runtime.totalMemory()=486539264 
  
  To get help, see http://broadinstitute.github.io/picard/index.html#GettingHelp
  
  java.lang.ArrayIndexOutOfBoundsException: Index 86 out of bounds for length 86
  at htsjdk.samtools.util.SequenceUtil.calculateMdAndNmTags(SequenceUtil.java:990)
  at picard.sam.AbstractAlignmentMerger.fixNmMdAndUq(AbstractAlignmentMerger.java:611)
  at picard.sam.AbstractAlignmentMerger.mergeAlignment(AbstractAlignmentMerger.java:586)
  at picard.sam.SamAlignmentMerger.mergeAlignment(SamAlignmentMerger.java:181)
  at picard.sam.MergeBamAlignment.doWork(MergeBamAlignment.java:368)
  at picard.cmdline.CommandLineProgram.instanceMain(CommandLineProgram.java:289)
  at org.broadinstitute.hellbender.cmdline.PicardCommandLineProgramExecutor.instanceMain(PicardCommandLineProgramExecutor.java:37)
  at org.broadinstitute.hellbender.Main.runCommandLineProgram(Main.java:160)
  at org.broadinstitute.hellbender.Main.mainEntry(Main.java:203)
  at org.broadinstitute.hellbender.Main.main(Main.java:289)
  </code></pre>
  <p>I have diagnosed u.sort.bam and A.sam.bam with ValidateSamFile:</p>
  <pre><code>java -jar gatk.jar ValidateSamFile -I u.sort.bam -M SUMMARY -R hg38.fa
  No errors found
  [Thu Dec 21 00:32:34 2023] picard.sam.ValidateSamFile done. Elapsed time: 0.00 minutes. 
  Runtime.totalMemory()=234881024 
  
  Tool returned: 
  0 
  
  java -jar gatk.jar ValidateSamFile -I A.sort.bam -M SUMMARY -R hg38.fa
  ERROR    2023-12-21 01:17:28    ValidateSamFile    Requesting earlier reference sequence: 23 &lt; 49 
  [Thu Dec 21 01:17:28 2023] picard.sam.ValidateSamFile done. Elapsed time: 0.18 minutes.
  Runtime.totalMemory()=2113929216
  To get help, see http://broadinstitute.github.io/picard/index.html#GettingHelp  
  
  Tool returned:
  -1
  </code></pre>
  <p>I have done the same actions only with <code>--MD</code> in minimap2. But the same errors occur. I would like to find out how to fix this problem. I use the latest versions of the programs. I have downloaded hg38.fa each one time, but it didn't change the situation. I acts in according with gatk's article 'Data pre-processing for variant discovery' : <a href="https://gatk.broadinstitute.org/hc/en-us/articles/360035535912-Data-pre-processing-for-variant-discovery" rel="nofollow noreferrer">https://gatk.broadinstitute.org/hc/en-us/articles/360035535912-Data-pre-processing-for-variant-discovery</a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-30T15:31:12.867Z
Comments.Text: I don't think that you need to resort as minimap should output the reads in the same order that they were ingested
Comments.UserDisplayName: Yossi Farjoun
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-30T15:36:15.577Z
Comments.Text: However, the fact that the output of gatk SortSam isn't valid is disconcerting. Can you see if it's a small part of your fasta file that is the problem? perhaps chop it in half and see if you can reproduce it on a smaller fastq?
Comments.UserDisplayName: Yossi Farjoun
Tags: <phylogenetics><gatk><minimap2><indexing><java>
Title: java.lang.ArrayIndexOutOfBoundsException: Index 86 out of bounds for length 86
ThreadText: # java.lang.ArrayIndexOutOfBoundsException: Index 86 out of bounds for length 86
  
  (Asked by: username_0 on 2023-12-21)
  
  I use the next versions:
  
  ```
  gatk   4.4.0.0
  minimap2   2.26-r1175
  samtools   1.19
  
  ```
  
  I have a fastq file. And try to implement gatk MergeBamAlignment to generate a new bam-file:
  
  ```
  java -jar gatk.jar FastqToSam --FASTQ .fastq --OUTPUT u.bam
  --SAMPLE_NAME sample1 -RG readgroup -PL ILLUMINA
  minimap2 -a hg38.fa .fastq -o A.sam
  java -jar gatk.jar SortSam -I A.sam -O A.sort.bam -SO queryname
  java -jar gatk.jar SortSam -I u.bam -O u.sort.bam -SO queryname
  java -jar gatk.jar MergeBamAlignment -O m.bam -R hg38.fa -UNMAPPED_BAM u.bam -ALIGNED_BAM A.sort.bam
  
  ```
  
  But I receive error on the output of MergeBamAlignment:
  
  ```
  INFO    2023-12-21 19:20:41     SamAlignmentMerger      Processing SAM file(s): [./myprogs/data/NR.Aligned.sort.bam] 
  
  [Thu Dec 21 19:20:42 2023] picard.sam.MergeBamAlignment done. 
  
  Elapsed time: 0.02 minutes.
  
  Runtime.totalMemory()=486539264 
  
  To get help, see http://broadinstitute.github.io/picard/index.html#GettingHelp
  
  java.lang.ArrayIndexOutOfBoundsException: Index 86 out of bounds for length 86
  at htsjdk.samtools.util.SequenceUtil.calculateMdAndNmTags(SequenceUtil.java:990)
  at picard.sam.AbstractAlignmentMerger.fixNmMdAndUq(AbstractAlignmentMerger.java:611)
  at picard.sam.AbstractAlignmentMerger.mergeAlignment(AbstractAlignmentMerger.java:586)
  at picard.sam.SamAlignmentMerger.mergeAlignment(SamAlignmentMerger.java:181)
  at picard.sam.MergeBamAlignment.doWork(MergeBamAlignment.java:368)
  at picard.cmdline.CommandLineProgram.instanceMain(CommandLineProgram.java:289)
  at org.broadinstitute.hellbender.cmdline.PicardCommandLineProgramExecutor.instanceMain(PicardCommandLineProgramExecutor.java:37)
  at org.broadinstitute.hellbender.Main.runCommandLineProgram(Main.java:160)
  at org.broadinstitute.hellbender.Main.mainEntry(Main.java:203)
  at org.broadinstitute.hellbender.Main.main(Main.java:289)
  
  ```
  
  I have diagnosed u.sort.bam and A.sam.bam with ValidateSamFile:
  
  ```
  java -jar gatk.jar ValidateSamFile -I u.sort.bam -M SUMMARY -R hg38.fa
  No errors found
  [Thu Dec 21 00:32:34 2023] picard.sam.ValidateSamFile done. Elapsed time: 0.00 minutes. 
  Runtime.totalMemory()=234881024 
  
  Tool returned: 
  0 
  
  java -jar gatk.jar ValidateSamFile -I A.sort.bam -M SUMMARY -R hg38.fa
  ERROR    2023-12-21 01:17:28    ValidateSamFile    Requesting earlier reference sequence: 23 < 49 
  [Thu Dec 21 01:17:28 2023] picard.sam.ValidateSamFile done. Elapsed time: 0.18 minutes.
  Runtime.totalMemory()=2113929216
  To get help, see http://broadinstitute.github.io/picard/index.html#GettingHelp  
  
  Tool returned:
  -1
  
  ```
  
  I have done the same actions only with `--MD` in minimap2. But the same errors occur. I would like to find out how to fix this problem. I use the latest versions of the programs. I have downloaded hg38.fa each one time, but it didn't change the situation. I acts in according with gatk's article 'Data pre-processing for variant discovery' : <https://gatk.broadinstitute.org/hc/en-us/articles/360035535912-Data-pre-processing-for-variant-discovery>
  
  **Tags:** phylogenetics, gatk, minimap2, indexing, java
  
  > **Comments:**
  > **username_1**: I don't think that you need to resort as minimap should output the reads in the same order that they were ingested  
  > **username_1**: However, the fact that the output of gatk SortSam isn't valid is disconcerting. Can you see if it's a small part of your fasta file that is the problem? perhaps chop it in half and see if you can reproduce it on a smaller fastq?  
  
  ---
  
  ## 0 answers
  
  