Commit f4614adf authored by rhpvorderman's avatar rhpvorderman

Defutured alignmentSummary.ssp

parent d9383457
#import(nl.lumc.sasc.biopet.utils.summary.db.Schema._)
#import(nl.lumc.sasc.biopet.utils.summary.db.Schema.Sample)
#import(nl.lumc.sasc.biopet.utils.summary.db.Schema.Library)
#import(nl.lumc.sasc.biopet.utils.summary.db.SummaryDb)
#import(nl.lumc.sasc.biopet.utils.summary.db.SummaryDb._)
#import(nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._)
......@@ -19,6 +20,8 @@
<%@ var runId: Int %>
<%@ var allSamples: Seq[Sample] %>
<%@ var allLibraries: Seq[Library] %>
<%@ var alignmentSummaryPlotLines: Seq[String] %>
<%@ var alignmentSummaryResults: Map[(Int, Int), Map[String, Option[Any]]] %>
#{
val samples = sampleId match {
case Some(id) => allSamples.filter(_.id == id).toList
......@@ -45,7 +48,7 @@
</div>
#end
#if (showPlot)
#{ BammetricsReport.alignmentSummaryPlot(outputDir, "alignmentSummary", summary, !sampleLevel, sampleId = sampleId) }#
#{ BammetricsReport.alignmentSummaryPlot(outputDir, "alignmentSummary", alignmentSummaryPlotLines, !sampleLevel) }#
<div class="panel-body">
<img src="alignmentSummary.png" class="img-responsive" />
......@@ -95,11 +98,10 @@
"Duplicates" -> List("flagstats", "Duplicates"),
"NotPrimaryAlignment" -> List("flagstats", "NotPrimaryAlignment")
)
val results = summary.getStatKeys(runId, "bammetrics", "bamstats", sample = sample.id, library = libId.map(LibraryId).getOrElse(NoLibrary), keyValues = statsPaths)
val total = results("All").getOrElse(0L).asInstanceOf[Long]
val mapped = results("Mapped").getOrElse(0L).asInstanceOf[Long]
val duplicates = results("Duplicates").getOrElse(0L).asInstanceOf[Long]
val secondary = results("NotPrimaryAlignment").getOrElse(0L).asInstanceOf[Long]
val total = alignmentSummaryResults(sample.id,libId)("All").getOrElse(0L).asInstanceOf[Long]
val mapped = alignmentSummaryResults(sample.id, libId)("Mapped").getOrElse(0L).asInstanceOf[Long]
val duplicates = alignmentSummaryResults(sample.id,libId)("Duplicates").getOrElse(0L).asInstanceOf[Long]
val secondary = alignmentSummaryResults(sample.id,libId)("NotPrimaryAlignment").getOrElse(0L).asInstanceOf[Long]
}#
<td>${total}</td>
<td>${mapped}</td>
......
......@@ -17,18 +17,14 @@ package nl.lumc.sasc.biopet.pipelines.bammetrics
import java.io.{File, PrintWriter}
import nl.lumc.sasc.biopet.utils.config.Configurable
import nl.lumc.sasc.biopet.core.report.{
ReportBuilder,
ReportBuilderExtension,
ReportPage,
ReportSection
}
import nl.lumc.sasc.biopet.core.report.{ReportBuilder, ReportBuilderExtension, ReportPage, ReportSection}
import nl.lumc.sasc.biopet.utils.ConfigUtils
import nl.lumc.sasc.biopet.utils.rscript.{LinePlot, StackedBarPlot}
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.Implicts._
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb._
import nl.lumc.sasc.biopet.utils.summary.db.Schema._
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
......@@ -154,25 +150,15 @@ object BammetricsReport extends ReportBuilder {
}
/**
* Generate a stackbar plot for alignment stats
* Generates the lines for alignmentSummaryPlot
*
* @param outputDir OutputDir for the tsv and png file
* @param prefix Prefix of the tsv and png file
* @param summary Summary class
* @param libraryLevel Default false, when set true plot will be based on library stats instead of sample stats
* @param sampleId Default it selects all sampples, when sample is giving it limits to selected sample
* * @param libraryLevel Default false, when set true plot will be based on library stats instead of sample stats
*/
def alignmentSummaryPlot(outputDir: File,
prefix: String,
summary: SummaryDb,
libraryLevel: Boolean = false,
sampleId: Option[Int] = None): Unit = {
val tsvFile = new File(outputDir, prefix + ".tsv")
val pngFile = new File(outputDir, prefix + ".png")
val tsvWriter = new PrintWriter(tsvFile)
if (libraryLevel) tsvWriter.print("Library") else tsvWriter.print("Sample")
tsvWriter.println("\tMapped\tDuplicates\tUnmapped\tSecondary")
def alignmentSummaryPlotLines(summary: SummaryDb,
sampleId: Option[Int] = None,
libraryLevel: Boolean = false): Seq[String] = {
val statsPaths = Map(
"Mapped" -> List("flagstats", "Mapped"),
"Duplicates" -> List("flagstats", "Duplicates"),
......@@ -196,6 +182,7 @@ object BammetricsReport extends ReportBuilder {
sample = sampleId.map(SampleId),
keyValues = statsPaths)
.map(x => (x._1, None) -> x._2)
val summaryPlotLines = ArrayBuffer[String]()
for (((s, l), result) <- results) {
val sampleName: String = summary.getSampleName(s).map(_.get)
......@@ -212,16 +199,40 @@ object BammetricsReport extends ReportBuilder {
sb.append(duplicates + "\t")
sb.append((total - mapped) + "\t")
sb.append(secondary)
tsvWriter.println(sb.toString)
summaryPlotLines += sb.toString
}
summaryPlotLines
}
/**
* Generate a stackbar plot for alignment stats
*
* @param outputDir OutputDir for the tsv and png file
* @param prefix Prefix of the tsv and png file
* @param summaryPlotLines A sequence of strings written to the summary tsv
* @param libraryLevel Default false, when set true plot will be based on library stats instead of sample stats
*/
def alignmentSummaryPlot(outputDir: File,
prefix: String,
summaryPlotLines: Seq[String],
libraryLevel: Boolean = false
): Unit = {
val tsvFile = new File(outputDir, prefix + ".tsv")
val pngFile = new File(outputDir, prefix + ".png")
val tsvWriter = new PrintWriter(tsvFile)
if (libraryLevel) tsvWriter.print("Library") else tsvWriter.print("Sample")
tsvWriter.println("\tMapped\tDuplicates\tUnmapped\tSecondary")
for (line <- summaryPlotLines) {
tsvWriter.println(line)
}
tsvWriter.close()
val plot = new StackedBarPlot(null)
plot.input = tsvFile
plot.output = pngFile
plot.ylabel = Some("Reads")
plot.width = Some(200 + (results.size * 10))
plot.width = Some(200 + (summaryPlotLines.size * 10))
plot.title = Some("Aligned_reads")
plot.runLocal()
}
......@@ -501,3 +512,27 @@ object BammetricsReport extends ReportBuilder {
writer.close()
}
}
object BamMetricsAlignmentSummary {
def values(summary: SummaryDb,
runId: Int,
allSamples: Seq[Sample],
allLibraries: Seq[Library],
sampleId: Option[Int] = None,
libId: Option[Int],
sampleLevel: Boolean = false): Map[String,Any] = {
val statsPaths = Map(
"All" -> List("flagstats", "All"),
"Mapped" -> List("flagstats", "Mapped"),
"Duplicates" -> List("flagstats", "Duplicates"),
"NotPrimaryAlignment" -> List("flagstats", "NotPrimaryAlignment")
)
val alignmentSummaryResults = summary.getStatsForLibraries(runId,"bammetrics","bamstats", sampleId, statsPaths)
val alignmentSummaryPlotLines = BammetricsReport.alignmentSummaryPlotLines(summary,sampleId,!sampleLevel)
Map(
"alignmentSummaryResults" -> alignmentSummaryResults,
"alignmentSummaryPlotLines" -> alignmentSummaryPlotLines
)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment