示例#1
0
def run_qc(gid,
           uid,
           r1,
           r2=None,
           adapter=None,
           contaminants=None,
           exclude=[],
           kmers=8,
           out_dir=".",
           threads=1):
    """Creates output directory tree, handles JSON creation and updating when running QC, and
    executes FastQC.

    Args:
        gid (str): group, batch, or experiment ID
        uid (str): unique ID to this group, e.g. run ID or sample ID
        r1 (str): file path to R1 FASTQ file
        r2 (Optional[str]): file path to R2 FASTQ file
        adapter (Optional[str]): file path to optional adapters FASTA
        contaminants (Optional[str]): file path to optional contaminants FASTA
        exclude (Optional[list]): tab name list to exclude from JSON
        kmers (Optional[int]): the length of kmer for which to look
        out_dir (Optional[str]): parent directory of server; holds 'plot_data' and child folders
        threads (Optional[str]): number of threads to use
    """
    qc_obj = Fastqc(gid, uid, r1, r2=r2, out_dir=out_dir)
    logging.info("Running FastQC")
    # executes fastqc and writes data tables
    qc_obj.run(adapter=adapter,
               contaminants=contaminants,
               kmers=kmers,
               threads=threads)
    # exclude user requested tabs
    tabs = []
    for filename, web_tab in qc_obj.tabs.items():
        if exclude and web_tab.tab_name in exclude:
            logging.info("Omitting tab '%s'" % web_tab.tab_name)
        else:
            tabs.append(web_tab.to_dict())
    # writes config for this uid
    with open(os.path.join(qc_obj.out_dir, "config.json"), 'w') as fh:
        print(json.dumps(tabs, indent=4), file=fh)
    # update groups json
    group_config = os.path.join(
        qc_obj.out_dir.partition(gid)[0], "groups.json")
    logging.debug("Updating Group config: %s" % group_config)
    group_data = import_json(group_config)
    # if existing group
    if gid in [i['group_id'] for i in group_data]:
        for i in range(len(group_data)):
            if group_data[i]['group_id'] == gid:
                if uid not in group_data[i]['uids']:
                    # update UID list
                    insort_left(group_data[i]['uids'], uid)
    else:
        group_data.insert(0, {'group_id': gid, 'uids': [uid]})
    with open(group_config, 'w') as fh:
        print(json.dumps(group_data, indent=4, sort_keys=True), file=fh)
    logging.info("Processing of %s complete" % uid)
示例#2
0
文件: fqc.py 项目: xtmgah/fqc
def run_add(config,
            name,
            plot_type,
            csv,
            prepend=False,
            status=None,
            x_value=None,
            y_value=None,
            x_label=None,
            y_label=None,
            subtitle=None,
            lower_quartile=None,
            upper_quartile=None,
            mean=None,
            value=None,
            label=None,
            colors=None,
            step=10,
            min_value=None,
            min_color=None,
            mid_color=None,
            max_value=None,
            max_color=None):
    """Copies the CSV into the directory containing the config file if it does not already exist
    there, then either appends an appropriate entry onto the existing config JSON or creates a new
    file.

    Args:
        config (str): file path to config.json
        name (str): name being used in the tab on the dashboard
        plot_type (str): type of chart being created
        csv (str): file path to data file being added to dashboard
        prepend (Optional[bool]): add new plot as first tab in dashboard
        status (Optional[str]): image icon to be displayed on tab [None, 'warn', 'pass', 'fail']
        x_value (Optional[str]): header label in CSV containing the x-values
        y_value (Optional[str]): header label in CSV containing the y-values
        x_label (Optional[str]): x-label to be drawn on dashboard; defaults to `x_value`
        y_label (Optional[str]): y-label to be drawn on dashboard; defaults to `y_value`
        subtitle (Optional[str]): subtitle to be drawn on dashboard
        lower_quartile (Optional[str]): arearange specific option; header label in CSV for lower
            quartile value
        upper_quartile (Optional[str]): arearange specific option; header label in CSV for upper
            quartile value
        mean (Optional[str]): arearange specific option; header label in CSV for mean value
        value (Optional[str]): value label in CSV to be plotted in heatmap
        label (Optional[str]): optional heatmap label header to mark individual coordinates
        colors (Optional[str]): optional color definitions for observable labels
        step (Optional[int]): histogram step
        min_value (Optional[int]): optional heatmap minimum value threshold for color map
        min_color (Optional[str]): optional heatmap minimum color
        mid_color (Optional[str]): optional heatmap mid color for color map
        max_value (Optional[int]): optional heatmap maximum value threshold for color map
        max_color (Optional[str]): optional heatmap maximum color
    """
    filename = add_csv_input(csv, os.path.dirname(config))
    if colors:
        # '(-)CTRL:#1f77b4,(+)CTRL:#d62728'
        try:
            colors_dict = dict([i.split(":") for i in colors.split(",")])
        except ValueError:
            logging.warning("Unable to parse colors (%s) in key:value pairs" %
                            colors)
            colors_dict = None
        colors = colors_dict

    web_tab = WebTab(
        filename, name, status,
        ChartProperties(plot_type,
                        subtitle,
                        x_label,
                        x_value,
                        y_label,
                        y_value,
                        lower_quartile,
                        upper_quartile,
                        mean,
                        value,
                        label,
                        minimum=min_value,
                        maximum=max_value,
                        min_color=min_color,
                        mid_color=mid_color,
                        max_color=max_color,
                        colors=colors,
                        step=step))

    # append onto configuration file
    if os.path.exists(config):
        shutil.copy(config, config + '.bak')
        logging.info("Saving backup: %s" % config + '.bak')
        tab_data = import_json(config)
        if prepend:
            tab_data.insert(0, web_tab.to_dict())
        else:
            tab_data.append(web_tab.to_dict())
        try:
            with open(config, 'w') as fh:
                print(json.dumps(tab_data, indent=4), file=fh)
        except Exception:
            shutil.move(config + '.bak', config)
            logging.exception("The backup config has been restored.")
            raise
    # create new
    else:
        with open(config, 'w') as fh:
            print(json.dumps([entry], indent=4), file=fh)
    logging.info("Added entry into %s\n%s" %
                 (config, json.dumps(web_tab.to_dict(), indent=4)))