Code Monkey home page Code Monkey logo

Comments (16)

Zulko avatar Zulko commented on July 21, 2024 1

Hey there,

I agree more examples would be a good thing. There are several possible answers to your questions.

First, keep in mind that the feature objects in the Biotranslator refer to Biopython Feature objects. So the Biopython docs will tell you everything about their structure.

Also, if a feature has qualifiers "color" or "label", these will be used by default by the BioPythonTranslator (unless you have a custom BiopythonTranslator where you overwrite this behavior). That means that instead of putting your logics in your custom translator, you can also pre-process your biopython record before you feed it to the translator:

seqrecord = snapgene_file_to_seqrecord(args.infile)#
for feature in seqrecord:
    feature.qualifiers["color"] = 'blue'
    # ... implement other rules
graphic_record =Translator().translate_record(args.seqfile)

This being said, here are some ways of doing what you want from the Translator:

For the gene product, I would do it as follows:

def compute_feature_label(self, feature):
    if feature.type == "CDS":
        return feature.qualifiers["product"]
        # or even more robust, will handle the case with 0 or 2+ products:
        return " ".join(feature.qualifiers.get("product", [""]))

For iterating through colors, i would do it as follows:

from itertools import cycle
color_iterator = cycle(['blue', 'red', 'green', 'purple'])

def compute_feature_color(self, feature):
        if feature.type == "CDS":
            return next(color_iterator)

also have a look at matplotlib.colors for ways of generating color palettes.

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

Hey did it work for you ? Can I close the issue ?

from dnafeaturesviewer.

jrjhealey avatar jrjhealey commented on July 21, 2024

Hey Zulko, havent had chance to try it yet but its given me some ideas - feel free to close if you like!

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Hi there,
this is what I am looking for (the "For the gene product, I would do it as follows:" part)

I attempted to do:


def compute_feature_label(self, feature):
    if feature.type == "CDS":
        return feature.qualifiers["product"]

graphic_record = BiopythonTranslator().translate_record("/srv/projects/coral/Accelerate_project/analyses/20190408_prokka_VirSorter_assemblies/df50_prokka_output/146assemblyVIRSorter_k141_30585_flag=3_multi=368_0000_len=9522-circular-cat_1/PROKKA_05142019.gbk")


compute_feature_label(graphic_record, 'CDS')


ax, _ = graphic_record.plot(figure_width=10)

Here I get the error message:


AttributeError Traceback (most recent call last)
in
1 graphic_record = BiopythonTranslator().translate_record("/srv/projects/coral/Accelerate_project/analyses/20190408_prokka_VirSorter_assemblies/df50_prokka_output/146assemblyVIRSorter_k141_30585_flag=3_multi=368_0000_len=9522-circular-cat_1/PROKKA_05142019.gbk")
----> 2 compute_feature_label(graphic_record, 'CDS')
3 ax, _ = graphic_record.plot(figure_width=10)

in compute_feature_label(self, feature)
1 def compute_feature_label(self, feature):
----> 2 if feature.type == "CDS":
3 return feature.qualifiers["product"]
4

AttributeError: 'str' object has no attribute 'type'

I'm not sure what to do next, it would be great to have an example of how to alter the labels of the graphic_record object, I think many bioinformaticians/biologists would find this tool extremely useful if so.

M.

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

@MeggyC Follow the example at the end of the README: you must define a custom Translator class, not simply a compute_feature_label method. It should look like this:

class CustomTranslator(BiopythonTranslator):
    def compute_feature_label(self, feature):
        if feature.type == "CDS":
            return feature.qualifiers["product"]
        else:
            # Return the default label:
            return BiopythonTranslator.compute_feature_label(self, feature)

path = "/srv/projects/coral/Accelerate_project/analyses/20190408_prokka_VirSorter_assemblies/df50_prokka_output/146assemblyVIRSorter_k141_30585_flag=3_multi=368_0000_len=9522-circular-cat_1/PROKKA_05142019.gbk"
translator = CustomTranslator()
graphic_record = translator.translate_record(path)
ax, _ = graphic_record.plot(figure_width=10)

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Hey there @Zulko - thanks for the explanation! Running precisely what you gave me flags up the following error:

AttributeError Traceback (most recent call last)
in
9 translator = CustomTranslator()
10 graphic_record = translator.translate_record(path)
---> 11 ax, _ = graphic_record.plot(figure_width=10)

