-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContigSerializer.scala
35 lines (31 loc) · 1.2 KB
/
ContigSerializer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.hammerlab.genomics.loci.map
import com.esotericsoftware.kryo.io.{ Input, Output }
import com.esotericsoftware.kryo.{ Kryo, Serializer ⇒ KryoSerializer }
import org.hammerlab.genomics.reference.{ ContigName, Locus }
/**
* We serialize a Contig as its name, the number of ranges, and the ranges themselves (two longs and a value each).
*/
class ContigSerializer[T] extends KryoSerializer[Contig[T]] {
def write(kryo: Kryo, output: Output, obj: Contig[T]) = {
kryo.writeObject(output, obj.name)
output.writeLong(obj.asMap.size)
obj.asMap.foreach {
case (range, value) =>
output.writeLong(range.start.locus)
output.writeLong(range.end.locus)
kryo.writeClassAndObject(output, value)
}
}
def read(kryo: Kryo, input: Input, klass: Class[Contig[T]]): Contig[T] = {
val builder = LociMap.newBuilder[T]
val contig = kryo.readObject(input, classOf[ContigName])
val count = input.readLong()
(0L until count).foreach { _ =>
val start = input.readLong()
val end = input.readLong()
val value: T = kryo.readClassAndObject(input).asInstanceOf[T]
builder.put(contig, Locus(start), Locus(end), value)
}
builder.result(contig)
}
}