Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for BQSR: Offset into qualityScore list was wrong #60

Merged
merged 1 commit into from
Jan 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ private[rdd] object RecalibrateBaseQualities extends Serializable with Logging {
val rdd = poorRdd.map(new RichADAMRecord(_))
// initialize the covariates
println("Instantiating covariates...")
val qualByRG = new QualByRG(rdd)
val otherCovars = List(new DiscreteCycle(rdd), new BaseContext(rdd))
val qualByRG = new QualByRG()
val otherCovars = List(new DiscreteCycle(), new BaseContext())
println("Creating object...")
val recalibrator = new RecalibrateBaseQualities(qualByRG, otherCovars)
println("Computing table...")
Expand All @@ -52,7 +52,6 @@ private[rdd] class RecalibrateBaseQualities(val qualCovar: QualByRG, val covars:
def computeTable(rdd: RDD[RichADAMRecord], dbsnp: SparkBroadcast[SnpTable]): RecalTable = {

def addCovariates(table: RecalTable, covar: ReadCovariates): RecalTable = {
//log.info("Aggregating covarates for read "+covar.read.record.getReadName.toString)
table + covar
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,35 @@ object ReadCovariates {
}

class ReadCovariates(val read: RichADAMRecord, qualByRG: QualByRG, covars: List[StandardCovariate],
val dbSNP: SnpTable) extends Iterator[BaseCovariates] with Serializable {
val dbSNP: SnpTable, val minQuality:Int = 2) extends Iterator[BaseCovariates] with Serializable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about the min quality value here. Is it arbitrary or a commonly used filter value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't know the significance of using 2. This was another reason I was getting different read counts vs GATK as their default min base quality is 6. At least this way its parameterizable, but if there is a more justified default we should probably change it to that. Or add a command line argument for it.

On that note, the argument options for Transform are growing large - is the goal to keep all of the operations in there (and all their various configurations) or to start splitting them out?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phred 2 is P=0.5.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah—you're right—Phred 3 is P=0.5... My apologies for the mistake!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q2 is a legacy value; it's effectively Illumina's garbage value; they tend
to be bad bases even after recalibration. Q2 is what the machine produces
when it's read through a nucleotide repeat and has no real idea what the
bases are. See below: all the faint bases are Q2.
[image: Inline image 1]

On Mon, Jan 27, 2014 at 1:54 PM, Matt Massie notifications@github.comwrote:

In
adam-core/src/main/scala/edu/berkeley/cs/amplab/adam/rdd/recalibration/ReadCovariates.scala:

@@ -28,27 +28,35 @@ object ReadCovariates {
}

class ReadCovariates(val read: RichADAMRecord, qualByRG: QualByRG, covars: List[StandardCovariate],

  •                 val dbSNP: SnpTable) extends Iterator[BaseCovariates] with Serializable {
    
  •                 val dbSNP: SnpTable, val minQuality:Int = 2) extends Iterator[BaseCovariates] with Serializable {
    

I think Phred 2 is P=0.63

https://www.google.com/search?q=10+%5E+(-2%2F10)&oq=10+%5E+(-2%2F10)&aqs=chrome..69i57j0j69i64l2.7131j0j7&sourceid=chrome&espv=210&es_sm=119&ie=UTF-8
)


Reply to this email directly or view it on GitHubhttps://github.com//pull/60/files#r9200596
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are also one of the primary targets for aligners that clip
low-quality tails.

On Mon, Jan 27, 2014 at 2:01 PM, christopherlhartl@gmail.com <
christopherlhartl@gmail.com> wrote:

Q2 is a legacy value; it's effectively Illumina's garbage value; they tend
to be bad bases even after recalibration. Q2 is what the machine produces
when it's read through a nucleotide repeat and has no real idea what the
bases are. See below: all the faint bases are Q2.
[image: Inline image 1]

On Mon, Jan 27, 2014 at 1:54 PM, Matt Massie notifications@github.comwrote:

In
adam-core/src/main/scala/edu/berkeley/cs/amplab/adam/rdd/recalibration/ReadCovariates.scala:

@@ -28,27 +28,35 @@ object ReadCovariates {
}

class ReadCovariates(val read: RichADAMRecord, qualByRG: QualByRG, covars: List[StandardCovariate],

  •                 val dbSNP: SnpTable) extends Iterator[BaseCovariates] with Serializable {
    
  •                 val dbSNP: SnpTable, val minQuality:Int = 2) extends Iterator[BaseCovariates] with Serializable {
    

I think Phred 2 is P=0.63

https://www.google.com/search?q=10+%5E+(-2%2F10)&oq=10+%5E+(-2%2F10)&aqs=chrome..69i57j0j69i64l2.7131j0j7&sourceid=chrome&espv=210&es_sm=119&ie=UTF-8
)


Reply to this email directly or view it on GitHubhttps://github.com//pull/60/files#r9200596
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'transform' commandline growing large is ok for now. We want all the transformations to be part of a single job (to minimize i/o, improve pipelining, etc). In the future, we'll probably need to use a configuration file instead which defined the exact pipeline of operations and options the user wants.


val startOffset = read.qualityScores.takeWhile(_ <= 2).size
val endOffset = read.qualityScores.size - read.qualityScores.reverseIterator.takeWhile(_ <= 2).size
val qualCovar: Array[Int] = qualByRG(read, startOffset, endOffset)
val requestedCovars: List[Array[Int]] = covars.map(covar => covar(read, startOffset, endOffset))
def isLowQualityBase(qual : Byte) : Boolean = {
qual.toInt <= minQuality
}

val qualityStartOffset = read.qualityScores.takeWhile(isLowQualityBase).size
val qualityEndOffset = read.qualityScores.size - read.qualityScores.reverseIterator.takeWhile(isLowQualityBase).size


val qualCovar: Array[Int] = qualByRG(read, qualityStartOffset, qualityEndOffset)
val requestedCovars: List[Array[Int]] = covars.map(covar => covar(read, qualityStartOffset, qualityEndOffset))

var readOffset = qualityStartOffset

var iter_position = startOffset

override def hasNext: Boolean = iter_position < endOffset
override def hasNext: Boolean = readOffset < qualityEndOffset

override def next(): BaseCovariates = {
val offset = (iter_position - startOffset).toInt
val mismatch = read.isMismatchAtReadOffset(offset)
val baseCovarOffset = readOffset - qualityStartOffset
val mismatch = read.isMismatchAtReadOffset(readOffset)
// FIXME: why does empty mismatch mean it should be masked?
val isMasked = dbSNP.isMaskedAtReadOffset(read, offset) || mismatch.isEmpty
val isMasked = dbSNP.isMaskedAtReadOffset(read, readOffset) || mismatch.isEmpty
// getOrElse because reads without an MD tag can appear during *application* of recal table
val isMismatch = mismatch.getOrElse(false)
iter_position += 1
new BaseCovariates(qualCovar(offset), requestedCovars.map(v => v(offset)).toArray,
read.qualityScores(offset), isMismatch, isMasked)
val qualityScore = read.qualityScores(readOffset)
readOffset += 1
new BaseCovariates(qualCovar(baseCovarOffset), requestedCovars.map(v => v(baseCovarOffset)).toArray,
qualityScore, isMismatch, isMasked)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import edu.berkeley.cs.amplab.adam.rich.RichADAMRecord._
import org.apache.spark.rdd.RDD

// this class is required, not just standard. Baked in to recalibration.
class QualByRG(rdd: RDD[RichADAMRecord]) extends Serializable {
class QualByRG() extends Serializable {

def apply(read: RichADAMRecord, start: Int, end: Int): Array[Int] = {
val rg_offset = RecalUtil.Constants.MAX_REASONABLE_QSCORE * read.getRecordGroupId
Expand All @@ -33,9 +33,10 @@ class QualByRG(rdd: RDD[RichADAMRecord]) extends Serializable {

trait StandardCovariate extends Serializable {
def apply(read: RichADAMRecord, start: Int, end: Int): Array[Int] // get the covariate for all the bases of the read

}

case class DiscreteCycle(args: RDD[RichADAMRecord]) extends StandardCovariate {
case class DiscreteCycle() extends StandardCovariate {
// this is a special-case of the GATK's Cycle covariate for discrete technologies.
// Not to be used for 454 or ion torrent (which are flow cycles)
def apply(read: RichADAMRecord, startOffset: Int, endOffset: Int): Array[Int] = {
Expand All @@ -46,10 +47,7 @@ case class DiscreteCycle(args: RDD[RichADAMRecord]) extends StandardCovariate {
}
}

case class BaseContext(records: RDD[RichADAMRecord], size: Int) extends StandardCovariate {
def this(_s: Int) = this(null, _s)

def this(_r: RDD[RichADAMRecord]) = this(_r, 2)
case class BaseContext(size: Int = 2) extends StandardCovariate {

val BASES = Array('A'.toByte, 'C'.toByte, 'G'.toByte, 'T'.toByte)
val COMPL = Array('T'.toByte, 'G'.toByte, 'C'.toByte, 'A'.toByte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@ class RecalibrateBaseQualitiesSuite extends SparkFunSuite {
val rec2 = ADAMRecord.newBuilder().setRecordGroupId(rg2).setQual(qualStr(qual2)).build()
val rec3 = ADAMRecord.newBuilder().setRecordGroupId(rg3).setQual(qualStr(qual3)).build()
val records = List(rec1, rec2, rec3)
val recRDD = sc.makeRDD(records, 1).map(new RichADAMRecord(_))
System.out.println(recRDD.first())
val qualByRG = new QualByRG(recRDD)

val qualByRG = new QualByRG()
val intervals = List((0, 29), (6, 29), (0, 21), (0, 20))
for (interval <- intervals) {
assert(qualByRG(rec1, interval._1, interval._2).deep == qual1.slice(interval._1, interval._2).toArray.deep)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo! More tests!

* Copyright (c) 2014. Mount Sinai School of Medicine
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package edu.berkeley.cs.amplab.adam.rdd.recalibration

import edu.berkeley.cs.amplab.adam.util.SparkFunSuite
import edu.berkeley.cs.amplab.adam.avro.ADAMRecord
import edu.berkeley.cs.amplab.adam.models.SnpTable

class ReadCovariatesSuite extends SparkFunSuite {

test("Test Quality Offset"){

val read = ADAMRecord.newBuilder()
.setRecordGroupId(0)
.setReadMapped(true).setStart(10000)
.setReferenceName("1")
.setCigar("10M")
.setMismatchingPositions("5C4")
.setSequence("CTACCCTAAC")
.setQual("##LKLPPQ##")
.build()
var readCovar = ReadCovariates( read, new QualByRG(), List(new BaseContext(2)), SnpTable() )
val firstBaseCovar = readCovar.next()
assert(firstBaseCovar.qual === 43 )
readCovar.foreach(bc => assert(bc.qual === bc.qualByRG) )

readCovar = ReadCovariates( read, new QualByRG(), List(new BaseContext(2)), SnpTable() )
val bases = readCovar.drop(3)
val mismatchedBase = bases.next()
assert(mismatchedBase.qual === 47)
assert(mismatchedBase.isMismatch === true)

}

test("Test ReadCovar on SoftClipped Read"){

val read = ADAMRecord.newBuilder()
.setRecordGroupId(0)
.setReadMapped(true).setStart(10000)
.setReferenceName("1")
.setCigar("2S6M2S")
.setMismatchingPositions("3C2")
.setSequence("CTACCCTAAC")
.setQual("##LKLPPQ##")
.build()
var readCovar = ReadCovariates( read, new QualByRG(), List(new BaseContext(2)), SnpTable() )
val firstBaseCovar = readCovar.next()
assert(firstBaseCovar.qual === 43 )
readCovar.foreach(bc => assert(bc.qual === bc.qualByRG) )

readCovar = ReadCovariates( read, new QualByRG(), List(new BaseContext(2)), SnpTable() )
val bases = readCovar.drop(3)
val mismatchedBase = bases.next()
assert(mismatchedBase.qual === 47)
assert(mismatchedBase.isMismatch === true)

}

}