Make last-dotplot try to find a nice font
authorMartin C. Frith
Wed Nov 08 11:07:22 2017 +0900 (2017-11-08)
changeset 9033af6b02e3f9c
parent 902 db84a7e4ff8a
child 904 c2f50cd4e29d
Make last-dotplot try to find a nice font
doc/last-dotplot.txt
scripts/last-dotplot
     1.1 --- a/doc/last-dotplot.txt	Wed Nov 08 10:01:05 2017 +0900
     1.2 +++ b/doc/last-dotplot.txt	Wed Nov 08 11:07:22 2017 +0900
     1.3 @@ -12,16 +12,12 @@
     1.4  
     1.5    last-dotplot alns alns.gif
     1.6  
     1.7 -To get a nicer font, try something like::
     1.8 +Terminology
     1.9 +-----------
    1.10  
    1.11 -  last-dotplot -f /usr/share/fonts/liberation/LiberationSans-Regular.ttf alns alns.png
    1.12 -
    1.13 -or::
    1.14 -
    1.15 -  last-dotplot -f /Library/Fonts/Arial.ttf alns alns.png
    1.16 -
    1.17 -If the fonts are located somewhere different on your computer, change
    1.18 -this as appropriate.
    1.19 +last-dotplot shows alignments of one set of sequences against another
    1.20 +set of sequences.  This document calls a "set of sequences" a
    1.21 +"genome", though it need not actually be a genome.
    1.22  
    1.23  Choosing sequences
    1.24  ------------------
    1.25 @@ -59,6 +55,15 @@
    1.26  
    1.27    last-dotplot -1 chr9:0-1000 alns alns.png
    1.28  
    1.29 +Fonts
    1.30 +-----
    1.31 +
    1.32 +last-dotplot tries to find a nice font on your computer, but may fail,
    1.33 +in which case it will use an ugly font.  You can specify a font like
    1.34 +this::
    1.35 +
    1.36 +  last-dotplot -f /usr/share/fonts/liberation/LiberationSans-Regular.ttf alns alns.png
    1.37 +
    1.38  Options
    1.39  -------
    1.40  
    1.41 @@ -172,7 +177,7 @@
    1.42  pixel.
    1.43  
    1.44  Colors
    1.45 -~~~~~~
    1.46 +------
    1.47  
    1.48  Colors can be specified in `various ways described here
    1.49  <http://effbot.org/imagingbook/imagecolor.htm>`_.
     2.1 --- a/scripts/last-dotplot	Wed Nov 08 10:01:05 2017 +0900
     2.2 +++ b/scripts/last-dotplot	Wed Nov 08 11:07:22 2017 +0900
     2.3 @@ -11,6 +11,7 @@
     2.4  
     2.5  import gzip
     2.6  from fnmatch import fnmatchcase
     2.7 +import subprocess
     2.8  import itertools, optparse, os, re, sys
     2.9  
    2.10  # Try to make PIL/PILLOW work:
    2.11 @@ -484,10 +485,29 @@
    2.12      for i, j in zip(seqNames, rangePixBegs):
    2.13          yield i, bp_per_pix * j - seqLimits[i][0]
    2.14  
    2.15 +def getFont(opts):
    2.16 +    if opts.fontfile:
    2.17 +        return ImageFont.truetype(opts.fontfile, opts.fontsize)
    2.18 +    fileNames = []
    2.19 +    try:
    2.20 +        x = ["fc-match", "-f%{file}", "arial"]
    2.21 +        p = subprocess.Popen(x, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    2.22 +        out, err = p.communicate()
    2.23 +        fileNames.append(out)
    2.24 +    except OSError as e:
    2.25 +        warn("fc-match error: " + str(e))
    2.26 +    fileNames.append("/Library/Fonts/Arial.ttf")  # for Mac
    2.27 +    for i in fileNames:
    2.28 +        try:
    2.29 +            font = ImageFont.truetype(i, opts.fontsize)
    2.30 +            warn("font: " + i)
    2.31 +            return font
    2.32 +        except IOError as e:
    2.33 +            warn("font load error: " + str(e))
    2.34 +    return ImageFont.load_default()
    2.35 +
    2.36  def lastDotplot(opts, args):
    2.37 -    if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    2.38 -    else:              font = ImageFont.load_default()
    2.39 -
    2.40 +    font = getFont(opts)
    2.41      image_mode = 'RGB'
    2.42      forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    2.43      reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    2.44 @@ -632,7 +652,7 @@
    2.45      og = optparse.OptionGroup(op, "Text options")
    2.46      og.add_option("-f", "--fontfile", metavar="FILE",
    2.47                    help="TrueType or OpenType font file")
    2.48 -    og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
    2.49 +    og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=14,
    2.50                    help="TrueType or OpenType font size (default: %default)")
    2.51      og.add_option("--labels1", type="int", default=0, metavar="N", help=
    2.52                    "genome1 labels: 0=name, 1=name:length, "
    2.53 @@ -687,6 +707,6 @@
    2.54  
    2.55      try: lastDotplot(opts, args)
    2.56      except KeyboardInterrupt: pass  # avoid silly error message
    2.57 -    except Exception, e:
    2.58 +    except Exception as e:
    2.59          prog = os.path.basename(sys.argv[0])
    2.60          sys.exit(prog + ": error: " + str(e))