Answers.Body: <p>Quick look at your script suggests that you are comparing the XS and AS from different reads. I would recommend removing the continue from the <code>read.has_tag('XS')</code> continue block.</p>
  
  <p>I think that if you filter out <code>is_secondary</code> and <code>is_supplementary</code> you don't need to do the XS to AS comparison assuming whatever generated for BAM did so correctly.</p>
  
  <pre><code>for read in bam.fetch()  
      if read.is_secondary=true  
          continue
      if read.has_tag('XS') &lt;- This block stores XS and then moves on to the next read
          XS = read.get_tag('XS')  
          continue 
      if read.has_tag('AS') 
          AS = read.get_tag('AS')  
      if AS &lt; XS  &lt;- This block compares the AS of the current read to the XS of the previous read that had one.
          read.write(read)  
  bam.close()  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-13T05:25:43.837Z
Answers.Text: I have not tried to executec it as this is my first `python` script i am looking for suggestions. Thank you :).
Answers.UserDisplayName: justaguy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-13T15:21:54.627Z
Answers.Text: I would suggest running the script to determine if it is doing what you want.  What I pointed out above is def a flaw in the logic.
Answers.UserDisplayName: Bioathlete
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-15T13:12:28.430Z
Answers.Text: I ran the script (modified it in the post) and there are many syntax errors... being new to `python` I am not sure what is best... what do you suggest? Thanks :).
Answers.UserDisplayName: justaguy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-15T16:03:27.063Z
Answers.Text: I would start will researching how to write to a file.  `read.write(read)` is not correct syntax.  You will need to open a file and write to that.  Go error by error until you have a working script then determine if the out is correct.
Answers.UserDisplayName: Bioathlete
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-13T02:41:48.677Z
Answers.LastActivityDate: 2019-04-13T02:41:48.677Z
Answers.UserDisplayName: Bioathlete
Body: <p>I am new to <code>python</code> and trying to learn. The below is an attempt to filter out secondary reads in a <code>bam</code> using <code>pysam</code>. I ran <code>mark duplicates</code> and got an error on several <code>bam</code> files due to secondary alignments. I added comments to each line to illustrate my thought process.  Thank you :).</p>
  
  <p>The specific <code>mark duplicates</code> error:</p>
  
  <pre><code>Value was put into PairInfoMap more than once
  </code></pre>
  
  <p>Here is the script:</p>
  
  <pre><code>#! /usr/bin/python  ## call python script
  import sys ## import python system functions
  import pysam  ## import module
  
  bam = pysam.AlignmentFile(*.bam, "rb")  ## open bam and read
  click.echo("Reading BAM file")  ## output message
  for read in bam:  ## start loop and iterate over each bam
      if read.is_secondary=true  ## not the primary alignment
      if read.has_tag('XS')  ## look for Secondary Alignment Score tag
          XS = read.get_tag('XS')  ## read and store AS value
      if read.has_tag('AS')  ## look for Alignment Score tag
          AS = read.get_tag('AS')  ## read and store value os XS tag  
      if AS &lt; XS  ## Alignment score less then 
          read.write(read)  ## only print primary alignments
  bam.close()  ## end processing
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-13T02:34:10.017Z
Comments.Text: Is there an issue with the script?  If so what is it?  Are you looking for feedback on the script?
Comments.UserDisplayName: Bioathlete
Tags: <python><pysam>
Title: filter secondary alignments using pysam
ThreadText: # filter secondary alignments using pysam
  
  (Asked by: username_0 on 2019-04-13)
  
  I am new to `python` and trying to learn. The below is an attempt to filter out secondary reads in a `bam` using `pysam`. I ran `mark duplicates` and got an error on several `bam` files due to secondary alignments. I added comments to each line to illustrate my thought process. Thank you :).
  
  The specific `mark duplicates` error:
  
  ```
  Value was put into PairInfoMap more than once
  
  ```
  
  Here is the script:
  
  ```
  #! /usr/bin/python  ## call python script
  import sys ## import python system functions
  import pysam  ## import module
  
  bam = pysam.AlignmentFile(*.bam, "rb")  ## open bam and read
  click.echo("Reading BAM file")  ## output message
  for read in bam:  ## start loop and iterate over each bam
      if read.is_secondary=true  ## not the primary alignment
      if read.has_tag('XS')  ## look for Secondary Alignment Score tag
          XS = read.get_tag('XS')  ## read and store AS value
      if read.has_tag('AS')  ## look for Alignment Score tag
          AS = read.get_tag('AS')  ## read and store value os XS tag  
      if AS < XS  ## Alignment score less then 
          read.write(read)  ## only print primary alignments
  bam.close()  ## end processing
  
  ```
  
  **Tags:** python, pysam
  
  > **Comments:**
  > **username_1**: Is there an issue with the script?  If so what is it?  Are you looking for feedback on the script?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-13. Score: 3
  
  Quick look at your script suggests that you are comparing the XS and AS from different reads. I would recommend removing the continue from the `read.has_tag('XS')` continue block.
  
  I think that if you filter out `is_secondary` and `is_supplementary` you don't need to do the XS to AS comparison assuming whatever generated for BAM did so correctly.
  
  ```
  for read in bam.fetch()  
      if read.is_secondary=true  
          continue
      if read.has_tag('XS') <- This block stores XS and then moves on to the next read
          XS = read.get_tag('XS')  
          continue 
      if read.has_tag('AS') 
          AS = read.get_tag('AS')  
      if AS < XS  <- This block compares the AS of the current read to the XS of the previous read that had one.
          read.write(read)  
  bam.close()  
  
  ```
  
  > **Comments:**
  > **username_0**: I have not tried to executec it as this is my first `python` script i am looking for suggestions. Thank you :).  
  > **username_1**: I would suggest running the script to determine if it is doing what you want.  What I pointed out above is def a flaw in the logic.  
  > **username_0**: I ran the script (modified it in the post) and there are many syntax errors... being new to `python` I am not sure what is best... what do you suggest? Thanks :).  
  > **username_1**: I would start will researching how to write to a file.  `read.write(read)` is not correct syntax.  You will need to open a file and write to that.  Go error by error until you have a working script then determine if the out is correct.  
  
  ---
  
  