コード例 #1
0
def test_from_genbank_to_circular(tmpdir):
    translator = BiopythonTranslator()
    graphic_record = translator.translate_record(
        example_genbank, record_class=CircularGraphicRecord)
    ax, _ = graphic_record.plot(figure_width=7)
    ax.figure.tight_layout()
    target_file = os.path.join(str(tmpdir), "from_genbank.png")
    ax.figure.savefig(target_file)
コード例 #2
0
def test_multiline_plot():

    translator = BiopythonTranslator()
    graphic_record = translator.translate_record(example_genbank)
    subrecord = graphic_record.crop((1700, 2200))
    fig, axes = subrecord.plot_on_multiple_lines(nucl_per_line=100,
                                                 figure_width=12,
                                                 plot_sequence=True)
    assert 9.5 < fig.get_figheight() < 10
コード例 #3
0
def test_multipage_plot(tmpdir):
    translator = BiopythonTranslator()
    graphic_record = translator.translate_record(example_genbank)
    subrecord = graphic_record.crop((1800, 2750))
    subrecord.plot_on_multiple_pages(
        pdf_target=os.path.join(str(tmpdir), "test.pdf"),
        nucl_per_line=70,
        lines_per_page=7,
        plot_sequence=True,
    )
コード例 #4
0
def test_multipage_plot_with_translation(tmpdir):
    # Github issue 61
    translator = BiopythonTranslator()
    graphic_record = translator.translate_record(example_genbank)
    subrecord = graphic_record.crop((1800, 2750))
    translation_params = {
        "location": (1830, 1890),
        "fontdict": {
            "weight": "bold"
        },
        "long_form_translation": False,
    }
    subrecord.plot_on_multiple_pages(
        pdf_target=os.path.join(str(tmpdir), "test_translation.pdf"),
        nucl_per_line=66,
        lines_per_page=7,
        plot_sequence=True,
        translation_params=translation_params,
    )
コード例 #5
0
    def redraw(self, start=1, end=2000):
        """Plot the features"""

        import matplotlib
        import pylab as plt
        from dna_features_viewer import GraphicFeature, GraphicRecord
        from dna_features_viewer import BiopythonTranslator

        ax = self.ax
        ax.clear()
        rec = self.rec
        length = len(self.rec.seq)
        if start < 0:
            start = 1
        if end <= 0:
            end = start + 2000
        if end - start > 100000:
            end = start + 100000
        if end > length:
            end = length
        rec = self.rec
        translator = BiopythonTranslator(
            features_filters=(lambda f: f.type not in ["gene", "source"], ),
            features_properties=lambda f:
            {"color": self.color_map.get(f.type, "white")},
        )
        #print (start, end, length)
        graphic_record = translator.translate_record(rec)
        cropped_record = graphic_record.crop((start, end))
        #print (len(cropped_record.features))
        cropped_record.plot(strand_in_label_threshold=7, ax=ax)
        if end - start < 150:
            cropped_record.plot_sequence(ax=ax, location=(start, end))
            cropped_record.plot_translation(ax=ax,
                                            location=(start, end),
                                            fontdict={'weight': 'bold'})
        plt.tight_layout()
        self.canvas.draw()
        self.view_range = end - start
        self.loclbl.setText(str(start) + '-' + str(end))
        return
コード例 #6
0
def gene_plot(gbk_file, **kwargs):
    """Create gene feature plot."""
    color_map = {
        "rep_origin": "yellow",
        "CDS": Colors.cerulean,
        "regulatory": "red",
        "rRNA": Colors.light_cornflower_blue,
        "misc_feature": "lightblue",
    }
    translator = BiopythonTranslator(
        features_filters=(lambda f: f.type not in ["gene", "source"], ),
        features_properties=lambda f:
        {"color": color_map.get(f.type, "white")})
    record = translator.translate_record(gbk_file)
    ax, _ = record.plot(figure_width=300,
                        strand_in_label_threshold=30,
                        **kwargs)
    encoded = fig_to_base64(ax.figure)
    plot = '<pre><img src="data:image/png;base64, {}"></pre>'.format(
        encoded.decode('utf-8'))
    return plot
    label = None
    if f.type == "Mutagenesis":
        label = f.qualifiers["Note"][0]
    color = {
        "Mutagenesis": "firebrick",
        "Active site": "yellow",
        "Beta strand": "lightyellow",
        "Chain": "lightcyan",
        "Helix": "honeydew",
        "Initiator methionine": "white",
        "Metal binding": "lightsteelblue",
        "Turn": "moccasin",
    }.get(f.type, "white")
    return dict(color=color, label=label)