~/.conda/envs/Megan_environment/lib/python3.6/site-packages/dna_features_viewer/GraphicRecord.py in plot(self, ax, figure_width, draw_line, with_ruler, plot_sequence, annotate_inline, level_offset, x_lim)
270 ax=ax, feature=feature, level=level
271 )
--> 272 nlines = len(feature.label.split("\n"))
273 line_height = height / nlines
274 # Hey look, a magic number!

AttributeError: 'BiopythonTranslator' object has no attribute 'split'

If you would like to troubleshoot using my file then I have attached it :D (file type changed to txt via simple rename as GitHub does not allow for gbk file types - )
PROKKA_05142019txt.txt

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

I think its because you need to upgrade DnaFeaturesViewer to the latest version.

pip install --upgrade dna_features_viewer

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Hey there Zulko. Unfortunately this didn't change the situation, even after the pip update.
Any hints? If it's any help I'm running python 3 from within a linux shell.

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

Oh sorry for that, there was an error in the example I gave above. I corrected it but here it is again just in case:

class CustomTranslator(BiopythonTranslator):
    def compute_feature_label(self, feature):
        if feature.type == "CDS":
            return feature.qualifiers["product"]
        else:
            # Return the default label:
            return BiopythonTranslator.compute_feature_label(self, feature)

path = "/srv/projects/coral/Accelerate_project/analyses/20190408_prokka_VirSorter_assemblies/df50_prokka_output/146assemblyVIRSorter_k141_30585_flag=3_multi=368_0000_len=9522-circular-cat_1/PROKKA_05142019.gbk"
translator = CustomTranslator()
graphic_record = translator.translate_record(path)
ax, _ = graphic_record.plot(figure_width=10)

This should work (or at least solve the bug you are facing)

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Hey Zulko,
I see no change between the example from 5 days ago and the one just posted above, and therefore I get get the same error message. Hmmmm

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

The change is compute_feature_label on this line:

return BiopythonTranslator.compute_feature_label(self, feature)

Weird that it still doesnt work. I'll have another look later.

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Thanks! Looking forward to it :D

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

Can you confirm that you see the exact same error when you run the code above?

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Hey there,

It's not the same error, excuse me.

The older code returns the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-1efb7adc5581> in <module>
     12 translator = CustomTranslator()
     13 graphic_record = translator.translate_record(path)
---> 14 ax, _ = graphic_record.plot(figure_width=10)

~/.conda/envs/Megan_environment/lib/python3.6/site-packages/dna_features_viewer/GraphicRecord.py in plot(self, ax, figure_width, draw_line, with_ruler, plot_sequence, annotate_inline, level_offset, x_lim)
    270                     ax=ax, feature=feature, level=level
    271                 )
--> 272                 nlines = len(feature.label.split("\n"))
    273                 line_height = height / nlines
    274                 # Hey look, a magic number!

AttributeError: 'BiopythonTranslator' object has no attribute 'split'

Whilst the newer edit returns:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-0638680d98bb> in <module>
     13 translator = CustomTranslator()
     14 graphic_record = translator.translate_record(path)
---> 15 ax, _ = graphic_record.plot(figure_width=10)

~/.conda/envs/Megan_environment/lib/python3.6/site-packages/dna_features_viewer/GraphicRecord.py in plot(self, ax, figure_width, draw_line, with_ruler, plot_sequence, annotate_inline, level_offset, x_lim)
    270                     ax=ax, feature=feature, level=level
    271                 )
--> 272                 nlines = len(feature.label.split("\n"))
    273                 line_height = height / nlines
    274                 # Hey look, a magic number!

AttributeError: 'list' object has no attribute 'split'

from dnafeaturesviewer.

Zulko avatar Zulko commented on July 21, 2024

Ok, that must be because Biopython loads sometimes loads information as as list (['label']) rather than strings ('label'). Here is the fix:

class CustomTranslator(BiopythonTranslator):
    def compute_feature_label(self, feature):
        if feature.type == "CDS":
            return "".join(feature.qualifiers["product"])
        else:
            # Return the default label:
            return BiopythonTranslator.compute_feature_label(self, feature)

path = "/srv/projects/coral/Accelerate_project/analyses/20190408_prokka_VirSorter_assemblies/df50_prokka_output/146assemblyVIRSorter_k141_30585_flag=3_multi=368_0000_len=9522-circular-cat_1/PROKKA_05142019.gbk"
translator = CustomTranslator()
graphic_record = translator.translate_record(path)
ax, _ = graphic_record.plot(figure_width=10)

from dnafeaturesviewer.

MeggyC avatar MeggyC commented on July 21, 2024

Brilliant, that works like a dream!
Thanks!

from dnafeaturesviewer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.