def __init__(self, output):
        assert output in SUPPORTED_FORMATS, f"supported formats are {SUPPORTED_FORMATS}"
        self.read_encoding = "utf-8"
        self.write_encoding = "utf-8"
        self.format = output

        if self.format == "pdf":
            pdf = PDFExporter()
            pdf.exclude_output_prompt = True
            pdf.exclude_input = True
            self.exporter = pdf
        elif self.format == "rst":
            self.exporter = RSTExporter()
        else:
            self.exporter = MarkdownExporter()
Example #2
0
    def __init__(self, output_format, destination_mode):
        assert output_format in SUPPORTED_FORMATS, f"supported formats are {SUPPORTED_FORMATS}"
        assert (
            destination_mode in SUPPORTED_DESTINATIONS_MODES
        ), f"supported destination modes are {SUPPORTED_DESTINATIONS_MODES}"

        self.read_encoding = "utf-8"
        self.write_encoding = "utf-8"
        self.format = output_format
        self.destination_mode = destination_mode

        if self.format == "pdf":
            pdf = PDFExporter()
            pdf.exclude_output_prompt = True
            pdf.exclude_input = True
            self.exporter = pdf
        elif self.format == "rst":
            self.exporter = RSTExporter()
        else:
            self.exporter = MarkdownExporter()
def generate_report_notebook(config, job_type, data_dir, processed_data_dir,
                             genome_dirpath, filenames, report_filename):
    file_loader = FileSystemLoader(os.path.join(ROOT_DIR, 'IDSort/src'))
    env = Environment(loader=file_loader)
    report_template = env.get_template('genome_report_template.ipynb')

    if job_type == 'sort':
        # convert given genomes to h5 files, and convert given inp files to
        # genomes and then to h5 files

        inp_to_genome_config = {
            'analysis': False,
            'readable': False,
            'create_genome': True,
            'id_filename': config['process_genome']['id_filename'],
            'magnets_filename': config['process_genome']['magnets_filename'],
            'id_template': config['process_genome']['id_template']
        }
        genome_to_h5_config = {
            'analysis': True,
            'readable': True,
            'create_genome': False,
            'id_filename': config['process_genome']['id_filename'],
            'magnets_filename': config['process_genome']['magnets_filename'],
            'id_template': config['process_genome']['id_template']
        }

        genome_h5_filepaths = []
        for filename in filenames:
            if filename.endswith('.genome'):
                filepath = os.path.join(genome_dirpath, filename)
                run_process_genome(genome_to_h5_config, filepath,
                                   processed_data_dir)
                genome_h5_filepaths.append(
                    os.path.join(processed_data_dir, filename + '.h5'))
            elif filename.endswith('.inp'):
                filepath = os.path.join(processed_data_dir, filename)
                run_process_genome(inp_to_genome_config, filepath,
                                   genome_dirpath)
                new_genome_filename = filename + '.genome'
                new_genome_filepath = os.path.join(genome_dirpath,
                                                   new_genome_filename)
                run_process_genome(genome_to_h5_config, new_genome_filepath,
                                   processed_data_dir)
                genome_h5_filepaths.append(
                    os.path.join(processed_data_dir,
                                 new_genome_filename + '.h5'))
    elif job_type == 'shim':
        genome_h5_filepaths = [
            os.path.join(genome_dirpath, filename) for filename in filenames
        ]

    report_output = report_template.render(
        job_type=job_type, genome_h5_filepaths=genome_h5_filepaths)

    notebook_name = 'genome_report.ipynb'
    notebook_path = os.path.join(data_dir, notebook_name)

    with open(notebook_path, 'w') as notebook:
        notebook.write(report_output)

    # create dir for genome reports
    genome_report_dir = 'genome_reports/'
    genome_report_dirpath = os.path.join(data_dir, genome_report_dir)
    if not os.path.exists(genome_report_dirpath):
        os.makedirs(genome_report_dirpath)

    # execute notebook
    with open(notebook_path, 'r') as notebook:
        nb = nbformat.read(notebook, as_version=4)

    ep = ExecutePreprocessor()
    ep.preprocess(nb, {'metadata': {'path': data_dir}})
    pdf_exporter = PDFExporter()
    pdf_exporter.exclude_output_prompt = True
    pdf_exporter.exclude_input = True
    pdf_data, resources = pdf_exporter.from_notebook_node(nb)

    # check the name of the report to see if a specific name has been given or
    # not
    if report_filename == 'genome_report.pdf':
        # concatenate all genome/inp files using "_" as a separator to form the
        # filename
        report_filename = '_'.join(filenames) + '.pdf'

    report_filepath = os.path.join(genome_report_dirpath, report_filename)
    with open(report_filepath, 'wb') as report:
        report.write(pdf_data)