Answers.Body: <p>Your first command only needs a slight modification to add in <code>-h</code>. This will create a SAM file with a header.</p>
  
  <pre><code>samtools view -h -F 256 input.bam &gt; headered.input.primaryOnly.sam
  </code></pre>
  
  <blockquote>
    <p><strong>-h</strong>        Include the header in the output.</p>
  </blockquote>
  
  <p>If your primary end goal is to create a BAM, then as Pierre has pointed out you can create BAM files directly from <code>samtools view</code> with the <code>-b</code> option:</p>
  
  <pre><code>samtools view -b -F 256 input.bam &gt; input.primaryOnly.bam
  </code></pre>
  
  <p>And if your end-goal is a <em>sorted</em> BAM file, then pipe the BAM output through samtools sort:</p>
  
  <pre><code>samtools view -b -F 256 input.bam | samtools sort &gt; sorted.input.primaryOnly.bam
  </code></pre>
  
  <hr>
  
  <p>So that's fixing what <em>I</em> think your problem is, but what about the specific question that you have asked, i.e. how can a header be added to an existing headerless SAM file?</p>
  
  <p>If you have an existing BAM file, you can use Picard (as you have mentioned), or manually piece the files together with <code>cat</code>, which will have a similar function (although Picard probably includes a few checks to make sure that the header is consistent with the input file):</p>
  
  <pre><code>(samtools view -H input.bam; cat input.primaryOnly.sam) &gt; headered.input.primaryOnly.sam
  </code></pre>
  
  <p>If the only thing that matters is that the BAM file has correct reference information (which is fairly common), then the <code>-T</code> option can be used with <code>samtools view</code>. Again, <code>-h</code> or <code>-b</code> is required to make sure both the header and the usual SAM fields are displayed:</p>
  
  <pre><code>samtools view -T reference.fa -h input.primaryOnly.sam &gt; headered.input.primaryOnly.sam
  samtools view -T reference.fa -b input.primaryOnly.sam &gt; headered.input.primaryOnly.bam
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-07T21:41:46.003Z
Answers.LastActivityDate: 2018-02-07T21:41:46.003Z
Answers.UserDisplayName: gringer
Body: <p>I subset a BAM to only include primary reads using the following samtools commands:</p>
  
  <pre><code>samtools view -F 256 input.bam &gt; input.primaryOnly.sam
  </code></pre>
  
  <p>Now, in order to convert this SAM into a new subsetted BAM, I would need to add a header. The way I normally do this is with Picard:</p>
  
  <pre><code>java -jar picard.jar ReplaceSamHeader \
        I=input.primaryOnly.sam \
        HEADER=input.header.sam \
        O=new.sam
  </code></pre>
  
  <p>whereby the header <code>input.header.sam</code> is from the original BAM: </p>
  
  <pre><code>samtools view -H input.bam &gt; input.header.sam
  </code></pre>
  
  <p>Then I could convert the SAM into a BAM with </p>
  
  <pre><code>samtools view -S -b i new.sam &gt; new.bam
  </code></pre>
  
  <p>My problem is, I'm using the original BAM header. If I look back at the BAM, it now contains wrong information, e.g.</p>
  
  <pre><code>samtools flagstat new.bam
  </code></pre>
  
  <p>might save that secondary alignments exist, when they don't. </p>
  
  <p>How do I get a BAM header that makes sense given my new BAM (with only primary alignments)? </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-07T19:30:33.013Z
