# Path: lib/util.py
# STATUS_NORMAL=1
# STATUS_MAX_MEMORY_EXCEEDED=2
# STATUS_MAX_RUNTIME_EXCEEDED=3
# MAX_LEN_OUT = 10000
# def runAndMeasure(command,detailed=True,maxtime=0,maxmem=0):
# def runSimple(command):
# def runAndMeasureInternal(return_queue,command,maxtime=0,maxmem=0):
# def runInternal(control_queue,command,max_memory):
# def measureProcess(queue, initial_pids, command, measurement_interval=1, max_runtime=0, max_memory=0, debug=False):
# 	def extendTargets(targets, proc):
# 	def killTargets(targets):
# 	def updateTargets(targets, metrics):
# 	def updateMetrics(target, metrics):
# def loadConfig(name, parent_dir="", already_included=[]):
# def setCallDir(d):
# def setRootDir(d):
# def enterCallDir():
# def enterRootDir():
# def getRootDir():
# def nl2br(text):
# def msg(text="", level=1):
# def yes_no(b):
# def md5(text):
# def merge(x, y):
# def formatFilesize(n):
# def percent(val, base, offset=0):
# def abs_path(path):
# def get_sam_header_line_count(filename):
# def is_sam_sorted(filename):
# def sort_sam(filename,threads=1):
# def sort_sam_picard(filename,threads=-1):
# def line_count(filename):
# def sanitize_string(s):
# def makeExportDropdown(plot_id,csv_filename):
# def parseMD(md,seq):


import json
import math
from lib import util


percent = util.percent


def generateMappingQualityPlot(self, page):
	results = self.getRunResults()

	columns = [["Correct"], ["Wrong"]]

	csv = "mapq_threshold,correct,wrong\n"

	correct = []
	wrong = []
	mc = max(1, float(results.total))
	mw = max(1, float(results.total))

	wrong_scale = float(results.correct / mc) / float((1 + results.wrong) / mw)

	mapqs = sorted(results.mapq_cumulated)
	for curr in mapqs:
		columns[0].append(results.mapq_cumulated[curr]["correct"] / float(mc))
		columns[1].append((-results.mapq_cumulated[curr]["wrong"] / float(mw)) * wrong_scale)
		csv += "%d,%d,%d\n" % (curr,results.mapq_cumulated[curr]["correct"],results.mapq_cumulated[curr]["wrong"])

	csv_filename = self.writeCSV(self.getMapper().getName() + "_mapqs",csv)


# Path: lib/util.py
# STATUS_NORMAL=1
# STATUS_MAX_MEMORY_EXCEEDED=2
# STATUS_MAX_RUNTIME_EXCEEDED=3
# MAX_LEN_OUT = 10000
# def runAndMeasure(command,detailed=True,maxtime=0,maxmem=0):
# def runSimple(command):
# def runAndMeasureInternal(return_queue,command,maxtime=0,maxmem=0):
# def runInternal(control_queue,command,max_memory):
# def measureProcess(queue, initial_pids, command, measurement_interval=1, max_runtime=0, max_memory=0, debug=False):
# 	def extendTargets(targets, proc):
# 	def killTargets(targets):
# 	def updateTargets(targets, metrics):
# 	def updateMetrics(target, metrics):
# def loadConfig(name, parent_dir="", already_included=[]):
# def setCallDir(d):
# def setRootDir(d):
# def enterCallDir():
# def enterRootDir():
# def getRootDir():
# def nl2br(text):
# def msg(text="", level=1):
# def yes_no(b):
# def md5(text):
# def merge(x, y):
# def formatFilesize(n):
# def percent(val, base, offset=0):
# def abs_path(path):
# def get_sam_header_line_count(filename):
# def is_sam_sorted(filename):
# def sort_sam(filename,threads=1):
# def sort_sam_picard(filename,threads=-1):
# def line_count(filename):
# def sanitize_string(s):
# def makeExportDropdown(plot_id,csv_filename):
# def parseMD(md,seq):


# Path: tests_base/base_mapping/report_detail.py
import json
import math
from lib import util



percent = util.percent


def generateMappingQualityPlot(self, page):
	results = self.getRunResults()

	columns = [["Correct"], ["Wrong"]]

	csv = "mapq_threshold,correct,wrong\n"

	correct = []
	wrong = []
	mc = max(1, float(results.total))
	mw = max(1, float(results.total))

	wrong_scale = float(results.correct / mc) / float((1 + results.wrong) / mw)

	mapqs = sorted(results.mapq_cumulated)
	for curr in mapqs:
		columns[0].append(results.mapq_cumulated[curr]["correct"] / float(mc))
		columns[1].append((-results.mapq_cumulated[curr]["wrong"] / float(mw)) * wrong_scale)
		csv += "%d,%d,%d\n" % (curr,results.mapq_cumulated[curr]["correct"],results.mapq_cumulated[curr]["wrong"])

	csv_filename = self.writeCSV(self.getMapper().getName() + "_mapqs",csv)


	page.addSection("Mapping Quality Thresholds", """<div id="plot_mapq_thresh"></div>%s""" % util.makeExportDropdown("plot_mapq_thresh",csv_filename),None,"The plot below shows the percentages of correctly and wrongly mapped reads for all mapping quality thresholds for this mapper. The values at threshold 0 therefore correspond to the unfiltered results.")