Source code

Download source archive ↓

src/GenomicConversions.scala

1package xstream.galaxy
2import java.io.InputStream
3import java.util.Locale
4import com.fasterxml.jackson.databind.JsonNode
5import scala.jdk.CollectionConverters._
6
7abstract class ExtendedGenomic(op:String) extends ExtendedOperator(op){
8 override protected def format:String=operation match{case "split_paired_reads"=>"fastq";case "pileup_interval"=>"bed";case "vcf_to_pgsnp"=>"pgsnp";case _=>"tsv"}
9 protected def run(input:InputStream,emit:String=>Unit,p:JsonNode):Unit={
10  def n(k:String)=param(p,k).asInt;def row(s:String):Unit=emit(s+"\n")
11  val fastq=scala.collection.mutable.ArrayBuffer.empty[String]
12  var chromosome="";var start=0L;var end=0L;val bases=new java.lang.StringBuilder
13  def flush():Unit=if(bases.length>0){row(s"$chromosome\t${start-1}\t$end\t$bases");bases.setLength(0)}
14  lines(input){line=>
15   active()
16   operation match{
17    case "split_paired_reads"=>fastq+=line;if(fastq.size==4){
18     val h=fastq(0);val s=fastq(1);val plus=fastq(2);val quality=fastq(3)
19     require(h.startsWith("@")&&h.length>1&&plus.startsWith("+")&&(plus.length==1||plus.drop(1)==h.drop(1)),"invalid FASTQ titles")
20     require(s.length==quality.length&&s.length%2==0,"paired sequence/quality lengths must match and be even")
21     require(s.forall(c=>c>32&&c<127)&&quality.forall(c=>c>=33&&c<=126),"invalid sequence or quality characters")
22     val suffix="/"+n("mate");val half=s.length/2;val from=if(n("mate")==1)0 else half
23     row(h+suffix);row(s.substring(from,from+half));row(if(plus.length==1)"+"else plus+suffix);row(quality.substring(from,from+half));fastq.clear()
24    }
25    case "pileup_interval"=>if(line.nonEmpty && !line.startsWith("#")){
26     val f=line.split("\t",-1);def col(k:String):String={val i=n(k)-1;require(i<f.length,"selected pileup column missing");f(i)}
27     val chr=col("sequence_column");val pos=col("position_column").toLong;val base=col("base_column");val coverage=col("coverage_column").toLong
28     require(chr.nonEmpty&&pos>0&&coverage>=0&&base.length==1,"invalid pileup coordinates, base or coverage")
29     if(coverage<n("min_coverage"))flush()else{
30      if(bases.length>0&&(chromosome!=chr||end==Long.MaxValue||pos!=end+1))flush()
31      if(bases.length==0){chromosome=chr;start=pos};end=pos;require(bases.length<8388608,"pileup active interval exceeds budget");bases.append(base)
32     }
33    }
34    case "divide_pg_snp"=>if(line.nonEmpty && !line.startsWith("#")){
35     val f=line.split("\t",-1);require(f.length>=7,"pgSnp requires seven fields");val count=f(4).toInt;require(count>0,"allele count must be positive")
36     if(count<=2){val a=f(3).split("/",-1).toVector;val freq=f(5).split(",",-1).toVector;val score=f(6).split(",",-1).toVector
37      require(a.size==count&&freq.size==count&&score.size==count&&a.forall(_.nonEmpty),"pgSnp allele arrays must match count");freq.foreach(x=>require(x.toLong>=0,"negative allele frequency"));score.foreach(x=>require(java.lang.Double.isFinite(x.toDouble),"invalid allele score"))
38      val ref=n("reference_column");require(ref<=f.length,"reference column missing");val second=if(count==2)Vector(a(1),freq(1),score(1))else Vector(if(ref==0)"N"else f(ref-1),"0","0")
39      row((f.take(3).toVector++Vector(a(0),freq(0),score(0))++second++f.drop(7)).mkString("\t"))
40     }
41    }
42    case "vcf_to_pgsnp"=>if(line.nonEmpty && !line.startsWith("#")){
43     val f=line.split("\t",-1);require(f.length>=10,"VCF requires sample columns");val pos=f(1).toLong;require(pos>0,"VCF position must be positive")
44     val alleles=(Vector(f(3))++f(4).split(",",-1)).map(_.toUpperCase(Locale.ROOT))
45     if(alleles.forall(a=>a.length==1&&"ACGT".contains(a.head))&&(f(6)=="PASS"||f(6)==".")){
46      val gt=f(8).split(":",-1).indexOf("GT");require(gt>=0,"VCF FORMAT lacks GT")
47      val selected=n("sample_column");require(selected<=f.length,"selected sample column missing");val samples=if(selected==0)f.drop(9).toVector else Vector(f(selected-1));val counts=Array.fill[Long](alleles.size)(0)
48      samples.foreach{s=>active();val parts=s.split(":",-1);if(s!="."){require(gt<parts.length,"sample lacks GT value");parts(gt).split("[/|]",-1).foreach{a=>if(a!="."){require(a.matches("[0-9]+"),"invalid genotype allele");val k=a.toInt;require(k>=0&&k<counts.length,"genotype allele index outside ALT");counts(k)=Math.addExact(counts(k),1L)}}}}
49      val called=counts.indices.filter(counts(_)>0);if(called.nonEmpty){val chr=if(param(p,"prefix_chr").asBoolean && !f(0).startsWith("chr"))"chr"+f(0)else f(0);row(Vector(chr,(pos-1).toString,pos.toString,called.map(alleles(_)).mkString("/"),called.size.toString,called.map(counts(_)).mkString(","),called.map(_=>"0").mkString(",")).mkString("\t"))}
50     }
51    }
52    case "motif_flank_counts"=>if(line.nonEmpty){
53     val f=line.split("\t",-1);require(f.length%2==1&&f.length>=3&&f(f.length/2)=="indel","expected odd window count and central indel")
54     val center=f.length/2;val windows=n("windows");require(windows<=center,"not enough flanking windows")
55     val motifs=param(p,"motifs").elements.asScala.map(_.asText).toVector;require(line.length.toLong*motifs.size<=64000000,"motif scan budget exceeded")
56     motifs.foreach{s=>active();val parts=s.split("=",-1);val motif=parts.last.toUpperCase(Locale.ROOT);val name=parts.head
57      def count(range:Range)=range.count(i=>f(i).toUpperCase(Locale.ROOT).contains(motif))
58      row(s"$name\t${count(center-windows until center)}\t${count(center+1 to center+windows)}")
59     }
60    }
61   }
62  }
63  if(operation=="split_paired_reads")require(fastq.isEmpty,"truncated four-line FASTQ record")
64  if(operation=="pileup_interval")flush()
65 }
66}