-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- A plugin class can be defined outside of the Adam jar, but run through the normal AdamMain - An example plugin, the "Take10Plugin" is included in the test directory - Adds a test suite to the cli module, which can reference the items available in the core module - Adds notion of AccessControl to control the records which can be accessed
- Loading branch information
Showing
11 changed files
with
277 additions
and
1 deletion.
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
108 changes: 108 additions & 0 deletions
108
adam-cli/src/main/scala/edu/berkeley/cs/amplab/adam/cli/PluginExecutor.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,108 @@ | ||
/** | ||
* Copyright 2014 Genome Bridge LLC | ||
* | ||
* 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.cli | ||
|
||
import org.kohsuke.args4j.{Argument,Option=>Args4jOption} | ||
import org.apache.spark.SparkContext | ||
import org.apache.hadoop.mapreduce.Job | ||
import edu.berkeley.cs.amplab.adam.plugins.{AccessControl, AdamPlugin} | ||
import edu.berkeley.cs.amplab.adam.rdd.AdamContext._ | ||
import org.apache.avro.Schema | ||
import org.apache.spark.rdd.RDD | ||
import parquet.filter.UnboundRecordFilter | ||
import org.apache.avro.specific.SpecificRecord | ||
import edu.berkeley.cs.amplab.adam.avro.ADAMRecord | ||
|
||
object PluginExecutor extends AdamCommandCompanion { | ||
val commandName: String = "plugin_executor" | ||
val commandDescription: String = "Executes an AdamPlugin" | ||
|
||
def apply(cmdLine: Array[String]): AdamCommand = { | ||
new ListDict(Args4j[ListDictArgs](cmdLine)) | ||
} | ||
} | ||
|
||
class PluginExecutorArgs extends Args4jBase with SparkArgs with ParquetArgs { | ||
@Argument(required = true, metaVar = "PLUGIN", usage = "The AdamPlugin to run", index = 0) | ||
var plugin: String = null | ||
|
||
@Argument(required = true, metaVar = "INPUT", usage = "The input locations", index = 1) | ||
var input: String = null | ||
|
||
@Args4jOption(name = "-access_control", usage = "Class for access control") | ||
var accessControl: String = "edu.berkeley.cs.amplab.adam.plugins.EmptyAccessControl" | ||
} | ||
|
||
class PluginExecutor(protected val args: PluginExecutorArgs) extends AdamSparkCommand[PluginExecutorArgs] { | ||
val companion: AdamCommandCompanion = PluginExecutor | ||
|
||
def loadPlugin[Input <% SpecificRecord : Manifest, Output](pluginName: String): AdamPlugin[Input, Output] = { | ||
Thread.currentThread() | ||
.getContextClassLoader | ||
.loadClass(pluginName) | ||
.newInstance() | ||
.asInstanceOf[AdamPlugin[Input, Output]] | ||
} | ||
|
||
def loadAccessControl[Input <% SpecificRecord : Manifest](accessControl: String): AccessControl[Input] = { | ||
Thread.currentThread() | ||
.getContextClassLoader | ||
.loadClass(accessControl) | ||
.newInstance() | ||
.asInstanceOf[AccessControl[Input]] | ||
} | ||
|
||
def load[Input <% SpecificRecord : Manifest](sc: SparkContext, locations: String, projection: Option[Schema]): RDD[Input] = { | ||
sc.adamLoad[Input, UnboundRecordFilter](locations, projection = projection) | ||
} | ||
|
||
def output[Output](sc: SparkContext, output: RDD[Output]) { | ||
output.map(_.toString).collect().foreach(println) | ||
} | ||
|
||
def run(sc: SparkContext, job: Job): Unit = { | ||
val plugin = loadPlugin[ADAMRecord,Any](args.plugin) | ||
val accessControl = loadAccessControl[ADAMRecord](args.accessControl) | ||
|
||
// Create an optional combined filter so that pass-through is not penalized | ||
val filter = accessControl.predicate match { | ||
case None => plugin.predicate match { | ||
case None => None | ||
case Some(predicateFilter) => Some(predicateFilter) | ||
} | ||
case Some(accessControlPredicate) => plugin.predicate match { | ||
case None => Some(accessControlPredicate) | ||
case Some(predicateFilter) => Some((value: ADAMRecord) => accessControlPredicate(value) && predicateFilter(value)) | ||
} | ||
} | ||
|
||
val firstRdd : RDD[ADAMRecord] = load[ADAMRecord](sc, args.input, plugin.projection) | ||
|
||
val input = filter match { | ||
case None => firstRdd | ||
case Some(filterFunc) => firstRdd.filter(filterFunc) | ||
} | ||
|
||
println("# Input records: %d".format(input.count())) | ||
|
||
val results = plugin.run(sc, input) | ||
|
||
println("# Output records: %d".format(results.count())) | ||
|
||
output(sc, results) | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
adam-cli/src/test/scala/edu/berkeley/cs/amplab/adam/cli/PluginExecutorSuite.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,48 @@ | ||
/** | ||
* Copyright 2014 Genome Bridge LLC | ||
* | ||
* 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.cli | ||
|
||
import java.io._ | ||
import org.scalatest.FunSuite | ||
|
||
class PluginExecutorSuite extends FunSuite { | ||
|
||
test("take10 works correctly on example SAM") { | ||
|
||
val args = new PluginExecutorArgs() | ||
args.plugin = "edu.berkeley.cs.amplab.adam.plugins.Take10Plugin" | ||
val stream = Thread.currentThread().getContextClassLoader.getResourceAsStream("reads12.sam") | ||
val file = File.createTempFile("reads12", ".sam") | ||
val os = new FileOutputStream(file) | ||
val bytes = new Array[Byte](stream.available()) | ||
stream.read(bytes) | ||
os.write(bytes) | ||
args.input = file.getAbsolutePath | ||
|
||
val pluginExecutor = new PluginExecutor(args) | ||
|
||
val pipeIn = new PipedInputStream() | ||
val ps = new PrintStream(new PipedOutputStream(pipeIn)) | ||
//scala.Console.withOut(ps)(() => pluginExecutor.run()) | ||
pluginExecutor.run() | ||
ps.close() | ||
|
||
val reader = new BufferedReader(new InputStreamReader(pipeIn)) | ||
val outputString = reader.readLine() | ||
//assert(outputString === "foo bar") | ||
|
||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
adam-core/src/main/scala/edu/berkeley/cs/amplab/adam/plugins/AccessControl.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,20 @@ | ||
/** | ||
* Copyright 2014 Genome Bridge LLC | ||
* | ||
* 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.plugins | ||
|
||
trait AccessControl[Input] { | ||
def predicate: Option[Input => Boolean] | ||
} |
27 changes: 27 additions & 0 deletions
27
adam-core/src/main/scala/edu/berkeley/cs/amplab/adam/plugins/AdamPlugin.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 2014 Genome Bridge LLC | ||
* | ||
* 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.plugins | ||
|
||
import org.apache.avro.Schema | ||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.SparkContext | ||
|
||
trait AdamPlugin[Input, Output] { | ||
def projection : Option[Schema] | ||
def predicate : Option[Input => Boolean] | ||
|
||
def run(sc: SparkContext, recs: RDD[Input]): RDD[Output] | ||
} |
20 changes: 20 additions & 0 deletions
20
adam-core/src/main/scala/edu/berkeley/cs/amplab/adam/plugins/EmptyAccessControl.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,20 @@ | ||
/** | ||
* Copyright 2014 Genome Bridge LLC | ||
* | ||
* 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.plugins | ||
|
||
class EmptyAccessControl[T] extends AccessControl[T] with Serializable { | ||
override def predicate: Option[T => Boolean] = None | ||
} |
30 changes: 30 additions & 0 deletions
30
adam-core/src/test/scala/edu/berkeley/cs/amplab/adam/plugins/Take10Plugin.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,30 @@ | ||
/** | ||
* Copyright 2014 Genome Bridge LLC | ||
* | ||
* 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.plugins | ||
|
||
import edu.berkeley.cs.amplab.adam.avro.ADAMRecord | ||
import org.apache.avro.Schema | ||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.SparkContext | ||
|
||
class Take10Plugin extends AdamPlugin[ADAMRecord, ADAMRecord] with Serializable { | ||
override def projection: Option[Schema] = None | ||
override def predicate: (ADAMRecord) => Boolean = _ => true | ||
|
||
override def run(sc: SparkContext, recs: RDD[ADAMRecord]): RDD[ADAMRecord] = { | ||
sc.parallelize(recs.take(10)) | ||
} | ||
} |