Body: <p>I have a bam file that I want to run through GATK's SplitNCigarReads tool. Because of the way the bam file was generated, the program will often fail, with an error message stating:</p>
  <blockquote>
  <p><code>##### ERROR MESSAGE: Bad input: Cannot split this read (might be an empty section between Ns, for example 1N1D1N): ABCDEFG</code></p>
  </blockquote>
  <p>My question is: how do I write a bash script to:</p>
  <ol>
  <li><p>Start GATK on the input bam file.</p>
  </li>
  <li><p>When (and if) the error message arises, take the bad read (e.g., ABCDEFG) and use samtools to remove it,</p>
  <pre><code>samtools view -h input.bam | grep -v ABCDEFG | samtools view -bS -o intermediate1.bam
  </code></pre>
  </li>
  <li><p>Index the new bam file</p>
  </li>
  <li><p>Run the same GATK command as #1, but with the newly generated bam file</p>
  </li>
  <li><p>Keep performing steps 1-4 until the GATK tool runs to completion</p>
  </li>
  </ol>
  <p>I realize this may be quite complicated and/or involved, so I don't expect someone to write the entire script for me. Are there any online resources dedicated to writing a script like this?</p>
  <p>P.S. This is my first StackExchange question, please let me know if I did anything incorrectly or inefficiently.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-09-16T13:46:18.550Z
Comments.Text: Removing offending reads one by one and rerunning the tool every time will be **very** time-consuming, and strikes me as fundamentally the wrong approach: due to how BAM files are structured, removing individual reads is incredibly inefficient. Your time would be better spent devising a way to detect and remove the offending reads all at once.
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-09-18T05:13:07.227Z
Comments.Text: may refer to page 217:  https://books.google.com.hk/books?id=JUp3EAAAQBAJ&pg=PA214&lpg=PA214&dq=bash+script+%22splitncigarreads%22&source=bl&ots=qf2xcKQL7Q&sig=ACfU3U1EQhIsOiGEnBndigXHEjnBkFX4aQ&hl=en&sa=X&ved=2ahUKEwi5t6XJyp36AhWQZd4KHaDUBDEQ6AF6BAgfEAM#v=onepage&q=bash%20script%20%22splitncigarreads%22&f=false
Comments.UserDisplayName: envs_h_gang_5
Tags: <samtools><bash><gatk>
Title: Write a bash script to run gatk, fix errors with input, and rerun until completion
ThreadText: # Write a bash script to run gatk, fix errors with input, and rerun until completion
  
  (Asked by: username_0 on 2022-09-14)
  
  I have a bam file that I want to run through GATK's SplitNCigarReads tool. Because of the way the bam file was generated, the program will often fail, with an error message stating:
  
  > `##### ERROR MESSAGE: Bad input: Cannot split this read (might be an empty section between Ns, for example 1N1D1N): ABCDEFG`
  
  My question is: how do I write a bash script to:
  
  1. Start GATK on the input bam file.
  2. When (and if) the error message arises, take the bad read (e.g., ABCDEFG) and use samtools to remove it,
  
     ```
     samtools view -h input.bam | grep -v ABCDEFG | samtools view -bS -o intermediate1.bam
  
     ```
  3. Index the new bam file
  4. Run the same GATK command as #1, but with the newly generated bam file
  5. Keep performing steps 1-4 until the GATK tool runs to completion
  
  I realize this may be quite complicated and/or involved, so I don't expect someone to write the entire script for me. Are there any online resources dedicated to writing a script like this?
  
  P.S. This is my first StackExchange question, please let me know if I did anything incorrectly or inefficiently.
  
  **Tags:** samtools, bash, gatk
  
  > **Comments:**
  > **username_1**: Removing offending reads one by one and rerunning the tool every time will be **very** time-consuming, and strikes me as fundamentally the wrong approach: due to how BAM files are structured, removing individual reads is incredibly inefficient. Your time would be better spent devising a way to detect and remove the offending reads all at once.  
  > **username_2**: may refer to page 217:  https://books.google.com.hk/books?id=JUp3EAAAQBAJ&pg=PA214&lpg=PA214&dq=bash+script+%22splitncigarreads%22&source=bl&ots=qf2xcKQL7Q&sig=ACfU3U1EQhIsOiGEnBndigXHEjnBkFX4aQ&hl=en&sa=X&ved=2ahUKEwi5t6XJyp36AhWQZd4KHaDUBDEQ6AF6BAgfEAM#v=onepage&q=bash%20script%20%22splitncigarreads%22&f=false  
  
  ---
  
  ## 0 answers
  
  