Skip to content
Snippets Groups Projects
Commit 3ddac00d authored by Blaise Li's avatar Blaise Li
Browse files

Made lfc-lfc plotting specificities optional.

parent 34c5af35
No related branches found
No related tags found
No related merge requests found
...@@ -152,9 +152,11 @@ class Scatterplot: ...@@ -152,9 +152,11 @@ class Scatterplot:
if gene_id in chose_from] if gene_id in chose_from]
return [gene_id for gene_id in self.data.query(selector).index] return [gene_id for gene_id in self.data.query(selector).index]
def plot_maker(self, grouping=None, group2colour=None, **kwargs): def plot_maker(self, annotate_folds=True, grouping=None, group2colour=None, **kwargs):
"""Builds a plotting function that can colour dots based on them """Builds a plotting function that can colour dots based on them
belonging to a group defined by *grouping*.""" belonging to a group defined by *grouping*.
If *annotate_folds* is True, lines indicating 2-fold thresholds
will be added to the plot, as well as counts in each quadrants."""
def plotting_function(): def plotting_function():
"""Generates the scatterplot, returns its legend so that """Generates the scatterplot, returns its legend so that
*save_plot* can include it in the bounding box.""" *save_plot* can include it in the bounding box."""
...@@ -173,110 +175,111 @@ class Scatterplot: ...@@ -173,110 +175,111 @@ class Scatterplot:
return None return None
else: else:
raise raise
# Lines indicating 2-fold threshold. if annotate_folds:
# Assumes the data are in log2 fold changes # Lines indicating 2-fold threshold.
line_style = { # Assumes the data are in log2 fold changes
"linewidth": 0.5, "color": "0.5", "linestyle": "dashed"} line_style = {
if "x_range" in kwargs: "linewidth": 0.5, "color": "0.5", "linestyle": "dashed"}
(x_annot_loc, _) = kwargs["x_range"] if "x_range" in kwargs:
else: (x_annot_loc, _) = kwargs["x_range"]
x_annot_loc = min(self.data.x) else:
if "y_range" in kwargs: x_annot_loc = min(self.data.x)
(y_annot_loc, _) = kwargs["y_range"] if "y_range" in kwargs:
else: (y_annot_loc, _) = kwargs["y_range"]
y_annot_loc = min(self.data.y) else:
axis.axhline(y=1, **line_style) y_annot_loc = min(self.data.y)
axis.annotate( axis.axhline(y=1, **line_style)
f"y = 1", xy=(x_annot_loc, 1), xycoords="data", axis.annotate(
horizontalalignment='left', f"y = 1", xy=(x_annot_loc, 1), xycoords="data",
verticalalignment='bottom', horizontalalignment='left',
size="x-small", verticalalignment='bottom',
color=line_style["color"]) size="x-small",
axis.axhline(y=-1, **line_style) color=line_style["color"])
axis.annotate( axis.axhline(y=-1, **line_style)
f"y = -1", xy=(x_annot_loc, -1), xycoords="data", axis.annotate(
horizontalalignment='left', f"y = -1", xy=(x_annot_loc, -1), xycoords="data",
verticalalignment='top', horizontalalignment='left',
size="x-small", verticalalignment='top',
color=line_style["color"]) size="x-small",
axis.axvline(x=1, **line_style) color=line_style["color"])
axis.annotate( axis.axvline(x=1, **line_style)
f"x = -1", xy=(-1, y_annot_loc), xycoords="data", axis.annotate(
horizontalalignment='right', f"x = -1", xy=(-1, y_annot_loc), xycoords="data",
verticalalignment='bottom', horizontalalignment='right',
rotation=90, size="x-small", verticalalignment='bottom',
color=line_style["color"]) rotation=90, size="x-small",
axis.axvline(x=-1, **line_style) color=line_style["color"])
axis.annotate( axis.axvline(x=-1, **line_style)
f"x = 1", xy=(1, y_annot_loc), xycoords="data", axis.annotate(
horizontalalignment='left', f"x = 1", xy=(1, y_annot_loc), xycoords="data",
verticalalignment='bottom', horizontalalignment='left',
rotation=90, size="x-small", verticalalignment='bottom',
color=line_style["color"]) rotation=90, size="x-small",
# Number of genes beyond lfc thresholds, in each quadrant color=line_style["color"])
# up_up = 100 * len(self.data.query( # Number of genes beyond lfc thresholds, in each quadrant
# f"x > 1 & y > 1")) / len(self.data) # up_up = 100 * len(self.data.query(
# up_down = 100 * len(self.data.query( # f"x > 1 & y > 1")) / len(self.data)
# f"x > 1 & y < 1")) / len(self.data) # up_down = 100 * len(self.data.query(
# down_up = 100 * len(self.data.query( # f"x > 1 & y < 1")) / len(self.data)
# f"x < 1 & y > 1")) / len(self.data) # down_up = 100 * len(self.data.query(
# down_down = 100 * len(self.data.query( # f"x < 1 & y > 1")) / len(self.data)
# f"x < 1 & y < 1")) / len(self.data) # down_down = 100 * len(self.data.query(
up_up = self.data.query( # f"x < 1 & y < 1")) / len(self.data)
"x > 1 & y > 1") up_up = self.data.query(
up_down = self.data.query( "x > 1 & y > 1")
"x > 1 & y < -1") up_down = self.data.query(
down_up = self.data.query( "x > 1 & y < -1")
"x < -1 & y > 1") down_up = self.data.query(
down_down = self.data.query( "x < -1 & y > 1")
"x < -1 & y < -1") down_down = self.data.query(
if isinstance(grouping, (list, tuple)): "x < -1 & y < -1")
try: if isinstance(grouping, (list, tuple)):
(_, colour) = group2colour try:
except (ValueError, TypeError): (_, colour) = group2colour
colour = "black" except (ValueError, TypeError):
select_ingroup = pd.Index(grouping).intersection colour = "black"
ingroup_up_up = " (\\textcolor{%s}{%d})" % ( select_ingroup = pd.Index(grouping).intersection
colour, ingroup_up_up = " (\\textcolor{%s}{%d})" % (
len(up_up.loc[select_ingroup(up_up.index)]),) colour,
ingroup_up_down = " (\\textcolor{%s}{%d})" % ( len(up_up.loc[select_ingroup(up_up.index)]),)
colour, ingroup_up_down = " (\\textcolor{%s}{%d})" % (
len(up_down.loc[select_ingroup(up_down.index)])) colour,
ingroup_down_up = " (\\textcolor{%s}{%d})" % ( len(up_down.loc[select_ingroup(up_down.index)]))
colour, ingroup_down_up = " (\\textcolor{%s}{%d})" % (
len(down_up.loc[select_ingroup(down_up.index)])) colour,
ingroup_down_down = " (\\textcolor{%s}{%d})" % ( len(down_up.loc[select_ingroup(down_up.index)]))
colour, ingroup_down_down = " (\\textcolor{%s}{%d})" % (
len(down_down.loc[select_ingroup(down_down.index)])) colour,
else: len(down_down.loc[select_ingroup(down_down.index)]))
ingroup_up_up = "" else:
ingroup_up_down = "" ingroup_up_up = ""
ingroup_down_up = "" ingroup_up_down = ""
ingroup_down_down = "" ingroup_down_up = ""
axis.annotate( ingroup_down_down = ""
f"{len(up_up)}{ingroup_up_up}", axis.annotate(
xy=(0.95, 0.95), xycoords="axes fraction", f"{len(up_up)}{ingroup_up_up}",
size="x-small", color=line_style["color"], xy=(0.95, 0.95), xycoords="axes fraction",
horizontalalignment="right", size="x-small", color=line_style["color"],
verticalalignment="top") horizontalalignment="right",
axis.annotate( verticalalignment="top")
f"{len(up_down)}{ingroup_up_down}", axis.annotate(
xy=(0.95, 0.05), xycoords="axes fraction", f"{len(up_down)}{ingroup_up_down}",
size="x-small", color=line_style["color"], xy=(0.95, 0.05), xycoords="axes fraction",
horizontalalignment="right", size="x-small", color=line_style["color"],
verticalalignment="bottom") horizontalalignment="right",
axis.annotate( verticalalignment="bottom")
f"{len(down_up)}{ingroup_down_up}", axis.annotate(
xy=(0.05, 0.95), xycoords="axes fraction", f"{len(down_up)}{ingroup_down_up}",
size="x-small", color=line_style["color"], xy=(0.05, 0.95), xycoords="axes fraction",
horizontalalignment="left", size="x-small", color=line_style["color"],
verticalalignment="top") horizontalalignment="left",
axis.annotate( verticalalignment="top")
f"{len(down_down)}{ingroup_down_down}", axis.annotate(
xy=(0.05, 0.05), xycoords="axes fraction", f"{len(down_down)}{ingroup_down_down}",
size="x-small", color=line_style["color"], xy=(0.05, 0.05), xycoords="axes fraction",
horizontalalignment="left", size="x-small", color=line_style["color"],
verticalalignment="bottom") horizontalalignment="left",
verticalalignment="bottom")
axis.set_xlabel(self.x_label, fontsize=17) axis.set_xlabel(self.x_label, fontsize=17)
axis.set_ylabel(self.y_label, fontsize=17) axis.set_ylabel(self.y_label, fontsize=17)
# This doesn't work with plt.axis("equal") # This doesn't work with plt.axis("equal")
...@@ -307,7 +310,7 @@ class Scatterplot: ...@@ -307,7 +310,7 @@ class Scatterplot:
return plotting_function return plotting_function
def save_plot(self, outfile, def save_plot(self, outfile,
grouping=None, group2colour=None, annotate_folds=True, grouping=None, group2colour=None,
**kwargs): **kwargs):
"""Creates the plotting function and transmits it for execution """Creates the plotting function and transmits it for execution
to the function that really does the saving.""" to the function that really does the saving."""
...@@ -318,10 +321,14 @@ class Scatterplot: ...@@ -318,10 +321,14 @@ class Scatterplot:
# equal_axes = False # equal_axes = False
# else: # else:
# equal_axes = True # equal_axes = True
equal_axes = True if annotate_folds:
equal_axes = True
else:
equal_axes = False
save_plot( save_plot(
outfile, outfile,
self.plot_maker( self.plot_maker(
annotate_folds,
grouping=grouping, group2colour=group2colour, **kwargs), grouping=grouping, group2colour=group2colour, **kwargs),
equal_axes=equal_axes, equal_axes=equal_axes,
tight=True) tight=True)
...@@ -407,6 +414,12 @@ def main(): ...@@ -407,6 +414,12 @@ def main():
# "-t", "--transform", # "-t", "--transform",
# help="log2, log10, or a linear scale to apply.", # help="log2, log10, or a linear scale to apply.",
# default=0) # default=0)
parser.add_argument(
"--not_foldchanges",
help="Use this option to inactivate plotting options for "
"log2FoldChange.",
default=False,
action="store_true")
parser.add_argument( parser.add_argument(
"--plot_regression", "--plot_regression",
help="Use this option to plot the regression line.", help="Use this option to plot the regression line.",
...@@ -476,6 +489,7 @@ def main(): ...@@ -476,6 +489,7 @@ def main():
plot_data.save_plot( plot_data.save_plot(
# args.x_axis, args.y_axis, # args.x_axis, args.y_axis,
out_pdf, out_pdf,
annotate_folds=not args.not_foldchanges,
grouping=gene_list, group2colour=(list_name, args.colour), grouping=gene_list, group2colour=(list_name, args.colour),
x_range=args.data_range, x_range=args.data_range,
y_range=args.data_range, y_range=args.data_range,
...@@ -489,6 +503,7 @@ def main(): ...@@ -489,6 +503,7 @@ def main():
plot_data.save_plot( plot_data.save_plot(
# args.x_axis, args.y_axis, # args.x_axis, args.y_axis,
out_pdf, out_pdf,
annotate_folds=not args.not_foldchanges,
x_range=args.data_range, x_range=args.data_range,
y_range=args.data_range, y_range=args.data_range,
regression=args.plot_regression) regression=args.plot_regression)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment