Search code examples
scaladata-structuresmany-to-manyrelationmany-to-one

Scala: Data structure for the representation of 1:1, 1:M, M:1, and M:M relationships between objects of the same type


In comparative genomics, the identification of orthologous genes [which are genes that are believed to have similar function] in two genomes is important in a variety of applications. The relationship between these genes from the two genomes can be 1:1, 1:M, M:1, and M:M. In Scala, I wrote this simple case class to represent a gene:

case class Gene(id: Int, protId: String, geneId: String)

and this function to do the mappings:

def orthologyMapping(genome1: Array[Gene], genome2: Array[Gene]): Vector[HashMap[Gene, Gene]] = { ...

I couldn't find in the documentation any built-in type for this specific type of collection of mapping relations. As you can see, orthologyMapping() return type is Vector[HashMap[Gene, Gene]], and that Vector contains a bunch of HashMap of 1:1 relationships.


Solution

  • Have you considered modeling this set of relationships as a graph? Because that seems like a natural fit to me. If you'd like a library that is ready to use, have a look at Quiver from Verizon's OnCue team: https://verizon.github.io/quiver/