Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#! /usr/bin/env python3
########################################################################### # # # This file is part of Counter RNAseq Window (craw) package. # # # # Authors: Bertrand Neron # # Copyright (c) 2017-2019 Institut Pasteur (Paris). # # see COPYRIGHT file for details. # # # # craw is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # craw is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # See the GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with craw (see COPYING file). # # If not, see <http://www.gnu.org/licenses/>. # # # ###########################################################################
""" check value given by the parser
:param str value: the value given by the parser for --sort-using-file option :return: the normpath of the value :raises: ArgumentError if the file does not exists, or is not a file, or not readable """ raise argparse.ArgumentError(None, "{} is not readable".format(value))
""" Parse value given by the parser
:param value: the value given by the parser for --sort-by-gene-size option :type value: string :return: name of column representing the start of gene, name of the column representing the end of gene :rtype: tuple of 2 string :raise: :class:`argparse.ArgumentError` object if value cannot be parsed """ # this function is called only if value is provided to sort-by-gene-size option rv = ['', ''] else: "Must be start_col, stop_col (separated by a comma), " "default= annotation_start,annotation_end".format(value)) from None
""" Parse value given by the parser for --size option the value must follow the syntax widexheight[unit] if the unit is omitted unit is inch otherwise unit must be
* 'mm' for millimeters * 'cm' for centimeters * 'in' for inches * 'px' for pixels
wide and height must be positive integers.
:param value: the size of the figure :type value: string :return: the size in inch ask by the user :rtype: tuple of float :raise: :class:`argparse.ArgumentError` object """
The value must be widexheight[unit] or 'raw'. 'wide' and 'height' must be positive integers By default unit is in inches. eg: 7x10 or 7x10in for 7 inches wide by 10 inches height 70x100mm for 70 mm by 100 mm. default=7x10 or 10x7 depending of the figure orientation.""" else:
""" :return: a human readable of craw_htmp version and it's main dependencies. :rtype: str """ # pyplot must be import after the argument parsing # because in function of the environment ($DYSPLAY and options) the craw_htmp behavior # is not the same # so heatmap cannot be import before argument parsing # so I import the dependencies in get_version_message. # it's not very important as get_version_message is called only when --version is set # so the program quit after displaying this message. Using: - numpy {np_ver} - pandas {pd_ver} - matplotlib {mtp_ver} - pillow {pil_ver} """.format(np_ver=np.__version__, pd_ver=pd.__version__, mtp_ver=mtp.__version__, pil_ver=PIL.PILLOW_VERSION )
""" :param args: the arguments and option as provided by sys.argv without the program name :return: the argument parsed :rtype: :class:`argparse.Namespace` object """ formatter_class=argparse.RawTextHelpFormatter)
help="the path to the coverage file.")
nargs=2, help="""crop the matrix. This option need two values the name of the first and last column to keep [start col, stop col] eg --crop -10 1000 .""")
default=False, help="sort the rows using the column COL.") type=_file_readable, help="""Sort the rows using a file. The file must have on the first line the name of the column which will use to sort. Each following lines must match to a value of this column in the data.""") type=_gene_size_parser, nargs='*', metavar='start_col,stop_col', help="""The rows will be sorted by gene size using start-col and stop-col to compute length.
start-col and stop-col must be a string separated by comma. If start-col and stop-col are not specified annotation_start and annotation_end for start-col and stop-col respectively will be used. (Don't put this option without value just before the coverage file.)""")
action="store_true", default=False, help="Display only sense matrix (default is display both).") action="store_true", default=False, help="Display only anti sense matrix (default is display both).")
default="Blues", help="""The color map used to display data. The allowed values are defined in http:matplotlib.org/examples/color/colormaps_reference.html eg: Blues, BuGn, Greens, GnBu, ... (default: Blues).""") help="""The figure title. It will display on the top of the figure. (default: the name of the coverage file without extension).""") type=int, help="""The resolution of the output . This option work only if --out option is specified with size not raw. (default: matplolibrc figure.dpi""") type=_size_fig_parser, help="""Specify the figure size. The value must be widexheight[unit] or 'raw'.
If value is raw it will be produce two image files (for sense and antisense) " with one pixel correspond to one coverage value for one nucleotide.
Otherwise, 'wide' and 'height' must be positive integers By default units are in inches eg:
* 7x10 or 7x10in for 7 inches wide by 10 inches height * 70x100mm for 70 mm by 100 mm.
(default: 7x10 or 10x7 depending of the figure orientation).""")
choices=["lin", "log", "row", "log+row", "row+log"], default="lin", help="""Which normalization to apply to the data before display them.
* lin a linear normalization is applied on the whole matrix. * log a 10 base logarithm will be applied on the data before matrix normalization. * row mean that a linear normalisation is compute row by row. * log+row mean a 10 base logarithm will be applied before a normalisation row by row. ('row+log' is an alias for 'log+row'). (default: lin""")
action='append', nargs='*', metavar='POS [COLOR]', help="""* POS is mandatory and must be a positive integer. * COLOR is optional The supported color formats are: - Hexadecimal color specifiers, given as '#rgb' or '#rrggbb'. For example, '#ff0000' specifies pure red. - Common HTML color names.
If COLOR is omitted the color corresponding to the highest value in the color map (--cmap) will be used. (this option cannot be the last one just before the coverage file, on the command line.)""")
action="store_true", default=False, help="Where to display the sense matrix relative to antisense matrix (default is top).") action="store_true", default=False, help="Where to display the sense matrix relative to antisense matrix (default is top).") action="store_true", default=False, help="Where to display the sense matrix relative to antisense matrix (default is top).") action="store_true", default=False, help="Where to display the sense matrix relative to antisense matrix (default is top).")
help="""The name of the file (the format will based on the extension) to save the figure. Instead of displaying the figure on the screen, save it directly in this file.
If this option is used with --size raw 2 files will be produced for respectively sense and anti sense. The extension 'sense' or 'antisense' will be added between the name and the suffix eg: --size raw --out foo.png give 2 files 'foo.sense.png' and 'foo.antisense.png'.
If no format (determine using the suffix) is given 'png' will be used.""")
action='count', default=0, help="Increase output verbosity.") action="count", default=0, help="Reduce output verbosity.") action='version', version=get_version_message(), help="display the version information and quit.")
""" The entrypoint for craw_html script
It will generate a heatmap representing the coverage matrix around the position of interest it can display the results on the screen or write it on file depending the options
:param args: The arguments and option representing the command line :type args: list of string :param log_level: the level of verbosity :param logger_out: True if you want to display logs on stdout, False otherwise :return: """
########################### # validating some options # ########################### # test if DISPlAY 'DISPLAY' variable is not set (you probably run craw_htmp in non graphic environment) So you must specify an output format (add ext to the output file option as 'my_file.png') """) out_format, list(non_interactive_backends.keys()) )) else: 'DISPLAY' variable is not set (you probably run craw_htmp in non graphic environment) So you cannot use interactive output please specify an output file (--out). """)
# the import of pyplot must be done after setting the backend
parsed_args.sort_by_gene_size = ['annotation_start', 'annotation_end'] parsed_args.sort_by_gene_size = parsed_args.sort_by_gene_size[0]
msg = "{} is not writable".format(out_dir) log.error(msg) raise RuntimeError(msg)
################ # sorting data # ################ # if data is empty or data is None sort return data # so it's error safe, and not time and space consuming start_col, stop_col = parsed_args.sort_by_gene_size sense_data = heatmap.sort(sense_data, 'by_gene_size', start_col=start_col, stop_col=stop_col) antisense_data = heatmap.sort(antisense_data, 'by_gene_size', start_col=start_col, stop_col=stop_col)
################ # croping data # ################
#################### # Normalizing data # ####################
else: # log+row or row+log
################## # Drawing figure # ################## # pillow backend else:
im_format = 'png' else: im_format = 'png' root_name = os.path.splitext(parsed_args.cov_file)[0]
else: log.warning("{} data are empty: skip drawing.".format(sense)) else: # matplotlib backend ########## # layout # ########## if parsed_args.sense_on_left: sense_on = 'left' elif parsed_args.sense_on_right: sense_on = 'right' elif parsed_args.sense_on_top: sense_on = 'top' elif parsed_args.sense_on_bottom: sense_on = 'bottom' else: sense_on = 'top'
if parsed_args.title is None: title = os.path.basename(os.path.splitext(parsed_args.cov_file)[0]) else: title = parsed_args.title
fig = heatmap.draw_heatmap(sense_data, antisense_data, color_map=color_map, title=title, sense_on=sense_on, size=parsed_args.size, marks=parsed_args.mark)
if parsed_args.out: if os.path.exists(parsed_args.out): msg = "The output file: {} already exists.".format(parsed_args.out) log.error(msg) raise RuntimeError(msg)
if parsed_args.dpi: fig.savefig(parsed_args.out, dpi=parsed_args.dpi) fig.savefig(parsed_args.out) else: plt.show()
main() |