gecrooks / weblogo

WebLogo 3: Sequence Logos redrawn
weblogo.threeplusone.com
Other
146 stars 39 forks source link

Center logo if **--show-yaxis no** #74

Closed ghuls closed 6 years ago

ghuls commented 6 years ago

If --show-yaxis is set to no, the left margin was set to zero, while the right margin was set to font_size.

./weblogo \
    --sequence-type dna \
    --datatype clusterbuster \
    --format png \
    --color-scheme classic \
    --fineprint '' \
    --stack-width 15.2222222222 \
    --aspect-ratio 6.6021897810 \
    --fontsize 11 \
    --logo-font Helvetica-Narrow \
    --show-xaxis no \
    --show-yaxis no \
    --show-ends no \
    --errorbars no \
    --fin jaspar__MA0060.2.transfac \
    --fout jaspar__MA0060.2.png

If --show-ends is specified and --show-yaxis no, the left margin is now set to the same value as the right margin (fontsize * 1.5), so the logo is centered in the middle.

The following patch always centeres the logo if --show-yaxis no:

$ git diff weblogolib/__init__.py
diff --git a/weblogolib/__init__.py b/weblogolib/__init__.py
index 7c8cdef..5916688 100755
--- a/weblogolib/__init__.py
+++ b/weblogolib/__init__.py
@@ -626,7 +626,10 @@ class LogoFormat(LogoOptions):
         if self.show_yaxis:
             self.line_margin_left = self.fontsize * 3.0
         else:
-            self.line_margin_left = 0
+            if self.show_ends and self.show_xaxis:
+                self.line_margin_left = self.fontsize * 1.5
+            else:
+                self.line_margin_left = self.fontsize

         if self.show_ends:
             self.line_margin_right = self.fontsize * 1.5
go-bears commented 6 years ago

thanks @ghuls ! for the tip to help WebLogo users with controlling WebLogo formatting!

ghuls commented 6 years ago

A better patch:

diff --git a/weblogolib/__init__.py b/weblogolib/__init__.py
index 7c8cdef..78b9e55 100755
--- a/weblogolib/__init__.py
+++ b/weblogolib/__init__.py
@@ -626,12 +626,15 @@ class LogoFormat(LogoOptions):
         if self.show_yaxis:
             self.line_margin_left = self.fontsize * 3.0
         else:
-            self.line_margin_left = 0
+            if self.show_ends and self.show_xaxis:
+                self.line_margin_left = self.fontsize * 1.5
+            else:
+                self.line_margin_left = 4

-        if self.show_ends:
+        if self.show_ends and self.show_xaxis:
             self.line_margin_right = self.fontsize * 1.5
         else:
-            self.line_margin_right = self.fontsize
+            self.line_margin_right = 4
# Current weblogo
./weblogo \
    --sequence-type dna \
    --datatype transfac \
    --format png \
    --color-scheme classic \
    --fineprint '' \
    --show-xaxis no \
    --show-yaxis no \
    --errorbars no \
    --fin jaspar__MA0171.1.transfac \
    --fout jaspar__MA0171.1.weblogo_unpatched.png 

# Add border to see the difference:
convert -border 1 -bordercolor black jaspar__MA0171.1.weblogo_unpatched.png jaspar__MA0171.1.weblogo_unpatched.border.png
\
# Patched weblogo
./weblogo \
    --sequence-type dna \
    --datatype transfac \
    --format png \
    --color-scheme classic \
    --fineprint '' \
    --show-xaxis no \
    --show-yaxis no \
    --errorbars no \
    --fin jaspar__MA0171.1.transfac \
    --fout jaspar__MA0171.1.weblogo_patched.png

# Add border to see the difference:
convert -border 1 -bordercolor black jaspar__MA0171.1.weblogo_patched.png jaspar__MA0171.1.weblogo_patched.border.png

Unpatched logo: jaspar__ma0171 1 weblogo_unpatched border

Patched logo: jaspar__ma0171 1 weblogo_patched border

go-bears commented 6 years ago

Thanks @ghuls ! Great work in keeping the logo centered with the patch. Appreciate the time and effort!

ghuls commented 6 years ago

@go-bears Is it still a wontfix, or do you accept a pull request?

go-bears commented 6 years ago

I removed the wontfix label @ghuls. If you add a test for your patch, I think it can be accepted as a PR.

ghuls commented 6 years ago

@go-bears How can an add a test? Is there a method in https://github.com/WebLogo/weblogo/blob/master/test_weblogo.py, which I can mimic? And what exactly needs to be tested?

gecrooks commented 6 years ago

Pull request tested and accepted. Thanks.

go-bears commented 6 years ago

thanks @ghuls for all your work!