Created
August 14, 2017 11:03
-
-
Save anthony-cros/308874d978c9cf1977deccab970857d3 to your computer and use it in GitHub Desktop.
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
package nebulis.transformation | |
// =========================================================================== | |
object Example extends App { // extending App so the body runs as a main() method | |
val results: Iterable[OutputHybrid] = // "turn a stream of input sample and mutations into a stream of 'hybrid'" | |
OutputHybrid.transform( | |
samples = ???, // assume actual data | |
mutations = ???) | |
// ... | |
} | |
// =========================================================================== | |
// input: these could correspond to the database's tables for instance | |
case class InputSample( | |
sampleId: String, | |
someSampleInfo: String, // note that we could push the type-checking farther and create dedicated case classes even just to wrap a String | |
someSampleInfo2: String) | |
case class InputMutation( | |
mutationId: String, | |
sampleId: String, // many to one relation to sample | |
someMutationNumericalInfo: Double /* compile-time check of type */) { | |
require(someMutationNumericalInfo >= 1) // example of runtime check (can't be encoded with typesystem) | |
} | |
// =========================================================================== | |
// output: this could correspond to a web resource for instance | |
case class OutputHybrid( | |
sampleId: String, | |
someSampleInfo: String, | |
mutationIds: Seq[String], | |
mutationNumericalInfoSum: Double /* imagine for some reason this is useful to the UI */) | |
// --------------------------------------------------------------------------- | |
object OutputHybrid { // doesn't have to be in the companion object necessarily | |
type SampleId = String // mostly for readability | |
def transform( | |
samples: Iterable[InputSample], | |
mutations: Iterable[InputMutation]) | |
: Iterable[OutputHybrid] = { | |
// first group mutations by their sample ID | |
val sampleToMutations: Map[SampleId, Iterable[InputMutation]] = | |
mutations.groupBy(_.sampleId) | |
samples | |
.map { sample => | |
// first retrieve mutations for the current sample | |
val mutations: Iterable[InputMutation] = | |
sampleToMutations(sample.sampleId) | |
// then build the "hybrid" result | |
OutputHybrid( | |
sampleId = sample.sampleId, | |
someSampleInfo = sample.someSampleInfo, | |
mutationIds = mutations.map(_.mutationId).toSeq, | |
mutationNumericalInfoSum = mutations.map(_.someMutationNumericalInfo).sum) | |
// note that we don't use Sample's someSampleInfo2 field | |
} | |
} | |
} | |
// =========================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment