-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContigSerializer.scala
39 lines (34 loc) · 1.17 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
35
36
37
38
39
package org.hammerlab.genomics.loci.set
import com.esotericsoftware.kryo.io.{ Input, Output }
import com.esotericsoftware.kryo.{ Kryo, Serializer ⇒ KryoSerializer }
import com.google.common.collect.{ TreeRangeSet, Range ⇒ JRange }
import JRange.closedOpen
import org.hammerlab.genomics.reference.{ ContigName, Interval, Locus }
/** Serialize a [[LociSet]] simply by writing out its [[Contig]]s. */
class ContigSerializer extends KryoSerializer[Contig] {
def write(kryo: Kryo, output: Output, obj: Contig) = {
kryo.writeObject(output, obj.name)
output.writeInt(obj.numRanges)
for {
Interval(start, end) ← obj.ranges
} {
output.writeLong(start.locus)
output.writeLong(end.locus)
}
}
def read(kryo: Kryo, input: Input, klass: Class[Contig]): Contig = {
val name = kryo.readObject(input, classOf[ContigName])
val numRanges = input.readInt()
val treeRangeSet = TreeRangeSet.create[Locus]()
val ranges =
(0 until numRanges).foreach { _ ⇒
treeRangeSet.add(
closedOpen(
Locus(input.readLong()),
Locus(input.readLong())
)
)
}
Contig(name, treeRangeSet)
}
}