src/Fastq.scala
1package xstream.galaxy
2
3import java.io._
4import java.nio.charset.{StandardCharsets,CodingErrorAction}
5import java.util.concurrent.CancellationException
6import com.fasterxml.jackson.databind.JsonNode
7import scala.jdk.CollectionConverters._
8import xstream.operator.{Operator,ByteStreamOperator,OperatorContext}
9import xstream.execution.{StreamDescriptor,InferenceResult,OutputSample,PreviewLimits}
10
11/** Four-line FASTQ conversion with explicit quality encoding and bounded records. */
12abstract class FastqOperator(qualityOutput:Boolean) extends Operator {
13 private def active():Unit=if(Thread.currentThread.isInterrupted)throw new CancellationException("operator cancelled")
14 private def encoding(p:JsonNode):String={
15 require(p!=null&&p.isObject&&p.fieldNames.asScala.forall(_=="encoding"),"only encoding is supported")
16 require(!p.has("encoding")||p.path("encoding").isTextual,"encoding must be text")
17 val e=p.path("encoding").asText("solexa64")
18 require(Set("solexa64","sanger33","integers")(e),"unsupported quality encoding");e
19 }
20 private val delegate=new ByteStreamOperator {
21 override def outputFormat:String=if(qualityOutput)"qual"else"fasta"
22 def transform(input:InputStream,output:OutputStream,p:JsonNode):Unit={
23 val e=encoding(p);active()
24 val in=new PushbackReader(new BufferedReader(new InputStreamReader(input,StandardCharsets.UTF_8.newDecoder.onMalformedInput(CodingErrorAction.REPORT))),1)
25 val out=new BufferedWriter(new OutputStreamWriter(output,StandardCharsets.UTF_8))
26 def line():Option[String]={
27 val b=new java.lang.StringBuilder;var c=in.read();if(c == -1)return None
28 while(c != -1&&c!='\n'&&c!='\r'){if((b.length&4095)==0)active();require(b.length<1048576,"FASTQ line exceeds 1 MiB characters");b.append(c.toChar);c=in.read()}
29 if(c=='\r'){val n=in.read();if(n != -1&&n!='\n')in.unread(n)};Some(b.toString)
30 }
31 def required():String=line().getOrElse(throw new IllegalArgumentException("truncated four-line FASTQ record"))
32 var header=line()
33 while(header.nonEmpty){
34 active();val h=header.get;require(h.startsWith("@")&&h.length>1,"FASTQ header must start with @ and have a title")
35 val seq=required();val plus=required();val q=required()
36 require(seq.forall(c=>c>32&&c<127),"sequence must contain printable non-whitespace ASCII")
37 require(plus.startsWith("+")&&(plus.length==1||plus.substring(1)==h.substring(1)),"FASTQ + title must match read title")
38 val scores=if(e=="integers"){
39 val parts=if(q.trim.isEmpty)Array.empty[String]else q.trim.split("\\s+")
40 require(parts.length==seq.length,"quality count must equal sequence length")
41 parts.map{v=>require(v.matches("-?[0-9]{1,3}"),"invalid integer quality");val n=v.toInt;require(n>= -5&&n<=93,"integer quality outside -5..93");n}
42 }else{
43 require(q.length==seq.length,"quality count must equal sequence length")
44 val base=if(e=="sanger33")33 else 64;val low=if(e=="sanger33")33 else 59
45 q.map{c=>require(c>=low&&c<=126,"quality character outside encoding range");c.toInt-base}.toArray
46 }
47 out.write(">");out.write(h.substring(1));out.write("\n")
48 out.write(if(qualityOutput)scores.mkString(" ")else seq);out.write("\n");header=line()
49 };active();out.flush()
50 }
51 }
52 def transform(i:InputStream,o:OutputStream,p:JsonNode):Unit=delegate.transform(i,o,p)
53 def inferOutputs(i:Map[String,StreamDescriptor],p:JsonNode):InferenceResult={encoding(p);delegate.inferOutputs(i,p)}
54 def previewOutputs(i:Map[String,StreamDescriptor],p:JsonNode,l:PreviewLimits):Map[String,OutputSample]={encoding(p);delegate.previewOutputs(i,p,l)}
55 def process(c:OperatorContext):Unit={encoding(c.params);delegate.process(c)}
56}
57object FastqToFasta extends FastqOperator(false)
58object FastqToQual extends FastqOperator(true)