tests/GenomicSafetyCheck.scala
1package xstream.galaxy
2import java.io._
3import java.nio.charset.StandardCharsets.UTF_8
4import java.util.concurrent.CancellationException
5import com.fasterxml.jackson.databind.ObjectMapper
6import xstream.execution.StreamDescriptor
7object GenomicSafetyCheck {
8 def main(args:Array[String]):Unit={
9 val mapper=new ObjectMapper();val p=mapper.readTree("{}");val inputs=Map("source"->StreamDescriptor(Set("bytes"),Set("text"),certainty="known"))
10 def rejects(f: => Unit):Unit={var failed=false;try f catch{case _:IllegalArgumentException=>failed=true};assert(failed)}
11 Seq("{\"start\":0}","{\"start\":3,\"end\":2}","{\"column\":-1}","{\"fastq\":\"true\"}","{\"ignore_prefixes\":\"a\\nb\"}").foreach(s=>rejects(Trim.inferOutputs(inputs,mapper.readTree(s))))
12 Seq("{\"window_size\":0}","{\"offset\":-1}","{\"start_column\":2,\"end_column\":2}").foreach(s=>rejects(MakeWindows.inferOutputs(inputs,mapper.readTree(s))))
13 Seq("{\"reference_sequence\":\"20\",\"region\":\"20:1..3\"}","{\"region\":\"20:0..3\"}","{\"region\":\"20:3..1\"}","{\"region\":\";sh\"}","{\"keep_info\":\"TV\",\"discard_info\":\"DP\"}","{\"quality\":-1}","{\"quality_operator\":\"eval\"}").foreach(s=>rejects(VcfExtract.inferOutputs(inputs,mapper.readTree(s))))
14 def run(op:GenomicTextOperator,s:String,params:String="{}"):String={val out=new ByteArrayOutputStream;op.transform(new ByteArrayInputStream(s.getBytes(UTF_8)),out,mapper.readTree(params));out.toString("UTF-8")}
15 rejects(run(MakeWindows,"chr1\t0\t100001","{\"window_size\":1}"))
16 rejects(run(MakeWindows,"chr1\t0\t999\t"+"x"*100000,"{\"window_size\":1}"))
17 rejects(run(MakeWindows,"chr1\t10\t2"))
18 rejects(run(Trim,"one","{\"column\":2}"))
19 assert(run(MakeWindows,"chr1\t9223372036854775797\t9223372036854775807","{\"window_size\":5}")=="chr1\t9223372036854775797\t9223372036854775802\nchr1\t9223372036854775802\t9223372036854775807\n")
20 assert(run(MakeWindows,"chr1\t0\t3","{\"window_size\":4,\"offset\":1}").isEmpty)
21 assert(run(VcfExtract,"20\t1\t.\tA\tG\t.\tPASS\tSDP=2","{\"keep_info\":\"DP\"}").isEmpty)
22 assert(run(VcfExtract,"20\t1\t.\tA\tG\t.\tPASS\tDP=2","{\"quality_operator\":\"ge\"}").isEmpty)
23 rejects(run(VcfExtract,"20\t1\t.\tA\tG\tNaN\tPASS\t.","{\"quality_operator\":\"ge\"}"))
24 rejects(run(VcfExtract,"20\t1"))
25 Seq(Trim,MakeWindows,VcfExtract).foreach{op=>
26 rejects(op.inferOutputs(inputs,mapper.readTree("{\"unknown\":1}")))
27 Thread.currentThread.interrupt()
28 try{op.transform(new ByteArrayInputStream(Array.emptyByteArray),new ByteArrayOutputStream,p);throw new AssertionError("cancellation ignored")}
29 catch{case _:CancellationException=>()}finally Thread.interrupted()
30 var io=false;try op.transform(new InputStream{def read():Int=throw new IOException("test")},new ByteArrayOutputStream,p)catch{case _:IOException=>io=true};assert(io)
31 var utf8=false;try op.transform(new ByteArrayInputStream(Array(0xff.toByte)),new ByteArrayOutputStream,p)catch{case _:java.nio.charset.MalformedInputException=>utf8=true};assert(utf8)
32 rejects(op.transform(new ByteArrayInputStream(("x"*1048577).getBytes(UTF_8)),new ByteArrayOutputStream,p))
33 }
34 // Cancellation is checked inside expansion, not just when reading the next input line.
35 val cancelOutput=new OutputStream{var bytes=0;def write(b:Int):Unit={bytes+=1;if(bytes==100)Thread.currentThread.interrupt()}}
36 try{MakeWindows.transform(new ByteArrayInputStream("chr1\t0\t9999".getBytes(UTF_8)),cancelOutput,mapper.readTree("{\"window_size\":1}"));throw new AssertionError("window cancellation ignored")}
37 catch{case _:CancellationException=>()}finally Thread.interrupted()
38 println("PASS trim/VCF/window validation, expansion limits, coordinate boundaries, exact tags, missing quality, UTF-8, I/O and mid-expansion cancellation")
39 }
40}