# GET THE RECORD FROM UNIPROT

response = urllib.request.urlopen("https://www.uniprot.org/uniprot/P0A7B8.gff")
record_file = StringIO(response.read().decode())

# TRANSLATE AND PLOT THE RECORD

translator = BiopythonTranslator(features_properties=features_properties)
graphic_record = translator.translate_record(record_file)
ax, _ = graphic_record.plot(
    figure_width=15, max_label_length=100, elevate_outline_annotations=True,
)
ax.set_title("Mutation effects in P0A7B8", fontweight="bold", fontsize=16)
ax.figure.savefig("gff_record_from_the_web.png", bbox_inches="tight")
コード例 #8
0
def plot_sequence_sites(
    sequence,
    enzymes_names,
    forbidden_enzymes=(),
    unique_sites=True,
    ax=None,
    figure_width=18,
    annotate_inline=True,
):
    """Plot the location of sites in the sequence.

    Non-unique and forbidden sites can be highlighted in red.

    Parameters
    ----------

    sequence
      The sequence of interest. ATGC string.

    enzymes_names
      List of names of the enzymes to plot.

    forbidden_enzymes
      The sites of these enzymes will also be plotted, but with a red
      background.

    unique_sites
      If true, for each enzyme in enzyme_name with more than one site in
      the sequence, these will be plotted on a red background.

    ax
      Matplotlib ax on which to draw the figure. If none is provided a new
      figure is created and the ax is returned at the end.

    figure_width
      Width of the figure if no ax is provided and a new figure is returned.

    annotate_inline
      If True, the enzyme names will be written inside the annotations
      when possible, instead of above.
    """

    record = annotate_enzymes_sites(
        sequence,
        enzymes_names,
        forbidden_enzymes=forbidden_enzymes,
        unique_sites=unique_sites,
    )
    default_props = dict(
        thickness=10,
        box_color=None,
        fontdict=dict(family="Impact", size=7, color="black", weight="normal"),
    )
    translator = BiopythonTranslator(
        features_properties=lambda f: default_props)
    graphic_record = translator.translate_record(record)
    graphic_record.labels_spacing = 1
    ax, _ = graphic_record.plot(figure_width=figure_width,
                                annotate_inline=annotate_inline,
                                ax=ax)
    return ax
