-
Notifications
You must be signed in to change notification settings - Fork 308
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
Predicate to filter conversion #234
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
adam-core/src/main/scala/org/bdgenomics/adam/predicates/ADAMGenotypeConditions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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 org.bdgenomics.adam.predicates | ||
|
||
import org.bdgenomics.adam.avro.{ ADAMRecord, ADAMGenotype } | ||
|
||
object ADAMGenotypeConditions { | ||
|
||
val isPassing = RecordCondition[ADAMGenotype](FieldCondition("variantCallingAnnotations.variantIsPassing", PredicateUtils.isTrue)) | ||
|
||
def hasMinReadDepth(minReadDepth: Int) = RecordCondition[ADAMRecord](FieldCondition("variantCallingAnnotations.readDepth", (x: Int) => x > minReadDepth)) | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
adam-core/src/main/scala/org/bdgenomics/adam/predicates/ADAMPredicate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 org.bdgenomics.adam.predicates | ||
|
||
import parquet.filter.{ RecordFilter, UnboundRecordFilter } | ||
import java.lang.Iterable | ||
import parquet.column.ColumnReader | ||
import org.apache.spark.rdd.RDD | ||
|
||
/** | ||
* | ||
* ADAMPredicate: Classes derived from ADAMPredicate can be used to set ParquetInputFormat.setUnboundRecordFilter | ||
* for predicate pushdown, or alternatively, filter an already loaded RDD | ||
* | ||
*/ | ||
trait ADAMPredicate[T] extends UnboundRecordFilter { | ||
val recordCondition: RecordCondition[T] | ||
|
||
final def apply(rdd: RDD[T]): RDD[T] = { | ||
rdd.filter(recordCondition.filter) | ||
} | ||
|
||
override def bind(readers: Iterable[ColumnReader]): RecordFilter = recordCondition.recordFilter.bind(readers) | ||
} |
33 changes: 33 additions & 0 deletions
33
adam-core/src/main/scala/org/bdgenomics/adam/predicates/ADAMRecordConditions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 org.bdgenomics.adam.predicates | ||
|
||
import org.bdgenomics.adam.projections.ADAMRecordField | ||
import org.bdgenomics.adam.avro.ADAMRecord | ||
|
||
object ADAMRecordConditions { | ||
|
||
val isMapped = RecordCondition[ADAMRecord](FieldCondition(ADAMRecordField.readMapped.toString, PredicateUtils.isTrue)) | ||
val isUnique = RecordCondition[ADAMRecord](FieldCondition(ADAMRecordField.duplicateRead.toString, PredicateUtils.isFalse)) | ||
|
||
val isPrimaryAlignment = RecordCondition[ADAMRecord](FieldCondition(ADAMRecordField.primaryAlignment.toString, PredicateUtils.isTrue)) | ||
|
||
val passedVendorQualityChecks = RecordCondition[ADAMRecord](FieldCondition(ADAMRecordField.failedVendorQualityChecks.toString, PredicateUtils.isFalse)) | ||
|
||
def isHighQuality(minQuality: Int) = RecordCondition[ADAMRecord](FieldCondition(ADAMRecordField.mapq.toString, (x: Int) => x > minQuality)) | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
adam-core/src/main/scala/org/bdgenomics/adam/predicates/FieldCondition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 org.bdgenomics.adam.predicates | ||
|
||
import parquet.filter.ColumnPredicates.Predicate | ||
import parquet.column.ColumnReader | ||
import parquet.filter.UnboundRecordFilter | ||
import parquet.filter.ColumnRecordFilter._ | ||
import org.bdgenomics.adam.predicates.ColumnReaderInput.ColumnReaderInput | ||
import scala.Predef._ | ||
|
||
object ColumnReaderInput extends Serializable { | ||
trait ColumnReaderInput[T] extends Serializable { | ||
def convert(x: ColumnReader): T | ||
} | ||
implicit object ColumnReaderInputInt extends ColumnReaderInput[Int] { | ||
def convert(input: ColumnReader): Int = input.getInteger | ||
} | ||
implicit object ColumnReaderInputString extends ColumnReaderInput[String] { | ||
def convert(input: ColumnReader): String = input.getBinary.toStringUsingUTF8 | ||
} | ||
implicit object ColumnReaderInputDouble extends ColumnReaderInput[Double] { | ||
def convert(input: ColumnReader): Double = input.getDouble | ||
} | ||
implicit object ColumnReaderInputFloat extends ColumnReaderInput[Float] { | ||
def convert(input: ColumnReader): Float = input.getFloat | ||
} | ||
implicit object ColumnReaderInputBoolean extends ColumnReaderInput[Boolean] { | ||
def convert(input: ColumnReader): Boolean = input.getBoolean | ||
} | ||
} | ||
|
||
private[predicates] case class FieldCondition[T](val fieldName: String, filter: T => Boolean)(implicit converter: ColumnReaderInput[T]) extends Predicate { | ||
|
||
def apply(input: Any): Boolean = { | ||
filter(input.asInstanceOf[T]) | ||
} | ||
|
||
override def apply(input: ColumnReader): Boolean = { | ||
filter(converter.convert(input)) | ||
} | ||
|
||
def columnFilter: UnboundRecordFilter = column(fieldName, this) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
adam-core/src/main/scala/org/bdgenomics/adam/predicates/HighQualityReadPredicate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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 org.bdgenomics.adam.predicates | ||
|
||
import org.bdgenomics.adam.avro.ADAMRecord | ||
|
||
class HighQualityReadPredicate extends ADAMPredicate[ADAMRecord] { | ||
|
||
override val recordCondition = ADAMRecordConditions.isMapped && ADAMRecordConditions.isHighQuality(30) | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
adam-core/src/main/scala/org/bdgenomics/adam/predicates/PredicateUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.bdgenomics.adam.predicates | ||
|
||
object PredicateUtils { | ||
|
||
val isTrue = (x: Boolean) => x == true | ||
val isFalse = (x: Boolean) => x == false | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
adam-core/src/main/scala/org/bdgenomics/adam/predicates/RecordCondition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 org.bdgenomics.adam.predicates | ||
|
||
import org.apache.avro.specific.SpecificRecord | ||
import org.apache.avro.Schema | ||
import org.apache.avro.Schema.Field | ||
import parquet.filter.{ AndRecordFilter, OrRecordFilter, UnboundRecordFilter } | ||
|
||
import org.bdgenomics.adam.util.ImplicitJavaConversions._ | ||
import scala.annotation.tailrec | ||
|
||
private[predicates] object RecordCondition { | ||
|
||
// Convert predicate on single field to predicate on record | ||
def getRecordPredicate[T <: SpecificRecord: Manifest, U](condition: FieldCondition[U]): T => Boolean = { | ||
@tailrec | ||
def getFieldValue(record: SpecificRecord, fieldPath: Seq[String]): Any = { | ||
val schema = record.getSchema | ||
val field: Field = schema.getField(fieldPath.head) | ||
val fieldType = field.schema.getTypes.filter(_.getType != Schema.Type.NULL)(0) | ||
if (fieldType.getType == Schema.Type.RECORD) { | ||
getFieldValue(record.get(field.pos).asInstanceOf[SpecificRecord], fieldPath.tail) | ||
} else { | ||
record.get(field.pos) | ||
} | ||
} | ||
|
||
(record: T) => { | ||
val fieldName = condition.fieldName | ||
val filter: Any => Boolean = condition.apply | ||
val fieldValue = getFieldValue(record, fieldName.split("\\.")) | ||
filter(fieldValue) | ||
} | ||
} | ||
|
||
// Create a record predicate from many individual field predicates | ||
def apply[T <: SpecificRecord: Manifest](conditions: FieldCondition[_]*): RecordCondition[T] = { | ||
conditions.map(c => { | ||
val fieldPredicate = getRecordPredicate(c) | ||
new RecordCondition(fieldPredicate, c.columnFilter) | ||
}).reduce(_ && _) | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* A RecordCondition is a filter on any Avro defined records and | ||
* contains an UnboundRecordFilter that can be used for predicate pushdown | ||
* with Parquet stored files | ||
* | ||
*/ | ||
private[predicates] class RecordCondition[T <% SpecificRecord: Manifest] private (val filter: T => Boolean, | ||
val recordFilter: UnboundRecordFilter) extends Serializable { | ||
|
||
// Combine two predicates through an AND | ||
def and(other: RecordCondition[T]): RecordCondition[T] = &&(other) | ||
def &&(other: RecordCondition[T]): RecordCondition[T] = { | ||
|
||
// Local variables to avoid serialization | ||
val thisFilter = filter | ||
val otherFilter = other.filter | ||
|
||
new RecordCondition[T](filter = (r: T) => thisFilter(r) && otherFilter(r), | ||
recordFilter = AndRecordFilter.and(recordFilter, other.recordFilter)) | ||
} | ||
|
||
// Combine two predicats through an OR | ||
def or(other: RecordCondition[T]): RecordCondition[T] = ||(other) | ||
def ||(other: RecordCondition[T]): RecordCondition[T] = { | ||
|
||
// Local variables to avoid serialization | ||
val thisFilter = filter | ||
val otherFilter = other.filter | ||
|
||
new RecordCondition[T](filter = (r: T) => thisFilter(r) || otherFilter(r), | ||
recordFilter = OrRecordFilter.or(recordFilter, other.recordFilter)) | ||
} | ||
|
||
// Apply the predicate on a record | ||
def apply(record: T): Boolean = { | ||
filter(record) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this was using LocusPredicate before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That predicate is necessary. Pileups are created from mapped reads only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should put a comment to keep others from tripping up on this too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure - but for this is after pileup creation right? Also the fields that LocusPredicate will check against are not defined for an ADAMPileup. I was going to substitute the MappedReadPredicate, but wasn't able to actually because of the typing as that is only applicable on ADAMRecord
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, Arun. This is after pileup creation so the predicate isn't needed.