Skip to content

Commit

Permalink
[ADAM-941] Automatically generate projection enums.
Browse files Browse the repository at this point in the history
Resolves #941.
  • Loading branch information
fnothaft authored and heuermh committed Jul 11, 2017
1 parent 467db1f commit 324ae74
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 497 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package org.bdgenomics.adam.codegen

import java.io.{ File, FileWriter }
import org.apache.avro.Schema
import org.apache.avro.reflect.ReflectData
import scala.collection.JavaConversions._

object DumpSchemasToProduct {
Expand All @@ -29,17 +28,7 @@ object DumpSchemasToProduct {
}
}

class DumpSchemasToProduct {

private def getSchemaByReflection(className: String): Schema = {

// load the class
val classLoader = Thread.currentThread().getContextClassLoader()
val klazz = classLoader.loadClass(className)

// get the schema through reflection
ReflectData.get().getSchema(klazz)
}
class DumpSchemasToProduct extends Generator {

private def toMatch(fields: Seq[(String, String)]): String = {
fields.map(_._1)
Expand Down Expand Up @@ -145,7 +134,7 @@ class DumpSchemasToProduct {
private def generateClassDump(className: String): String = {

// get schema
val schema = getSchemaByReflection(className)
val schema = ReflectSchema.getSchemaByReflection(className)

// get class name without package
val classNameNoPackage = className.split('.').last
Expand Down Expand Up @@ -218,33 +207,6 @@ class DumpSchemasToProduct {
getters(schema))
}

private def writeHeader(fw: FileWriter, packageName: String) {
val hdr = Seq(
"/**",
"* Licensed to Big Data Genomics (BDG) under one",
"* or more contributor license agreements. See the NOTICE file",
"* distributed with this work for additional information",
"* regarding copyright ownership. The BDG licenses this file",
"* to you 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 %s".format(packageName),
"",
"import scala.collection.JavaConversions._",
"import scala.collection.JavaConverters._").mkString("\n")

fw.write(hdr)
}

def apply(args: Array[String]) {

if (args.length < 3) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you 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.codegen

import java.io.{ File, FileWriter }
import org.apache.avro.Schema
import org.apache.avro.reflect.ReflectData
import scala.collection.JavaConversions._

object DumpSchemasToProjectionEnums {

def main(args: Array[String]) {
new DumpSchemasToProjectionEnums()(args)
}
}

class DumpSchemasToProjectionEnums extends Generator {

private def fields(schema: Schema): Seq[String] = {
schema.getFields()
.map(field => field.name)
.toSeq
}

private def generateClassDump(className: String): String = {

// get schema
val schema = ReflectSchema.getSchemaByReflection(className)

// get class name without package
val classNameNoPackage = className.split('.').last

"\n\nobject %sField extends FieldEnumeration(%s.SCHEMA$) {\n val %s = SchemaValue\n}".format(
classNameNoPackage,
className,
fields(schema).mkString(", "))
}

def apply(args: Array[String]) {

if (args.length < 3) {
println("DumpSchemasToProjectionEnums <package-name> <class-to-dump> ... <file-to-dump-to>")
System.exit(1)
} else {

// drop the file to write and the package name
val classesToDump = args.drop(1).dropRight(1)

// open the file to write
val dir = new File(args.last).getParentFile
if (!dir.exists()) {
dir.mkdirs()
}
val fw = new FileWriter(args.last)

// write the header
writeHeader(fw, args.head)

// loop and dump the classes
classesToDump.foreach(className => {
val dumpString = generateClassDump(className)

fw.write("\n")
fw.write(dumpString)
})

// we are done, so close and flush
fw.close()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG licenses this file
* to you 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.codegen

import java.io.FileWriter

trait Generator {

protected def writeHeader(fw: FileWriter, packageName: String) {
val hdr = Seq(
"/**",
"* Licensed to Big Data Genomics (BDG) under one",
"* or more contributor license agreements. See the NOTICE file",
"* distributed with this work for additional information",
"* regarding copyright ownership. The BDG licenses this file",
"* to you 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 %s".format(packageName),
"",
"import scala.collection.JavaConversions._",
"import scala.collection.JavaConverters._").mkString("\n")

fw.write(hdr)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bdgenomics.adam.projections
package org.bdgenomics.adam.codegen

import org.bdgenomics.formats.avro.Fragment
import org.apache.avro.reflect.ReflectData
import org.apache.avro.Schema

/**
* Enumeration of Fragment field names for predicates and projections.
*/
object FragmentField extends FieldEnumeration(Fragment.SCHEMA$) {
object ReflectSchema {

private[codegen] def getSchemaByReflection(className: String): Schema = {

// load the class
val classLoader = Thread.currentThread().getContextClassLoader()
val klazz = classLoader.loadClass(className)

val readName, instrument, runId, fragmentSize, alignments = SchemaValue
// get the schema through reflection
ReflectData.get().getSchema(klazz)
}
}
32 changes: 32 additions & 0 deletions adam-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,38 @@
<classpathScope>compile</classpathScope>
</configuration>
</execution>
<execution>
<id>generate-scala-projection-fields</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.bdgenomics.adam.codegen.DumpSchemasToProjectionEnums</mainClass>
<arguments>
<argument>org.bdgenomics.adam.projections</argument>
<argument>org.bdgenomics.formats.avro.AlignmentRecord</argument>
<argument>org.bdgenomics.formats.avro.Contig</argument>
<argument>org.bdgenomics.formats.avro.Dbxref</argument>
<argument>org.bdgenomics.formats.avro.Feature</argument>
<argument>org.bdgenomics.formats.avro.Fragment</argument>
<argument>org.bdgenomics.formats.avro.Genotype</argument>
<argument>org.bdgenomics.formats.avro.NucleotideContigFragment</argument>
<argument>org.bdgenomics.formats.avro.OntologyTerm</argument>
<argument>org.bdgenomics.formats.avro.Read</argument>
<argument>org.bdgenomics.formats.avro.RecordGroup</argument>
<argument>org.bdgenomics.formats.avro.Sample</argument>
<argument>org.bdgenomics.formats.avro.Sequence</argument>
<argument>org.bdgenomics.formats.avro.Slice</argument>
<argument>org.bdgenomics.formats.avro.TranscriptEffect</argument>
<argument>org.bdgenomics.formats.avro.Variant</argument>
<argument>org.bdgenomics.formats.avro.VariantAnnotation</argument>
<argument>org.bdgenomics.formats.avro.VariantCallingAnnotations</argument>
<argument>adam-core/target/generated-sources/src/main/scala/org/bdgenomics/adam/projections/Enums.scala</argument>
</arguments>
<classpathScope>compile</classpathScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 324ae74

Please sign in to comment.