Commit dd6189e2 authored by Peter van 't Hof's avatar Peter van 't Hof

Added rna metrics plot

parent b9f23956
#import(nl.lumc.sasc.biopet.utils.summary.Summary)
#import(nl.lumc.sasc.biopet.core.report.ReportPage)
#import(nl.lumc.sasc.biopet.pipelines.bammetrics.BammetricsReport)
#import(java.io.File)
#import(org.apache.commons.io.FileUtils)
<%@ var summary: Summary %>
<%@ var sampleId: Option[String] = None %>
<%@ var libId: Option[String] = None %>
<%@ var rootPath: String %>
<%@ var metricsTag: String = "bammetrics" %>
<%@ var sampleLevel: Boolean = false %>
<%@ var outputDir: File %>
<%@ var fields: List[String] = List()%>
<%@ var showPlot: Boolean = false %>
<%@ var showTable: Boolean = true %>
<%@ var showIntro: Boolean = true%>
#{
val samples = sampleId match {
case Some(sample) => {
List(sample.toString)
}
case _ => summary.samples.toList
}
}#
#if (showIntro)
<br/>
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-6">
<p>
Here we show the total number of positions in the reference that are covered with a given coverage. This plot is whole-genome based, and will therefore be highly skewed in the case of an exome or targeted approach.
</p>
</div>
</div>
#end
#if (showPlot)
#{ BammetricsReport.rnaHistogramPlot(outputDir, "rna", summary, !sampleLevel, sampleId = sampleId, libId = libId) }#
<div class="panel-body">
<img src="rna.png" class="img-responsive" />
</div>
<div class="panel-footer">
#if (showTable)
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#rnaTable">Hide table</button>
#else
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#rnaTable">Show table</button>
#end
<i class="glyphicon glyphicon-file"></i> <a href="rna.tsv">tsv file</a>
</div>
#end
<div class="panel-body collapse #if (showTable)in#end" id="rnaTable">
<!-- Table -->
<table class="table sortable-theme-bootstrap" data-sortable>
<thead><tr>
<th data-sorted="true" data-sorted-direction="ascending">Sample</th>
#if (!sampleLevel) <th>Library</th> #end
#for (field <- fields)
<th>${field.replaceAll("_", " ")}</th>
#end
</tr></thead>
<tbody>
#for (sample <- samples.toList.sorted)
#{
val libs = (libId, sampleLevel) match {
case (_, true) => List("")
case (Some(libId), _) => List(libId.toString)
case _ => summary.libraries(sample).toList
}
}#
<tr><td rowspan="${libs.size}"><a href="${rootPath}Samples/${sample}/index.html">${sample}</a></td>
#for (libId <- libs)
#if (libs.head != libId) <tr> #end
#if (!sampleLevel) <td><a href="${rootPath}Samples/${sample}/Libraries/${libId}/index.html">${libId}</a></td> #end
#{
val prefixPath = List("samples", sample) ::: (if (libId.isEmpty) Nil else List("libraries", libId)) ::: List("bammetrics", "stats")
val fieldValues = for (field <- fields) yield {
summary.getValue((prefixPath ::: List("rna", "metrics", field.toUpperCase)):_*).getOrElse(prefixPath ::: metricsTag :: Nil)
}
}#
#for (value <- fieldValues)
<td>${value}</td>
#end
</tr>
#end
#end
</tbody>
</table>
</div>
......@@ -321,4 +321,94 @@ object BammetricsReport extends ReportBuilder {
plot.title = Some("Whole genome coverage")
plot.runLocal()
}
/**
* Generate a line plot for rna coverage
* @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
*/
def rnaHistogramPlot(outputDir: File,
prefix: String,
summary: Summary,
libraryLevel: Boolean = false,
sampleId: Option[String] = None,
libId: Option[String] = None): Unit = {
val tsvFile = new File(outputDir, prefix + ".tsv")
val pngFile = new File(outputDir, prefix + ".png")
val tsvWriter = new PrintWriter(tsvFile)
if (libraryLevel) {
tsvWriter.println((for (
sample <- summary.samples if sampleId.isEmpty || sampleId.get == sample;
lib <- summary.libraries(sample) if libId.isEmpty || libId.get == lib
) yield s"$sample-$lib")
.mkString("library\t", "\t", ""))
} else {
sampleId match {
case Some(sample) => tsvWriter.println("\t" + sample)
case _ => tsvWriter.println(summary.samples.mkString("Sample\t", "\t", ""))
}
}
var map: Map[Int, Map[String, Double]] = Map()
def fill(sample: String, lib: Option[String]): Unit = {
val insertSize = new SummaryValue(List("bammetrics", "stats", "rna", "histogram", "normalized_position"),
summary, Some(sample), lib).value.getOrElse(List())
val counts = new SummaryValue(List("bammetrics", "stats", "rna", "histogram", "All_Reads.normalized_coverage"),
summary, Some(sample), lib).value.getOrElse(List())
(insertSize, counts) match {
case (l: List[_], l2: List[_]) =>
l.zip(l2).foreach(i => {
val insertSize = i._1.toString.toInt
val count = i._2.toString.toDouble
val old = map.getOrElse(insertSize, Map())
if (libraryLevel) map += insertSize -> (old + ((s"$sample-" + lib.get) -> count))
else map += insertSize -> (old + (sample -> count))
})
case _ => throw new IllegalStateException("Must be a list")
}
}
if (libraryLevel) {
for (
sample <- summary.samples if sampleId.isEmpty || sampleId.get == sample;
lib <- summary.libraries(sample) if libId.isEmpty || libId.get == lib
) fill(sample, Some(lib))
} else if (sampleId.isDefined) fill(sampleId.get, None)
else summary.samples.foreach(fill(_, None))
for ((insertSize, counts) <- map) {
tsvWriter.print(insertSize)
if (libraryLevel) {
for (
sample <- summary.samples if sampleId.isEmpty || sampleId.get == sample;
lib <- summary.libraries(sample) if libId.isEmpty || libId.get == lib
) {
tsvWriter.print("\t" + counts.getOrElse(s"$sample-$lib", "0"))
}
} else {
for (sample <- summary.samples if sampleId.isEmpty || sampleId.get == sample) {
tsvWriter.print("\t" + counts.getOrElse(sample, "0"))
}
}
tsvWriter.println()
}
tsvWriter.close()
val plot = new LinePlot(null)
plot.input = tsvFile
plot.output = pngFile
plot.ylabel = Some("Reletive position")
plot.xlabel = Some("Coverage")
plot.width = Some(1200)
plot.removeZero = true
plot.title = Some("Rna coverage")
plot.runLocal()
}
}
......@@ -27,6 +27,7 @@ trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
def indexPage = {
val wgsExecuted = summary.getSampleValues("bammetrics", "stats", "wgs").values.exists(_.isDefined)
val rnaExecuted = summary.getSampleValues("bammetrics", "stats", "rna").values.exists(_.isDefined)
ReportPage(
List("Samples" -> generateSamplesPage(pageArgs)) ++
......@@ -44,7 +45,9 @@ trait MultisampleMappingReportTrait extends MultisampleReportBuilder {
),
"Insert Size" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/insertSize.ssp",
Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false))) ++
( if (wgsExecuted) List("Whole genome coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/wgsHistogram.ssp",
(if (wgsExecuted) List("Whole genome coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/wgsHistogram.ssp",
Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false))) else Nil) ++
(if (rnaExecuted) List("Rna coverage" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/bammetrics/rnaHistogram.ssp",
Map("sampleLevel" -> true, "showPlot" -> true, "showTable" -> false))) else Nil) ++
List("QC reads" -> ReportSection("/nl/lumc/sasc/biopet/pipelines/flexiprep/flexiprepReadSummary.ssp",
Map("showPlot" -> true, "showTable" -> false)),
......
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