Commit 595baf7d authored by Ruben Vorderman's avatar Ruben Vorderman
Browse files

created plotconstructors in bammetrics

parent d0d60c78
...@@ -17,7 +17,12 @@ package nl.lumc.sasc.biopet.pipelines.bammetrics ...@@ -17,7 +17,12 @@ package nl.lumc.sasc.biopet.pipelines.bammetrics
import java.io.{File, PrintWriter} import java.io.{File, PrintWriter}
import nl.lumc.sasc.biopet.utils.config.Configurable 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.ConfigUtils
import nl.lumc.sasc.biopet.utils.rscript.{LinePlot, StackedBarPlot} 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
...@@ -158,7 +163,7 @@ object BammetricsReport extends ReportBuilder { ...@@ -158,7 +163,7 @@ object BammetricsReport extends ReportBuilder {
* * @param libraryLevel Default false, when set true plot will be based on library stats instead of sample stats * * @param libraryLevel Default false, when set true plot will be based on library stats instead of sample stats
*/ */
def alignmentSummaryPlotLines(summary: SummaryDb, def alignmentSummaryPlotLines(summary: SummaryDb,
sampleId: Option[Int] = None, sampleId: Option[Int] = None,
libraryLevel: Boolean = false): Seq[String] = { libraryLevel: Boolean = false): Seq[String] = {
val statsPaths = Map( val statsPaths = Map(
"Mapped" -> List("flagstats", "Mapped"), "Mapped" -> List("flagstats", "Mapped"),
...@@ -170,18 +175,18 @@ object BammetricsReport extends ReportBuilder { ...@@ -170,18 +175,18 @@ object BammetricsReport extends ReportBuilder {
val results: Map[(Int, Option[Int]), Map[String, Option[Any]]] = if (libraryLevel) { val results: Map[(Int, Option[Int]), Map[String, Option[Any]]] = if (libraryLevel) {
summary summary
.getStatsForLibraries(runId, .getStatsForLibraries(runId,
"bammetrics", "bammetrics",
"bamstats", "bamstats",
sampleId = sampleId, sampleId = sampleId,
keyValues = statsPaths) keyValues = statsPaths)
.map(x => (x._1._1, Some(x._1._2)) -> x._2) .map(x => (x._1._1, Some(x._1._2)) -> x._2)
} else } else
summary summary
.getStatsForSamples(runId, .getStatsForSamples(runId,
"bammetrics", "bammetrics",
"bamstats", "bamstats",
sample = sampleId.map(SampleId), sample = sampleId.map(SampleId),
keyValues = statsPaths) keyValues = statsPaths)
.map(x => (x._1, None) -> x._2) .map(x => (x._1, None) -> x._2)
val summaryPlotLines = ArrayBuffer[String]() val summaryPlotLines = ArrayBuffer[String]()
...@@ -216,8 +221,7 @@ object BammetricsReport extends ReportBuilder { ...@@ -216,8 +221,7 @@ object BammetricsReport extends ReportBuilder {
def alignmentSummaryPlot(outputDir: File, def alignmentSummaryPlot(outputDir: File,
prefix: String, prefix: String,
summaryPlotLines: Seq[String], summaryPlotLines: Seq[String],
libraryLevel: Boolean = false libraryLevel: Boolean = false): Unit = {
): Unit = {
val tsvFile = new File(outputDir, prefix + ".tsv") val tsvFile = new File(outputDir, prefix + ".tsv")
val pngFile = new File(outputDir, prefix + ".png") val pngFile = new File(outputDir, prefix + ".png")
val tsvWriter = new PrintWriter(tsvFile) val tsvWriter = new PrintWriter(tsvFile)
...@@ -237,6 +241,7 @@ object BammetricsReport extends ReportBuilder { ...@@ -237,6 +241,7 @@ object BammetricsReport extends ReportBuilder {
plot.title = Some("Aligned_reads") plot.title = Some("Aligned_reads")
plot.runLocal() plot.runLocal()
} }
/** /**
* This is a generic method to create plots * This is a generic method to create plots
* @param libraryLevel If enabled the plots will show data per library * @param libraryLevel If enabled the plots will show data per library
...@@ -248,16 +253,15 @@ object BammetricsReport extends ReportBuilder { ...@@ -248,16 +253,15 @@ object BammetricsReport extends ReportBuilder {
* @param pipeline Query for the pipeline * @param pipeline Query for the pipeline
* @param module Query for the module * @param module Query for the module
*/ */
def summaryForPlot( summary: SummaryDb, def summaryForPlot(summary: SummaryDb,
statsPaths: Map[String, List[String]], statsPaths: Map[String, List[String]],
yKeyList: List[String], yKeyList: List[String],
xKeyList: List[String], xKeyList: List[String],
pipeline: PipelineQuery, pipeline: PipelineQuery,
module: ModuleQuery, module: ModuleQuery,
libraryLevel: Boolean = false, libraryLevel: Boolean = false,
sampleId: Option[Int] = None, sampleId: Option[Int] = None,
libraryId: Option[Int] = None libraryId: Option[Int] = None): Array[Map[String, Array[Any]]] = {
): Array[Map[String, Array[Any]]]= {
val results: Map[(Int, Option[Int]), Map[String, Option[Array[Any]]]] = if (libraryLevel) { val results: Map[(Int, Option[Int]), Map[String, Option[Array[Any]]]] = if (libraryLevel) {
summary summary
.getStatsForLibraries(runId, pipeline, module, sampleId = sampleId, keyValues = statsPaths) .getStatsForLibraries(runId, pipeline, module, sampleId = sampleId, keyValues = statsPaths)
...@@ -267,10 +271,10 @@ object BammetricsReport extends ReportBuilder { ...@@ -267,10 +271,10 @@ object BammetricsReport extends ReportBuilder {
} else } else
summary summary
.getStatsForSamples(runId, .getStatsForSamples(runId,
pipeline, pipeline,
module, module,
sample = sampleId.map(SampleId), sample = sampleId.map(SampleId),
keyValues = statsPaths) keyValues = statsPaths)
.map(x => (x._1, None) -> x._2.map(x => x._1 -> x._2.map(ConfigUtils.any2list(_).toArray))) .map(x => (x._1, None) -> x._2.map(x => x._1 -> x._2.map(ConfigUtils.any2list(_).toArray)))
val tables: Array[Map[String, Array[Any]]] = results.map { val tables: Array[Map[String, Array[Any]]] = results.map {
case ((sample, library), map) => case ((sample, library), map) =>
...@@ -303,7 +307,6 @@ object BammetricsReport extends ReportBuilder { ...@@ -303,7 +307,6 @@ object BammetricsReport extends ReportBuilder {
* @param title Title of the plot * @param title Title of the plot
* @param removeZero * @param removeZero
*/ */
def writePlotFromSummary(outputDir: File, def writePlotFromSummary(outputDir: File,
prefix: String, prefix: String,
tables: Array[Map[String, Array[Any]]], tables: Array[Map[String, Array[Any]]],
...@@ -318,15 +321,23 @@ object BammetricsReport extends ReportBuilder { ...@@ -318,15 +321,23 @@ object BammetricsReport extends ReportBuilder {
writeTableToTsv(tsvFile, mergeTables(tables, yKeyList.head), yKeyList.head) writeTableToTsv(tsvFile, mergeTables(tables, yKeyList.head), yKeyList.head)
LinePlot(tsvFile, LinePlot(
pngFile, tsvFile,
xlabel = xlabel, pngFile,
ylabel = ylabel, xlabel = xlabel,
title = title, ylabel = ylabel,
hideLegend = tables.size > 40, /* changed from results.size. Original results in summaryForPlot*/ title = title,
removeZero = removeZero).runLocal() hideLegend = tables.size > 40,
/* changed from results.size. Original results in summaryForPlot*/
removeZero = removeZero
).runLocal()
} }
def insertSizePlotTables(summary: SummaryDb,
libraryLevel: Boolean = false,
sampleId: Option[Int] = None,
libraryId: Option[Int] = None): Array[Map[String, Array[Any]]] = {}
/** /**
* Generate a line plot for insertsize * Generate a line plot for insertsize
* *
...@@ -338,10 +349,7 @@ object BammetricsReport extends ReportBuilder { ...@@ -338,10 +349,7 @@ object BammetricsReport extends ReportBuilder {
*/ */
def insertSizePlot(outputDir: File, def insertSizePlot(outputDir: File,
prefix: String, prefix: String,
tables: Array[Map[String, Array[Any]]], insertSizePlotTables: Array[Map[String, Array[Any]]]): Unit = {
libraryLevel: Boolean = false,
sampleId: Option[Int] = None,
libraryId: Option[Int] = None): Unit = {
val statsPaths = Map( val statsPaths = Map(
"insert_size" -> List("histogram", "insert_size"), "insert_size" -> List("histogram", "insert_size"),
"count" -> List("histogram", "All_Reads.fr_count") "count" -> List("histogram", "All_Reads.fr_count")
...@@ -350,7 +358,7 @@ object BammetricsReport extends ReportBuilder { ...@@ -350,7 +358,7 @@ object BammetricsReport extends ReportBuilder {
writePlotFromSummary( writePlotFromSummary(
outputDir, outputDir,
prefix, prefix,
tables, insertSizePlotTables,
libraryLevel, libraryLevel,
sampleId, sampleId,
libraryId, libraryId,
...@@ -365,41 +373,45 @@ object BammetricsReport extends ReportBuilder { ...@@ -365,41 +373,45 @@ object BammetricsReport extends ReportBuilder {
) )
} }
def mappingQualityPlotSummary(summary: SummaryDb, def mappingQualityPlotTables(summary: SummaryDb,
): Unit = { libraryLevel: Boolean = false,
sampleId: Option[Int] = None,
libraryId: Option[Int] = None): Array[Map[String, Array[Any]]] = {
val statsPaths = Map( val statsPaths = Map(
"mapping_quality" -> List("mapping_quality", "histogram", "values"), "mapping_quality" -> List("mapping_quality", "histogram", "values"),
"count" -> List("mapping_quality", "histogram", "counts") "count" -> List("mapping_quality", "histogram", "counts")
) )
val plotSummary: Array[Map[String, Array[Any]]] = summaryForPlot(summary, statsPaths, val plotTables: Array[Map[String, Array[Any]]] = summaryForPlot(summary,
"mapping_quality" :: Nil, statsPaths,
"count" :: Nil, "mapping_quality" :: Nil,
"bammetrics", "count" :: Nil,
"bamstats" "bammetrics",
) "bamstats",
plotSummary libraryLevel,
sampleId,
libraryId)
plotTables
} }
def mappingQualityPlot(outputDir: File, def mappingQualityPlot(outputDir: File,
prefix: String, prefix: String,
mappingQualityPlotSummary: Array[Map[String, Array[Any]]], mappingQualityPlotTables: Array[Map[String, Array[Any]]]): Unit = {
libraryLevel: Boolean = false, writePlotFromSummary(outputDir,
sampleId: Option[Int] = None, prefix,
libraryId: Option[Int] = None): Unit = { mappingQualityPlotTables,
val statsPaths = Map( "mapping_quality" :: Nil,
"mapping_quality" -> List("mapping_quality", "histogram", "values"), "count" :: Nil,
"count" -> List("mapping_quality", "histogram", "counts") "Mapping Quality",
) "Reads",
writePlotFromSummary(outputDir, "Mapping Quality")
prefix, }
mappingQualityPlotSummary, def clippingPlotTables(summary: SummaryDb,
"mapping_quality" :: Nil, libraryLevel: Boolean = false,
"count" :: Nil,"Mapping Quality", "Reads","Mapping Quality") sampleId: Option[Int] = None,
} libraryId: Option[Int] = None): Array[Map[String, Array[Any]]] = {}
def clippingPlot(outputDir: File, def clippingPlot(outputDir: File,
prefix: String, prefix: String,
tables: Array[Map[String, Array[Any]]], clippingPlotTables: Array[Map[String, Array[Any]]],
libraryLevel: Boolean = false, libraryLevel: Boolean = false,
sampleId: Option[Int] = None, sampleId: Option[Int] = None,
libraryId: Option[Int] = None): Unit = { libraryId: Option[Int] = None): Unit = {
...@@ -537,29 +549,30 @@ object BamMetricsAlignmentReport { ...@@ -537,29 +549,30 @@ object BamMetricsAlignmentReport {
sampleLevel: Boolean = false, sampleLevel: Boolean = false,
showPlot: Boolean = false, showPlot: Boolean = false,
showIntro: Boolean = true, showIntro: Boolean = true,
showTable: Boolean = true): Map[String,Any] = { showTable: Boolean = true): Map[String, Any] = {
val statsPaths = Map( val statsPaths = Map(
"All" -> List("flagstats", "All"), "All" -> List("flagstats", "All"),
"Mapped" -> List("flagstats", "Mapped"), "Mapped" -> List("flagstats", "Mapped"),
"Duplicates" -> List("flagstats", "Duplicates"), "Duplicates" -> List("flagstats", "Duplicates"),
"NotPrimaryAlignment" -> List("flagstats", "NotPrimaryAlignment") "NotPrimaryAlignment" -> List("flagstats", "NotPrimaryAlignment")
) )
val alignmentSummaryResults = summary.getStatsForLibraries(runId,"bammetrics","bamstats", sampleId, statsPaths) val alignmentSummaryResults =
summary.getStatsForLibraries(runId, "bammetrics", "bamstats", sampleId, statsPaths)
val alignmentSummaryPlotLines: Option[Seq[String]] = val alignmentSummaryPlotLines: Option[Seq[String]] =
if (showPlot) if (showPlot)
Some(BammetricsReport.alignmentSummaryPlotLines(summary,sampleId,!sampleLevel)) Some(BammetricsReport.alignmentSummaryPlotLines(summary, sampleId, !sampleLevel))
else None else None
Map( Map(
"alignmentSummaryResults" -> alignmentSummaryResults, "alignmentSummaryResults" -> alignmentSummaryResults,
"alignmentSummaryPlotLines" -> alignmentSummaryPlotLines, "alignmentSummaryPlotLines" -> alignmentSummaryPlotLines,
"sampleLevel" -> sampleLevel, "sampleLevel" -> sampleLevel,
"showPlot" -> showPlot, "showPlot" -> showPlot,
"showIntro" -> showIntro, "showIntro" -> showIntro,
"showTable" -> showTable, "showTable" -> showTable,
"sampleId" -> sampleId, "sampleId" -> sampleId,
"libId" -> libId "libId" -> libId
) )
} }
} }
object BamMetricsMappingQuality { object BamMetricsMappingQuality {
...@@ -569,15 +582,14 @@ object BamMetricsMappingQuality { ...@@ -569,15 +582,14 @@ object BamMetricsMappingQuality {
allLibraries: Seq[Library], allLibraries: Seq[Library],
sampleId: Option[Int], sampleId: Option[Int],
libId: Option[Int], libId: Option[Int],
sampleLevel: Boolean= false, sampleLevel: Boolean = false,
showPlot: Boolean = false, showPlot: Boolean = false,
showIntro: Boolean = true, showIntro: Boolean = true,
showTable: Boolean = true): Map[String,Any] = { showTable: Boolean = true): Map[String, Any] = {
val samples = sampleId match { val samples = sampleId match {
case Some(id) => allSamples.filter(_.id == id).toList case Some(id) => allSamples.filter(_.id == id).toList
case _ => allSamples.toList case _ => allSamples.toList
} }
val
} }
} }
\ No newline at end of file
...@@ -14,7 +14,12 @@ ...@@ -14,7 +14,12 @@
*/ */
package nl.lumc.sasc.biopet.pipelines.mapping package nl.lumc.sasc.biopet.pipelines.mapping
import nl.lumc.sasc.biopet.core.report.{MultisampleReportBuilder, ReportBuilderExtension, ReportPage, ReportSection} import nl.lumc.sasc.biopet.core.report.{
MultisampleReportBuilder,
ReportBuilderExtension,
ReportPage,
ReportSection
}
import nl.lumc.sasc.biopet.pipelines.bammetrics.{BamMetricsAlignmentReport, BammetricsReport} import nl.lumc.sasc.biopet.pipelines.bammetrics.{BamMetricsAlignmentReport, BammetricsReport}
import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport import nl.lumc.sasc.biopet.pipelines.flexiprep.FlexiprepReport
import nl.lumc.sasc.biopet.pipelines.gears.GearsKronaPlot import nl.lumc.sasc.biopet.pipelines.gears.GearsKronaPlot
...@@ -60,17 +65,17 @@ trait MultisampleMappingReportTrait extends MultisampleReportBuilder { ...@@ -60,17 +65,17 @@ trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
def indexPage: Future[ReportPage] = { def indexPage: Future[ReportPage] = {
val krakenExecuted = Await.result(summary.getStatsSize(runId, val krakenExecuted = Await.result(summary.getStatsSize(runId,
"gearskraken", "gearskraken",
"krakenreport", "krakenreport",
library = NoLibrary, library = NoLibrary,
mustHaveSample = true), mustHaveSample = true),
Duration.Inf) >= 1 Duration.Inf) >= 1
val centrifugeExecuted = Await.result(summary.getStatsSize(runId, val centrifugeExecuted = Await.result(summary.getStatsSize(runId,
"gearscentrifuge", "gearscentrifuge",
"centrifuge_report", "centrifuge_report",
library = NoLibrary, library = NoLibrary,
mustHaveSample = true), mustHaveSample = true),
Duration.Inf) >= 1 Duration.Inf) >= 1
val wgsExecuted = Await.result( val wgsExecuted = Await.result(
summary.getStatsSize(runId, "bammetrics", "wgs", library = NoLibrary, mustHaveSample = true), summary.getStatsSize(runId, "bammetrics", "wgs", library = NoLibrary, mustHaveSample = true),
Duration.Inf) >= 1 Duration.Inf) >= 1
...@@ -79,60 +84,91 @@ trait MultisampleMappingReportTrait extends MultisampleReportBuilder { ...@@ -79,60 +84,91 @@ trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
Duration.Inf) >= 1 Duration.Inf) >= 1
val insertsizeExecuted = summary val insertsizeExecuted = summary
.getStatsForSamples(runId, .getStatsForSamples(runId,
"bammetrics", "bammetrics",
"CollectInsertSizeMetrics", "CollectInsertSizeMetrics",
keyValues = Map("metrics" -> List("metrics"))) keyValues = Map("metrics" -> List("metrics")))
.exists(_._2("metrics").isDefined) .exists(_._2("metrics").isDefined)
val mappingExecuted = Await.result( val mappingExecuted = Await.result(
summary.getStatsSize(runId, "mapping", NoModule, mustHaveLibrary = true), summary.getStatsSize(runId, "mapping", NoModule, mustHaveLibrary = true),
Duration.Inf) >= 1 Duration.Inf) >= 1
val mappingSettings = summary.getSettingsForLibraries(runId, val mappingSettings = summary.getSettingsForLibraries(runId,
"mapping", "mapping",
NoModule, NoModule,
keyValues = keyValues =
Map("paired" -> List("paired"))) Map("paired" -> List("paired")))
val pairedFound = !mappingExecuted || mappingSettings.exists(_._2.exists(_._2 == Option(true))) val pairedFound = !mappingExecuted || mappingSettings.exists(_._2.exists(_._2 == Option(true)))
val flexiprepExecuted = Await val flexiprepExecuted = Await
.result(summary.getStatsSize(runId, "flexiprep", mustHaveLibrary = true), Duration.Inf) >= 1 .result(summary.getStatsSize(runId, "flexiprep", mustHaveLibrary = true), Duration.Inf) >= 1
val krakenDustbinAnalysis = GearsKronaPlot.values(summary, runId, "gearskraken", "krakenreport", samples, libraries, sampleId, libId ) val krakenDustbinAnalysis = GearsKronaPlot.values(summary,
val centrifugeAnalysis = GearsKronaPlot.values(summary, runId, "gearscentrifuge", "centrifuge_report", samples, libraries, sampleId, libId ) runId,
val centrifugeUniqueAnalysis = GearsKronaPlot.values(summary, runId, "gearscentrifuge", "centrifuge_unique_report", samples, libraries, sampleId, libId ) "gearskraken",
val alignmentSummary = BamMetricsAlignmentReport.values(summary, runId, samples, libraries, sampleId, libId, sampleLevel =true,showPlot=true,showTable=false) "krakenreport",
val mappingQualitySummary= BammetricsReport.mappingQualityPlotSummary(summary) samples,
libraries,
sampleId,
libId)
val centrifugeAnalysis = GearsKronaPlot.values(summary,
runId,
"gearscentrifuge",
"centrifuge_report",
samples,
libraries,
sampleId,
libId)
val centrifugeUniqueAnalysis = GearsKronaPlot.values(summary,
runId,
"gearscentrifuge",
"centrifuge_unique_report",
samples,
libraries,
sampleId,
libId)
val alignmentSummary = BamMetricsAlignmentReport.values(summary,
runId,
samples,
libraries,
sampleId,
libId,
sampleLevel = true,
showPlot = true,
showTable = false)
val mappingQualitySummary = BammetricsReport.mappingQualityPlotTables(summary)
Future { Future {
ReportPage( ReportPage(
List("Samples" -> generateSamplesPage(pageArgs)) ++ List("Samples" -> generateSamplesPage(pageArgs)) ++
(if (krakenExecuted) (if (krakenExecuted)
List( List(
"Dustbin analysis - Kraken" -> Future.successful( "Dustbin analysis - Kraken" -> Future.successful(
ReportPage(List(), ReportPage(List(),
List("Krona plot" -> ReportSection( List("Krona plot" -> ReportSection(
"/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",krakenDustbinAnalysis)), "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
Map()))) krakenDustbinAnalysis)),
else Nil) ++ (if (centrifugeExecuted) Map())))
List( else Nil) ++ (if (centrifugeExecuted)
"Centrifuge analysis" -> Future.successful(ReportPage( List(
List("Non-unique" -> Future.successful( "Centrifuge analysis" -> Future.successful(ReportPage(
ReportPage(List(), List(
List("All mappings" -> ReportSection( "Non-unique" -> Future.successful(ReportPage(
"/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp", List(),
centrifugeAnalysis)), List("All mappings" -> ReportSection(
Map()))), "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
List("Unique mappings" -> ReportSection( centrifugeAnalysis)),
"/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp", Map()))),
centrifugeUniqueAnalysis)), List("Unique mappings" -> ReportSection(
Map("summaryModuleTag" -> "gearscentrifuge", "/nl/lumc/sasc/biopet/pipelines/gears/krakenKrona.ssp",
"centrifugeTag" -> Some("centrifuge")) centrifugeUniqueAnalysis)),
))) Map("summaryModuleTag" -> "gearscentrifuge",
else Nil) ++ "centrifugeTag" -> Some("centrifuge"))
)))
else Nil) ++
List( List(
"Reference" -> Future.successful( "Reference" -> Future.successful(
ReportPage( ReportPage(