コード例 #9
0
ファイル: PlotSequenceFeatures.py プロジェクト: jeqka24/CUBA
    def work(self):
        self.logger(message="Reading Data...")
        data = self.data

        must_contain = [
            s.strip() for s in data.must_contain.split(',') if s.strip() != ''
        ]
        must_not_contain = [
            s.strip() for s in data.must_not_contain.split(',')
            if s.strip() != ''
        ]
        filter_feature_types = [f.lower() for f in data.keep_or_discard_types]

        def feature_text(f):
            return ", ".join([str(v) for v in f.qualifiers.values()])

        def feature_filter(f):
            ftype = f.type.lower()
            keep = data.keep_or_discard == 'keep'
            if filter_feature_types != []:
                in_types = ftype in filter_feature_types
                if (keep and not in_types) or (in_types and not keep):
                    return False
            text = feature_text(f)
            if len(must_contain) and not any([c in text
                                              for c in must_contain]):
                return False
            if len(must_not_contain) and any(
                [c in text for c in must_not_contain]):
                return False
            return True

        def features_properties(f):
            properties = {
                'color': data.default_color,
                'linewidth': data.default_thickness
            }
            if not data.default_display_label:
                properties['label'] = None
            ftype = f.type.lower()
            for fl in data.custom_styles:
                keep = fl.keep_or_discard == 'keep'
                if (fl.selector == 'text'):
                    has_term = fl.feature_text in feature_text(f)
                    if (keep and has_term) or ((not keep) and (not has_term)):
                        properties['color'] = fl.color
                        properties['linewidth'] = fl.thickness
                        if fl.display_label:
                            properties.pop('label', '')
                if (fl.selector == 'type'):
                    is_type = (ftype == fl.feature_type.lower())
                    if (keep and is_type) or ((not keep) and (not is_type)):
                        properties['color'] = fl.color
                        properties['linewidth'] = fl.thickness
                        if fl.display_label:
                            properties.pop('label', '')
            return properties

        display_class = {
            'linear': GraphicRecord,
            'circular': CircularGraphicRecord
        }[data.display]

        translator = BiopythonTranslator(
            features_filters=(feature_filter, ),
            features_properties=features_properties)
        records = records_from_data_files(data.files)
        figures = []
        for rec in self.logger.iter_bar(record=records):
            gr = translator.translate_record(rec, record_class=display_class)
            if not data.plot_full_sequence:
                gr = gr.crop((data.plot_from_position, data.plot_to_position))
            ax, _ = gr.plot(figure_width=data.plot_width,
                            with_ruler=data.plot_ruler,
                            annotate_inline=data.inline_labels)
            if data.plot_nucleotides:
                gr.plot_sequence(ax)
            figure = ax.figure
            figure.suptitle(rec.id)
            figures.append(figure)

        if data.pdf_report:
            pdf_io = BytesIO()

            with PdfPages(pdf_io) as pdf:
                for fig in figures:
                    pdf.savefig(fig, bbox_inches="tight")

            pdf_data = ('data:application/pdf;base64,' +
                        b64encode(pdf_io.getvalue()).decode("utf-8"))
            figures_data = {
                'data': pdf_data,
                'name': 'sequence_feature_plots.pdf',
                'mimetype': 'application/pdf'
            }
        else:
            figures_data = []
            for _file, fig in zip(data.files, figures):
                figdata = matplotlib_figure_to_svg_base64_data(
                    fig, bbox_inches="tight")
                figures_data.append({
                    'img_data': figdata,
                    'filename': _file.name
                })

        return {
            'pdf_report': None if not data.pdf_report else figures_data,
            'figures_data': None if data.pdf_report else figures_data
        }
コード例 #10
0
from dna_features_viewer import BiopythonTranslator

start, end = 1300, 2700


def feature_properties(f):
    """Fade away all features not overlapping with [start, end]"""
    if f.location.end < start or f.location.start > end:
        return dict(color="white", linecolor="grey", label=None)
    return {}


translator = BiopythonTranslator(features_properties=feature_properties)
graphic_record = translator.translate_record("example_sequence.gb")
ax, _ = graphic_record.plot(figure_width=12, elevate_outline_annotations=True)
ax.fill_between([start, end],
                -10,
                10,
                facecolor="peachpuff",
                alpha=0.2,
                zorder=-1)

ax.figure.savefig('locally_highlighted_record.png', bbox_inches='tight')
コード例 #11
0
record = SeqIO.read(handle, "genbank")

# CREATE THE GRAPHIC RECORD WITH DNA_FEATURES_VIEWER

color_map = {
    'rep_origin': 'yellow',
    'CDS': 'orange',
    'regulatory': 'red',
    'misc_recomb': 'darkblue',
    'misc_feature': 'lightblue',
}
translator = BiopythonTranslator(
    features_filters=(lambda f: f.type not in ['gene', 'source'], ),
    features_properties=lambda f: {'color': color_map.get(f.type, 'white')})
translator.max_line_length = 15
graphic_record = translator.translate_record(
    record, record_class=CircularGraphicRecord)
graphic_record.labels_spacing = 30

# ANIMATE INTO A GIF WITH MOVIEPY

duration = 5


def make_frame(t):
    top_nucleotide_index = t * graphic_record.sequence_length / duration
    graphic_record.top_position = top_nucleotide_index
    ax, _ = graphic_record.plot(figure_width=8)
    np_image = mplfig_to_npimage(ax.figure)
    plt.close(ax.figure)
    return np_image