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

Renaming function, commenting.

parent 443b2dfe
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3 #!/usr/bin/env python3
# vim: set fileencoding=<utf-8> : # vim: set fileencoding=<utf-8> :
"""This script reads data from "tidy" files and makes plots out of """This script reads data from "tidy" files and makes a scatter plot out of it.
it, at the same scale.
It also outputs a table containing the plotted data points.""" It also outputs a table containing the plotted data points."""
import argparse import argparse
...@@ -91,12 +90,19 @@ class Scatterplot: ...@@ -91,12 +90,19 @@ class Scatterplot:
y_column, y_column,
labels, labels,
extra_cols=None): extra_cols=None):
# usecols can be a callable to filter column names:
# If callable, the callable function will be evaluated against the
# column names, returning names where the callable function evaluates
# to True.
if extra_cols is None: if extra_cols is None:
x_usecols = ["gene", x_column].__contains__ x_usecols = ["gene", x_column].__contains__
y_usecols = ["gene", y_column].__contains__ y_usecols = ["gene", y_column].__contains__
else: else:
x_usecols = ["gene", x_column, *extra_cols].__contains__ x_usecols = ["gene", x_column, *extra_cols].__contains__
y_usecols = ["gene", y_column, *extra_cols].__contains__ y_usecols = ["gene", y_column, *extra_cols].__contains__
# The columns containing the data to plot might have the same name
# in the two tables.
# We rename them to x and y for simplicity.
x_data = pd.read_csv( x_data = pd.read_csv(
x_input_file, sep="\t", index_col="gene", usecols=x_usecols).rename( x_input_file, sep="\t", index_col="gene", usecols=x_usecols).rename(
columns={x_column: "x"}) columns={x_column: "x"})
...@@ -115,6 +121,7 @@ class Scatterplot: ...@@ -115,6 +121,7 @@ class Scatterplot:
self.data = pd.merge( self.data = pd.merge(
x_data, y_data, x_data, y_data,
left_index=True, right_index=True, validate="one_to_one") left_index=True, right_index=True, validate="one_to_one")
# Compute a classifier column (to be used to colour points)
if extra_cols is not None: if extra_cols is not None:
extra_cols = list(concat(( extra_cols = list(concat((
[colname] if colname in self.data.columns [colname] if colname in self.data.columns
...@@ -148,7 +155,7 @@ class Scatterplot: ...@@ -148,7 +155,7 @@ class Scatterplot:
def plot_maker(self, grouping=None, group2colour=None, **kwargs): def plot_maker(self, 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*."""
def plot_lfclfc_scatter(): 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."""
# fig, axis = plot_scatter( # fig, axis = plot_scatter(
...@@ -297,9 +304,11 @@ class Scatterplot: ...@@ -297,9 +304,11 @@ class Scatterplot:
# TODO: force ticks to be integers # TODO: force ticks to be integers
# Return a tuple of "extra artists", # Return a tuple of "extra artists",
# to correctly define the bounding box # to correctly define the bounding box
return plot_lfclfc_scatter return plotting_function
def save_plot(self, outfile, grouping=None, group2colour=None, **kwargs): def save_plot(self, outfile,
grouping=None, group2colour=None,
**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."""
if grouping is None and self.grouping_col is not None: if grouping is None and self.grouping_col is not None:
......
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