Comments.Text: "Now, in order to convert this SAM into a new subsetted BAM, I would need to add a header." I don't understand why you do ignore the sam header at the beginning ?  why don't you just use 'samtools view -F 256 -b -o out.bam input.bam' ?
Comments.UserDisplayName: Pierre
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-07T21:30:03.457Z
Comments.Text: `samtools flagstat` looks at the statistics for the *reads*, not the header. The thing that would possibly change in the header would be sequence lengths (i.e. from `samtools idxstats`), but as long as the reference sequence is the same for your original and new BAM, that won't be a problem.
Comments.UserDisplayName: gringer
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-08T19:22:57.340Z
Comments.Text: @Pierre Yes, I wasn't thinking about this clearly. Thanks
Comments.UserDisplayName: EB2127
Tags: <sam><samtools>
Title: Modifying a SAM header after removing all non-primary reads
ThreadText: # Modifying a SAM header after removing all non-primary reads
  
  (Asked by: username_0 on 2018-02-07)
  
  I subset a BAM to only include primary reads using the following samtools commands:
  
  ```
  samtools view -F 256 input.bam > input.primaryOnly.sam
  
  ```
  
  Now, in order to convert this SAM into a new subsetted BAM, I would need to add a header. The way I normally do this is with Picard:
  
  ```
  java -jar picard.jar ReplaceSamHeader \
        I=input.primaryOnly.sam \
        HEADER=input.header.sam \
        O=new.sam
  
  ```
  
  whereby the header `input.header.sam` is from the original BAM:
  
  ```
  samtools view -H input.bam > input.header.sam
  
  ```
  
  Then I could convert the SAM into a BAM with
  
  ```
  samtools view -S -b i new.sam > new.bam
  
  ```
  
  My problem is, I'm using the original BAM header. If I look back at the BAM, it now contains wrong information, e.g.
  
  ```
  samtools flagstat new.bam
  
  ```
  
  might save that secondary alignments exist, when they don't.
  
  How do I get a BAM header that makes sense given my new BAM (with only primary alignments)?
  
  **Tags:** sam, samtools
  
  > **Comments:**
  > **username_1**: "Now, in order to convert this SAM into a new subsetted BAM, I would need to add a header." I don't understand why you do ignore the sam header at the beginning ?  why don't you just use 'samtools view -F 256 -b -o out.bam input.bam' ?  
  > **username_2**: `samtools flagstat` looks at the statistics for the *reads*, not the header. The thing that would possibly change in the header would be sequence lengths (i.e. from `samtools idxstats`), but as long as the reference sequence is the same for your original and new BAM, that won't be a problem.  
  > **username_0**: @username_1 Yes, I wasn't thinking about this clearly. Thanks  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-02-07. Score: 5 | Accepted answer
  
  Your first command only needs a slight modification to add in `-h`. This will create a SAM file with a header.
  
  ```
  samtools view -h -F 256 input.bam > headered.input.primaryOnly.sam
  
  ```
  
  > **-h** Include the header in the output.
  
  If your primary end goal is to create a BAM, then as username_1 has pointed out you can create BAM files directly from `samtools view` with the `-b` option:
  
  ```
  samtools view -b -F 256 input.bam > input.primaryOnly.bam
  
  ```
  
  And if your end-goal is a *sorted* BAM file, then pipe the BAM output through samtools sort:
  
  ```
  samtools view -b -F 256 input.bam | samtools sort > sorted.input.primaryOnly.bam
  
  ```
  
  ---
  
  So that's fixing what *I* think your problem is, but what about the specific question that you have asked, i.e. how can a header be added to an existing headerless SAM file?
  
  If you have an existing BAM file, you can use Picard (as you have mentioned), or manually piece the files together with `cat`, which will have a similar function (although Picard probably includes a few checks to make sure that the header is consistent with the input file):
  
  ```
  (samtools view -H input.bam; cat input.primaryOnly.sam) > headered.input.primaryOnly.sam
  
  ```
  
  If the only thing that matters is that the BAM file has correct reference information (which is fairly common), then the `-T` option can be used with `samtools view`. Again, `-h` or `-b` is required to make sure both the header and the usual SAM fields are displayed:
  
  ```
  samtools view -T reference.fa -h input.primaryOnly.sam > headered.input.primaryOnly.sam
  samtools view -T reference.fa -b input.primaryOnly.sam > headered.input.primaryOnly.bam
  
  ```
  
  
  ---
  
  