Answers.Body: <p>Yes, if MarkDuplicates encounters a pair that's marked as a duplicate that it considers (for whatever reason) to not be a duplicate then it will unset the duplicate mark. You can test this yourself by making a small BAM file either with or without duplicate entries but some marked regardless. In the case of actual duplicates, the duplicate flag will be maintained. In the case where there are no actual duplicates then the flag will be removed. At least that's the case in version 2.14.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-30T20:15:42.250Z
Answers.LastActivityDate: 2017-10-30T20:15:42.250Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have a RNA-seq bam file and there are few reads that are puzzling me.</p>
  
  <p>According to the bam header, this bam file is sorted by coordinate, created using tophat and markduplicate step is not done. But some reads are marked for being duplicate in the samflag. What is worse is when I run picard markduplicate, these reads' pcr duplicate flag is toggled, marking them not a duplicate. Also I manually found the duplicate of this read (identical reads with same start positions and mates start position) so initial marking looks true.</p>
  
  <p>So my questions are: </p>
  
  <p>Any idea why would this happen?</p>
  
  <p>Does Tophat mark reads for being duplicate? ( I don't think so)
  And does <code>picard markduplicate</code> toggle the duplicate flag if the reads are already marked as being duplicates?</p>
  
  <p>Here is how the read looks before and after mark duplicate step.<br></p>
  
  <p>Before:</p>
  
  <pre><code>C0RTF 1187 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  0UNP1 163 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  </code></pre>
  
  <p>After Markduplicate:</p>
  
  <pre><code>C0RTF 163 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  0UNP1 163 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-30T14:21:48.503Z
Comments.Text: > And does picard markduplicate toggle the duplicate flag if the reads are already marked as being duplicates? yes: https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/markduplicates/MarkDuplicates.java#L342
Comments.UserDisplayName: Pierre
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-30T16:00:58.443Z
Comments.Text: @Pierre I know no Java, but that looks like it will unset the duplicate flag if `isDuplicate` is false. Not if it is already true. Am I missing something?
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-30T16:18:25.307Z
Comments.Text: @terdon: if it's duplicate the flag is set https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/markduplicates/MarkDuplicates.java#L330 , else it is unset https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/markduplicates/MarkDuplicates.java#L342
Comments.UserDisplayName: Pierre
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-30T16:33:09.253Z
Comments.Text: @Pierre yes, exactly. If I understand correctly, the OP has a file with the duplicate flags already set but, when passed through markDuplicates, the flags that were set are now toggled: they're unset, so they are no longer marked as duplicates. The code you show would suggest that if the flag is already set, nothing will be changed. Unless picard doesn't read the flags and only does its own calculations to determine the dupes and, in this case, disagreed with whatever produced the original markings.
Comments.UserDisplayName: terdon
Tags: <rna-seq><samtools><markduplicate>
Title: Does picard markduplicate toggle PCR duplicate samflag
ThreadText: # Does picard markduplicate toggle PCR duplicate samflag
  
  (Asked by: username_0 on 2017-10-27)
  
  I have a RNA-seq bam file and there are few reads that are puzzling me.
  
  According to the bam header, this bam file is sorted by coordinate, created using tophat and markduplicate step is not done. But some reads are marked for being duplicate in the samflag. What is worse is when I run picard markduplicate, these reads' pcr duplicate flag is toggled, marking them not a duplicate. Also I manually found the duplicate of this read (identical reads with same start positions and mates start position) so initial marking looks true.
  
  So my questions are:
  
  Any idea why would this happen?
  
  Does Tophat mark reads for being duplicate? ( I don't think so)
  And does `picard markduplicate` toggle the duplicate flag if the reads are already marked as being duplicates?
  
  Here is how the read looks before and after mark duplicate step.
  
  Before:
  
  ```
  C0RTF 1187 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  0UNP1 163 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  
  ```
  
  After Markduplicate:
  
  ```
  C0RTF 163 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  0UNP1 163 17 7579880 255 61M10754N40M = 7579927 10902 CTC... 
  
  ```
  
  **Tags:** rna-seq, samtools, markduplicate
  
  > **Comments:**
  > **username_1**: > And does picard markduplicate toggle the duplicate flag if the reads are already marked as being duplicates? yes: https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/markduplicates/MarkDuplicates.java#L342  
  > **username_2**: @username_1 I know no Java, but that looks like it will unset the duplicate flag if `isDuplicate` is false. Not if it is already true. Am I missing something?  
  > **username_1**: @username_2: if it's duplicate the flag is set https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/markduplicates/MarkDuplicates.java#L330 , else it is unset https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/sam/markduplicates/MarkDuplicates.java#L342  
  > **username_2**: @username_1 yes, exactly. If I understand correctly, the OP has a file with the duplicate flags already set but, when passed through markDuplicates, the flags that were set are now toggled: they're unset, so they are no longer marked as duplicates. The code you show would suggest that if the flag is already set, nothing will be changed. Unless picard doesn't read the flags and only does its own calculations to determine the dupes and, in this case, disagreed with whatever produced the original markings.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2017-10-30. Score: 6
  
  Yes, if MarkDuplicates encounters a pair that's marked as a duplicate that it considers (for whatever reason) to not be a duplicate then it will unset the duplicate mark. You can test this yourself by making a small BAM file either with or without duplicate entries but some marked regardless. In the case of actual duplicates, the duplicate flag will be maintained. In the case where there are no actual duplicates then the flag will be removed. At least that's the case in version 2.14.
  
  
  ---
  
  