Comments (16)
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.
Hey did it work for you ? Can I close the issue ?
from dnafeaturesviewer.
Hey Zulko, havent had chance to try it yet but its given me some ideas - feel free to close if you like!
from dnafeaturesviewer.
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.
@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.
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.
I think its because you need to upgrade DnaFeaturesViewer to the latest version.
pip install --upgrade dna_features_viewer
from dnafeaturesviewer.
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.
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.
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.
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.
Thanks! Looking forward to it :D
from dnafeaturesviewer.
Can you confirm that you see the exact same error when you run the code above?
from dnafeaturesviewer.
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.
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.
Brilliant, that works like a dream!
Thanks!
from dnafeaturesviewer.
Related Issues (20)
- Change shape of feature arrows HOT 2
- Cannot apply sequence translation on plot_on_multiple_lines or plot_on_multiple_pages HOT 2
- BiopythonTranslator: SeqFeatures with location_operator='join' get wrong position
- Labels overlap whan sharing axis HOT 2
- Feature request: x_lim best-fit HOT 6
- Enhanced Bokeh support for multiple plots HOT 2
- Linking exon annotations with intron lines, and other things. HOT 5
- Add an example which plots sequence features not starting from 0 HOT 5
- truncated scaffolds from gff files HOT 4
- figure is not coming
- type hints support HOT 1
- Error on BioPython v1.8.0 HOT 9
- type error for global variable related to BioPython update HOT 2
- sequences display from 1 not 0 ? HOT 1
- How to solve the memory problem of batch plotting? HOT 3
- Overlapping arrows in circular DNA HOT 1
- CircularGraphicRecord does not work. Unable to make circular graphs HOT 1
- UndefinedSequenceError with gff files and BioPython 1.81
- Oval or diamond/octagon in addition to arrows for displayed features? HOT 1
- 'Seq' object has no attribute 'defined' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dnafeaturesviewer.