Skip to content

Commit

Permalink
Merge pull request #35 from karel-brinda/updates
Browse files Browse the repository at this point in the history
RNFtools 0.2.3
  • Loading branch information
karel-brinda committed Nov 2, 2015
2 parents a5ca5de + 919616d commit 38d9ea9
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 89 deletions.
12 changes: 6 additions & 6 deletions examples/02_all_simulators_example/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ rnftools.mishmash.WgSim(
read_length_2=0,
)

rnftools.mishmash.CuReSim(
fasta=smbl.fasta.EXAMPLE_1,
number_of_read_tuples=10000,
read_length_1=100,
read_length_2=0,
)
#rnftools.mishmash.CuReSim(
# fasta=smbl.fasta.EXAMPLE_1,
# number_of_read_tuples=10000,
# read_length_1=100,
# read_length_2=0,
#)


rnftools.mishmash.sample("pe_example",reads_in_tuple=2)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ sphinx>=1.3.0
sphinxcontrib-napoleon>=0.3.3
sphinx_rtd_theme>=0.1.8
snakemake>=3.4.2
smbl>=1.3.0
smbl>=1.4.0
81 changes: 52 additions & 29 deletions rnftools/lavender/Bam.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,19 +731,21 @@ def create_html(self):
for line in roc:
line=line.strip()
if line!="" and line[0]!="#":
(q,M,w,m,P,U,u,T,t,x,a)=line.split("\t")
(q,M,w,m,P,U,u,T,t,x,a)=map(int,line.split("\t"))
roc_dict = {
"q":int(q),
"M":int(M),
"w":int(w),
"m":int(m),
"P":int(P),
"U":int(U),
"u":int(u),
"T":int(T),
"t":int(t),
"x":int(x),
"a":int(a)
"q":q,
"M":M,
"w":w,
"m":m,
"P":P,
"U":U,
"u":u,
"T":T,
"t":t,
"x":x,
"a":a,
"mapped":M+w+m+P,
"unmapped":U+u+T+t+x,
}
roc_dicts.append(roc_dict)
tbody = os.linesep.join([
Expand Down Expand Up @@ -777,30 +779,30 @@ def create_html(self):
</tr>
""".format(
quality = roc_dict["q"],
mapped = roc_dict["M"]+roc_dict["w"]+roc_dict["m"]+roc_dict["P"],
mapped_proc = 100.0*(roc_dict["M"]+roc_dict["w"]+roc_dict["m"]+roc_dict["P"])/roc_dict["a"],
mapped = roc_dict["mapped"],
mapped_proc = 100.0*(roc_dict["mapped"])/roc_dict["a"],
M = roc_dict["M"],
M_proc = 100.0*(roc_dict["M"])/roc_dict["a"],
M_proc = 100.0*(roc_dict["M"])/(roc_dict["mapped"]) if (roc_dict["mapped"]) != 0 else 0,
w = roc_dict["w"],
w_proc = 100.0*(roc_dict["w"])/roc_dict["a"],
w_proc = 100.0*(roc_dict["w"])/(roc_dict["mapped"]) if (roc_dict["mapped"]) != 0 else 0,
m = roc_dict["m"],
m_proc = 100.0*(roc_dict["m"])/roc_dict["a"],
m_proc = 100.0*(roc_dict["m"])/(roc_dict["mapped"]) if (roc_dict["mapped"]) != 0 else 0,
P = roc_dict["P"],
P_proc = 100.0*(roc_dict["P"])/roc_dict["a"],
unmapped = roc_dict["U"]+roc_dict["u"]+roc_dict["T"]+roc_dict["t"]+roc_dict["x"],
unmapped_proc = 100.0*(roc_dict["U"]+roc_dict["u"]+roc_dict["T"]+roc_dict["t"]+roc_dict["x"])/roc_dict["a"],
P_proc = 100.0*(roc_dict["P"])/(roc_dict["mapped"]) if (roc_dict["mapped"]) != 0 else 0,
unmapped = roc_dict["unmapped"],
unmapped_proc = 100.0*(roc_dict["unmapped"])/roc_dict["a"],
U = roc_dict["U"],
U_proc = 100.0*(roc_dict["U"])/roc_dict["a"],
U_proc = 100.0*(roc_dict["U"])/(roc_dict["unmapped"]) if (roc_dict["unmapped"]) != 0 else 0,
u = roc_dict["u"],
u_proc = 100.0*(roc_dict["u"])/roc_dict["a"],
u_proc = 100.0*(roc_dict["u"])/(roc_dict["unmapped"]) if (roc_dict["unmapped"]) != 0 else 0,
T = roc_dict["T"],
T_proc = 100.0*(roc_dict["T"])/roc_dict["a"],
T_proc = 100.0*(roc_dict["T"])/(roc_dict["unmapped"]) if (roc_dict["unmapped"]) != 0 else 0,
t = roc_dict["t"],
t_proc = 100.0*(roc_dict["t"])/roc_dict["a"],
t_proc = 100.0*(roc_dict["t"])/(roc_dict["unmapped"]) if (roc_dict["unmapped"]) != 0 else 0,
x = roc_dict["x"],
x_proc = 100.0*(roc_dict["x"])/roc_dict["a"],
x_proc = 100.0*(roc_dict["x"])/(roc_dict["unmapped"]) if (roc_dict["unmapped"]) != 0 else 0,
sum = roc_dict["a"],
prec_proc = 100.0*(roc_dict["M"])/(roc_dict["M"]+roc_dict["w"]+roc_dict["m"]+roc_dict["P"]) if (roc_dict["M"]+roc_dict["w"]+roc_dict["m"]+roc_dict["P"]) != 0 else 0,
prec_proc = 100.0*(roc_dict["M"])/(roc_dict["mapped"]) if (roc_dict["mapped"]) != 0 else 0,
)
for roc_dict in roc_dicts
])
Expand All @@ -823,11 +825,23 @@ def create_html(self):

program_info=["<table>"]
for part in parts:
(lvalue,colon,rvalue)=part.partition(":")
(lvalue,_,rvalue)=part.partition(":")
program_info.append('<tr><td style="font-weight:bold">{}:</td><td style="text-align:left">{}</td></tr>'.format(lvalue,rvalue))

program_info.append("</table>")



eval_info=["<table>"]
for (lvalue,rvalue) in [
("BAM file", self._bam_fn),
("Allowed delta", self.report.allowed_delta),
]:
eval_info.append('<tr><td style="font-weight:bold">{}:</td><td style="text-align:left">{}</td></tr>'.format(lvalue,rvalue))

eval_info.append("</table>")


html_src="""<!DOCTYPE html>
<html>
<head>
Expand All @@ -851,12 +865,20 @@ def create_html(self):
</h1>
<p>
<a href="#info">Information about program</a> -
<a href="#info_eval">Information about evaluation</a> -
<a href="#info_prog">Information about program</a> -
<a href="#roctable">ROC table</a> -
<a href="#graphs">Graphs</a>
</p>
<h2 id="info">
<h2 id="info_eval">
Information about evaluation
{headline_links}
</h2>
{eval_info}
<h2 id="info_prog">
Information about program
{headline_links}
</h2>
Expand Down Expand Up @@ -998,6 +1020,7 @@ def create_html(self):
self._gp_fn,
os.path.dirname(self._html_fn)
),
eval_info=os.linesep.join(eval_info),
program_info=os.linesep.join(program_info),
homepage=os.path.relpath(
self.report.html_fn(),
Expand Down
10 changes: 9 additions & 1 deletion rnftools/lavender/Panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class Panel:
report (rnftools.lavender.Report): The owner report.
bam_dir (str): Directory to the BAM files for this panel.
panel_dir (str): Directory with auxiliary files for this panel.
name (str): Name of the panel.
name (str): Name of the panel (used for CSS, etc.).
keep_intermediate_files (bool): Keep files created in intermediate steps during evaluation.
compress_intermediate_files (bool): Compress files created in intermediate steps during evaluation.
default_x_axis (str): Values on x-axis, e.g., "({m}+{w})/({M}+{m}+{w})".
default_x_label (str): Label on x-axis.
title (str): Title of the panel (to be displayed).
gp_style_func (function(i, nb)): Function assigning GnuPlot styles for overall graphs. Arguments: i: 0-based id of curve, nb: number of curves.
Raises:
Expand All @@ -36,6 +37,7 @@ def __init__(self,
bam_dir,
panel_dir,
name,
title,
keep_intermediate_files,
compress_intermediate_files,
default_x_axis,
Expand All @@ -46,6 +48,7 @@ def __init__(self,
self.report=report
rnftools.lavender.add_panel(self)
self.name=name
self.title=title
self.panel_dir=panel_dir
self.default_x_axis=default_x_axis
self.default_x_label=default_x_label
Expand Down Expand Up @@ -109,6 +112,9 @@ def get_html_column(self):

panel_id="panel_{}".format(self.name)
return [
"<h2>{}</h2>".format(self.title)
] + [
#list of links
(" <br />"+os.linesep).join(
[
"""
Expand All @@ -126,6 +132,7 @@ def get_html_column(self):

),

#main graph
"""
<div class="formats">
<a href="{html}" id="{panel_id}_">
Expand All @@ -139,6 +146,7 @@ def get_html_column(self):
panel_id=panel_id,
),
] + [
# overall graphs
"""
<div class="formats">
<img src="{svg}" />
Expand Down
114 changes: 89 additions & 25 deletions rnftools/lavender/Report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import rnftools
import snakemake
import os
import collections


from . import DEFAULT_ALLOWED_DELTA
Expand All @@ -12,11 +13,14 @@
##############
##############
class Report:
"""Class for an entire report.
"""Class for an entire report.
Args:
name (str): Name of the report.
bam_dirs (str): Directories with BAM files.
name (str): Name of the report (name of output file and dir).
title (str): Title of the report (if None, then name is used).
description (str): Description of the report.
bam_dirs (list of str): Directories with BAM files.
panels (list of dicts): More advanced configuration of directories with BAM files.
allowed_delta (int): Tolerance of difference of coordinates between true (i.e., expected) alignment and real alignment (very important parameter!).
default_x_run ((float,float)): Range for x-axis in GnuPlot plots.
default_y_run ((float,float)): Range for y-axis in GnuPlot plots.
Expand All @@ -29,12 +33,17 @@ class Report:
gp_style_func (function(i, nb)): Function assigning GnuPlot styles for overall graphs. Arguments: i: 0-based id of curve, nb: number of curves.
"""

#todo: describe format of panels


def __init__(
self,
name,
title=None,
description="",
allowed_delta=DEFAULT_ALLOWED_DELTA,
bam_dirs=[],
bam_dirs=None,
panels=None,
default_x_run=(0.00001,1.0),
default_y_run=(60,100),
default_pdf_size_cm=(10,10),
Expand All @@ -50,8 +59,10 @@ def __init__(

rnftools.lavender.add_report(self)

self.name = name
self.report_dir = self.name
self.name=name
self.report_dir=self.name
self.title=self.name if title==None else title
self.description=description

self.default_x_run=self._load_x_run(default_x_run)
self.default_y_run=self._load_y_run(default_y_run)
Expand All @@ -63,20 +74,69 @@ def __init__(
assert 0 <= allowed_delta

self._html_fn = name+".html"
self.panels = [
rnftools.lavender.Panel(
bam_dir=bam_dirs[i],
panel_dir=os.path.join(self.report_dir,str(i)),
report=self,
name=str(i),
keep_intermediate_files=keep_intermediate_files,
compress_intermediate_files=compress_intermediate_files,
default_x_axis=default_x_axis,
default_x_label=default_x_label,
gp_style_func=self._gp_style_func,

assert bam_dirs is None or panels is None, "Panels can be specified using bam_dirs or panels, but not both at the same time."

self.panels=[]

if bam_dirs is not None:

assert hasattr(bam_dirs, '__iter__'), "bamdirs should be iterable (list, tuple, etc.)"

#assert isinstance(bam_dirs,collections.iterable)
#assert isinstance(bamdirs, basestring)

self.panels = [
rnftools.lavender.Panel(
bam_dir=bam_dirs[i],
panel_dir=os.path.join(self.report_dir,str(i)),
report=self,
name=str(i),
title="dir {}".format(i),
keep_intermediate_files=keep_intermediate_files,
compress_intermediate_files=compress_intermediate_files,
default_x_axis=default_x_axis,
default_x_label=default_x_label,
gp_style_func=self._gp_style_func,
)
for i in range(len(bam_dirs))
]

if panels is not None:

for i,panel_dict in enumerate(panels):

bam_dir=panel_dict["bam_dir"]

try:
panel_dir=panel_dict["panel_dir"]
except KeyError:
panel_dir=os.path.join(self.report_dir,str(i))

try:
panel_name=panel_dict["name"]
except KeyError:
panel_name="panel_{}".format(i)

try:
panel_title=panel_dict["title"]
except KeyError:
panel_title="dir {}".format(i)

self.panels.append(
rnftools.lavender.Panel(
bam_dir=bam_dir,
panel_dir=panel_dir,
report=self,
name=panel_name,
title=panel_title,
keep_intermediate_files=keep_intermediate_files,
compress_intermediate_files=compress_intermediate_files,
default_x_axis=default_x_axis,
default_x_label=default_x_label,
gp_style_func=self._gp_style_func,
)
)
for i in range(len(bam_dirs))
]

rnftools.lavender.add_input(self._html_fn)

Expand Down Expand Up @@ -121,7 +181,7 @@ def add_graph(self,
y_run=None,
pdf_size_cm=None,
svg_size_px=None,
key_position="top left",
key_position="bottom right",
):
"""
Add a new graph to the overlap report.
Expand Down Expand Up @@ -214,9 +274,9 @@ def create_html(self):
<html>
<head>
<meta charset="UTF-8" />
<title>{name}</title>
<title>{title}</title>
<style type="text/css">
table {{border-collapse:collapse;}}
.main_table {{border-collapse:collapse;margin-top:15px;}}
td {{border: solid #aaaaff 1px;padding:4px;vertical-alignment:top;}}
colgroup, thead {{border: solid black 2px;padding 2px;}}
.configuration {{font-size:85%;}}
Expand All @@ -226,14 +286,18 @@ def create_html(self):
</style>
</head>
<body>
<h1>{name}</h1>
<table>
<h1>{title}</h1>
<strong>{description}</strong>
<table class="main_table">
{html_table}
</table>
</body>
""".format(
html_table=html_table,
name=self.name
title=self.title,
description=self.description,
)
f.write(html_src)

Expand Down
Loading

0 comments on commit 38d9ea9

Please sign in to comment.