heuermh / bdg-convert-ga4gh

Conversions between GA4GH Protobuf and Big Data Genomics Avro Formats. Apache 2 licensed.
Apache License 2.0
0 stars 1 forks source link

how to create alignmentConverter outside of the package #4

Open jpdna opened 7 years ago

jpdna commented 7 years ago

@heuermh I am trying to use bdg-ga4gh-convert 'alignmentConverter' to map ( or maybe collect to array and map locally) an AlignmentRecordRDD.rdd to ga4gh JSON.

To do so, I need to create a new alignmentConverter in a package outside of org.bdgenomics.convert.ga4gh ( we could create an AlignmentRecordRDDtoJSON converter inside bdg-convert-ga4gh, but I think better to be able to just use the basic object per type converters as a library.)

I try to do this in in scala in an outside package after importing:

import org.bdgenomics.convert.Converter
import org.bdgenomics.convert.ga4gh.{AlignmentRecordToReadAlignment, CigarOperatorToOperation, CigarToCigarUnits}

I try:

var operatorConverter = new CigarOperatorToOperation
var cigarConverter = new CigarToCigarUnits(operatorConverter)
var alignmentConverter = new AlignmentRecordToReadAlignment(cigarConverter)

the above fails and says "not accessible from this location" for AlignmentRecordToReadAlignment and the the other new statements above

How can I instantiate a new AlignmentRecordToReadAlignment?

Should the constructor for AlignmentRecordToReadAlignment by accessible outside the bdg-convert-ga4gh package?

I do notice how AlignmentRecordToReadAlignment is not public while: public abstract class AbstractConverter<S, T> implements Converter<S, T>, Serializable { is public, but I still clearly I dont' understand how to use AlingmentRecordToReadAlignment from outside the package, and can't see to correct create it as an instance of the public base package.

heuermh commented 7 years ago

This is where the dependency injection stuff comes in, you don't have to call new. In fact, as you've noticed, the constructors are all package private, so you can't.

Add Converter<AlignmentRecord, ReadAlignment> as a parameter to your constructor and an @Inject annotation and it will be provided by Guice.

@Inject
MyClass(final Converter<AlignmentRecord, ReadAlignment> converter) { ... }

Or to do it directly,

Injector injector = Guice
  .createInjector(new Ga4ghModule());

Converter<AlignmentRecord, ReadAlignment> converter = injector
  .getInstance(Key.get(new TypeLiteral<Converter<AlignmentRecord, ReadAlignment>>() {}));
jpdna commented 7 years ago

Do you happen to have an example injecting into a scala class? Google seems to say yes possible with scala, but if you have an example handy....

heuermh commented 7 years ago

Guice is already used in ADAM for injecting commands into ADAMMain

https://github.com/bigdatagenomics/adam/blob/master/adam-cli/src/main/scala/org/bdgenomics/adam/cli/ADAMMain.scala#L73

https://github.com/bigdatagenomics/adam/blob/master/adam-cli/src/main/scala/org/bdgenomics/adam/cli/ADAMMain.scala#L144

https://github.com/bigdatagenomics/adam/blob/master/adam-cli/src/test/scala/org/bdgenomics/adam/cli/ADAMMainSuite.scala#L33

I can write up a scala example. What is the class that would be using the converter? Or should I work up an example in the shell?

heuermh commented 7 years ago

Note https://github.com/bigdatagenomics/bdg-convert/issues/27 has been fixed; this repository has been migrated to bdg-convert as a new Maven module. I'll leave this repo as is for a week or so then close/migrate any issues upstream.