Refactoring.
authorMartin C. Frith
Mon Oct 19 11:13:11 2015 +0900 (2015-10-19)
changeset 641281e6460a6a9
parent 640 374bc3ee1115
child 642 50a6e3e00d36
Refactoring.
scripts/last-dotplot
     1.1 --- a/scripts/last-dotplot	Mon Oct 19 11:09:26 2015 +0900
     1.2 +++ b/scripts/last-dotplot	Mon Oct 19 11:13:11 2015 +0900
     1.3 @@ -15,44 +15,6 @@
     1.4  try: from PIL import Image, ImageDraw, ImageFont, ImageColor
     1.5  except ImportError: import Image, ImageDraw, ImageFont, ImageColor
     1.6  
     1.7 -my_name = os.path.basename(sys.argv[0])
     1.8 -usage = """
     1.9 -  %prog --help
    1.10 -  %prog [options] last-tabular-output dotplot.png
    1.11 -  %prog [options] last-tabular-output dotplot.gif
    1.12 -  etc."""
    1.13 -parser = optparse.OptionParser(usage=usage)
    1.14 -# Replace "width" & "height" with a single "length" option?
    1.15 -parser.add_option("-x", "--width", type="int", dest="width", default=1000,
    1.16 -                  help="maximum width in pixels (default: %default)")
    1.17 -parser.add_option("-y", "--height", type="int", dest="height", default=1000,
    1.18 -                  help="maximum height in pixels (default: %default)")
    1.19 -parser.add_option("-f", "--fontfile", dest="fontfile",
    1.20 -                  help="TrueType or OpenType font file")
    1.21 -parser.add_option("-s", "--fontsize", type="int", dest="fontsize", default=11,
    1.22 -                  help="TrueType or OpenType font size (default: %default)")
    1.23 -parser.add_option("-c", "--forwardcolor", dest="forwardcolor", default="red",
    1.24 -                  help="Color for forward alignments (default: %default)")
    1.25 -parser.add_option("-r", "--reversecolor", dest="reversecolor", default="blue",
    1.26 -                  help="Color for reverse alignments (default: %default)")
    1.27 -(opts, args) = parser.parse_args()
    1.28 -if len(args) != 2: parser.error("2 arguments needed")
    1.29 -
    1.30 -if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.31 -else:              font = ImageFont.load_default()
    1.32 -
    1.33 -# Make these options too?
    1.34 -text_color = "black"
    1.35 -background_color = "white"
    1.36 -pix_tween_seqs = 2  # number of border pixels between sequences
    1.37 -border_shade = 239, 239, 239  # the shade of grey to use for border pixels
    1.38 -label_space = 5     # minimum number of pixels between axis labels
    1.39 -
    1.40 -image_mode = 'RGB'
    1.41 -forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    1.42 -reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    1.43 -overlap_color = tuple([(i+j)//2 for i, j in zip(forward_color, reverse_color)])
    1.44 -
    1.45  def tabBlocks(beg1, beg2, blocks):
    1.46      '''Get the gapless blocks of an alignment, from LAST tabular format.'''
    1.47      for i in blocks.split(","):
    1.48 @@ -124,14 +86,6 @@
    1.49          seqLengths2[chr2] = seqlen2
    1.50      return alignments, seqLengths1, seqLengths2
    1.51  
    1.52 -sys.stderr.write(my_name + ": reading alignments...\n")
    1.53 -input = fileinput.input(args[0])
    1.54 -alignments, seq_size_dic1, seq_size_dic2 = readAlignments(input)
    1.55 -sys.stderr.write(my_name + ": done\n")
    1.56 -
    1.57 -if not alignments:
    1.58 -    sys.exit(my_name + ": there are no alignments")
    1.59 -
    1.60  def natural_sort_key(my_string):
    1.61      '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
    1.62      parts = re.split(r'(\d+)', my_string)
    1.63 @@ -155,9 +109,6 @@
    1.64      margin = max(zip(*name_sizes)[1])  # maximum text height
    1.65      return seq_names, seq_sizes, name_sizes, margin
    1.66  
    1.67 -seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
    1.68 -seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
    1.69 -
    1.70  def div_ceil(x, y):
    1.71      '''Return x / y rounded up.'''
    1.72      q, r = divmod(x, y)
    1.73 @@ -178,12 +129,6 @@
    1.74          if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
    1.75      return bp_per_pix
    1.76  
    1.77 -sys.stderr.write(my_name + ": choosing bp per pixel...\n")
    1.78 -bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
    1.79 -bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
    1.80 -bp_per_pix = max(bp_per_pix1, bp_per_pix2)
    1.81 -sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
    1.82 -
    1.83  def get_seq_starts(seq_pix, pix_tween_seqs, margin):
    1.84      '''Get the start pixel for each sequence.'''
    1.85      seq_starts = []
    1.86 @@ -201,11 +146,6 @@
    1.87      tot_pix = seq_starts[-1] + seq_pix[-1]
    1.88      return seq_pix, seq_starts, tot_pix
    1.89  
    1.90 -seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
    1.91 -seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
    1.92 -seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
    1.93 -seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
    1.94 -
    1.95  def drawLineForward(hits, width, bp_per_pix, origin, beg1, beg2, size):
    1.96      while True:
    1.97          q1, r1 = divmod(beg1, bp_per_pix)
    1.98 @@ -248,11 +188,6 @@
    1.99                                  beg1, beg2, size)
   1.100      return hits
   1.101  
   1.102 -sys.stderr.write(my_name + ": processing alignments...\n")
   1.103 -hits = alignmentPixels(width, height, alignments, bp_per_pix,
   1.104 -                       seq_start_dic1, seq_start_dic2)
   1.105 -sys.stderr.write(my_name + ": done\n")
   1.106 -
   1.107  def make_label(text, text_size, range_start, range_size):
   1.108      '''Return an axis label with endpoint & sort-order information.'''
   1.109      text_width  = text_size[0]
   1.110 @@ -288,6 +223,71 @@
   1.111          draw.text(position, i[3], font=font, fill=text_color)
   1.112      return im
   1.113  
   1.114 +my_name = os.path.basename(sys.argv[0])
   1.115 +usage = """
   1.116 +  %prog --help
   1.117 +  %prog [options] last-tabular-output dotplot.png
   1.118 +  %prog [options] last-tabular-output dotplot.gif
   1.119 +  etc."""
   1.120 +parser = optparse.OptionParser(usage=usage)
   1.121 +# Replace "width" & "height" with a single "length" option?
   1.122 +parser.add_option("-x", "--width", type="int", dest="width", default=1000,
   1.123 +                  help="maximum width in pixels (default: %default)")
   1.124 +parser.add_option("-y", "--height", type="int", dest="height", default=1000,
   1.125 +                  help="maximum height in pixels (default: %default)")
   1.126 +parser.add_option("-f", "--fontfile", dest="fontfile",
   1.127 +                  help="TrueType or OpenType font file")
   1.128 +parser.add_option("-s", "--fontsize", type="int", dest="fontsize", default=11,
   1.129 +                  help="TrueType or OpenType font size (default: %default)")
   1.130 +parser.add_option("-c", "--forwardcolor", dest="forwardcolor", default="red",
   1.131 +                  help="Color for forward alignments (default: %default)")
   1.132 +parser.add_option("-r", "--reversecolor", dest="reversecolor", default="blue",
   1.133 +                  help="Color for reverse alignments (default: %default)")
   1.134 +(opts, args) = parser.parse_args()
   1.135 +if len(args) != 2: parser.error("2 arguments needed")
   1.136 +
   1.137 +if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
   1.138 +else:              font = ImageFont.load_default()
   1.139 +
   1.140 +# Make these options too?
   1.141 +text_color = "black"
   1.142 +background_color = "white"
   1.143 +pix_tween_seqs = 2  # number of border pixels between sequences
   1.144 +border_shade = 239, 239, 239  # the shade of grey to use for border pixels
   1.145 +label_space = 5     # minimum number of pixels between axis labels
   1.146 +
   1.147 +image_mode = 'RGB'
   1.148 +forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
   1.149 +reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
   1.150 +overlap_color = tuple([(i+j)//2 for i, j in zip(forward_color, reverse_color)])
   1.151 +
   1.152 +sys.stderr.write(my_name + ": reading alignments...\n")
   1.153 +input = fileinput.input(args[0])
   1.154 +alignments, seq_size_dic1, seq_size_dic2 = readAlignments(input)
   1.155 +sys.stderr.write(my_name + ": done\n")
   1.156 +
   1.157 +if not alignments:
   1.158 +    sys.exit(my_name + ": there are no alignments")
   1.159 +
   1.160 +seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
   1.161 +seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
   1.162 +
   1.163 +sys.stderr.write(my_name + ": choosing bp per pixel...\n")
   1.164 +bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
   1.165 +bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
   1.166 +bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   1.167 +sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
   1.168 +
   1.169 +seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
   1.170 +seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
   1.171 +seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
   1.172 +seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
   1.173 +
   1.174 +sys.stderr.write(my_name + ": processing alignments...\n")
   1.175 +hits = alignmentPixels(width, height, alignments, bp_per_pix,
   1.176 +                       seq_start_dic1, seq_start_dic2)
   1.177 +sys.stderr.write(my_name + ": done\n")
   1.178 +
   1.179  image_size = width, height
   1.180  im = Image.new(image_mode, image_size, background_color)
   1.181