scripts/last-dotplot
changeset 643 3868726068b0
parent 642 50a6e3e00d36
child 644 08cf5e7f1629
     1.1 --- a/scripts/last-dotplot	Mon Oct 19 11:21:24 2015 +0900
     1.2 +++ b/scripts/last-dotplot	Mon Oct 19 11:25:48 2015 +0900
     1.3 @@ -223,96 +223,97 @@
     1.4          draw.text(position, i[3], font=font, fill=text_color)
     1.5      return im
     1.6  
     1.7 -my_name = os.path.basename(sys.argv[0])
     1.8 -usage = """
     1.9 +if __name__ == "__main__":
    1.10 +    my_name = os.path.basename(sys.argv[0])
    1.11 +    usage = """
    1.12    %prog --help
    1.13    %prog [options] last-tabular-output dotplot.png
    1.14    %prog [options] last-tabular-output dotplot.gif
    1.15    etc."""
    1.16 -op = optparse.OptionParser(usage=usage)
    1.17 -# Replace "width" & "height" with a single "length" option?
    1.18 -op.add_option("-x", "--width", type="int", default=1000,
    1.19 -              help="maximum width in pixels (default: %default)")
    1.20 -op.add_option("-y", "--height", type="int", default=1000,
    1.21 -              help="maximum height in pixels (default: %default)")
    1.22 -op.add_option("-f", "--fontfile",
    1.23 -              help="TrueType or OpenType font file")
    1.24 -op.add_option("-s", "--fontsize", type="int", default=11,
    1.25 -              help="TrueType or OpenType font size (default: %default)")
    1.26 -op.add_option("-c", "--forwardcolor", default="red",
    1.27 -              help="Color for forward alignments (default: %default)")
    1.28 -op.add_option("-r", "--reversecolor", default="blue",
    1.29 -              help="Color for reverse alignments (default: %default)")
    1.30 -(opts, args) = op.parse_args()
    1.31 -if len(args) != 2: op.error("2 arguments needed")
    1.32 +    op = optparse.OptionParser(usage=usage)
    1.33 +    # Replace "width" & "height" with a single "length" option?
    1.34 +    op.add_option("-x", "--width", type="int", default=1000,
    1.35 +                  help="maximum width in pixels (default: %default)")
    1.36 +    op.add_option("-y", "--height", type="int", default=1000,
    1.37 +                  help="maximum height in pixels (default: %default)")
    1.38 +    op.add_option("-f", "--fontfile",
    1.39 +                  help="TrueType or OpenType font file")
    1.40 +    op.add_option("-s", "--fontsize", type="int", default=11,
    1.41 +                  help="TrueType or OpenType font size (default: %default)")
    1.42 +    op.add_option("-c", "--forwardcolor", default="red",
    1.43 +                  help="Color for forward alignments (default: %default)")
    1.44 +    op.add_option("-r", "--reversecolor", default="blue",
    1.45 +                  help="Color for reverse alignments (default: %default)")
    1.46 +    (opts, args) = op.parse_args()
    1.47 +    if len(args) != 2: op.error("2 arguments needed")
    1.48  
    1.49 -if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.50 -else:              font = ImageFont.load_default()
    1.51 +    if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.52 +    else:              font = ImageFont.load_default()
    1.53  
    1.54 -# Make these options too?
    1.55 -text_color = "black"
    1.56 -background_color = "white"
    1.57 -pix_tween_seqs = 2  # number of border pixels between sequences
    1.58 -border_shade = 239, 239, 239  # the shade of grey for border pixels
    1.59 -label_space = 5     # minimum number of pixels between axis labels
    1.60 +    # Make these options too?
    1.61 +    text_color = "black"
    1.62 +    background_color = "white"
    1.63 +    pix_tween_seqs = 2  # number of border pixels between sequences
    1.64 +    border_shade = 239, 239, 239  # the shade of grey for border pixels
    1.65 +    label_space = 5     # minimum number of pixels between axis labels
    1.66  
    1.67 -image_mode = 'RGB'
    1.68 -forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    1.69 -reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    1.70 -zipped_colors = zip(forward_color, reverse_color)
    1.71 -overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
    1.72 +    image_mode = 'RGB'
    1.73 +    forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    1.74 +    reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    1.75 +    zipped_colors = zip(forward_color, reverse_color)
    1.76 +    overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
    1.77  
    1.78 -sys.stderr.write(my_name + ": reading alignments...\n")
    1.79 -input = fileinput.input(args[0])
    1.80 -alignments, seq_size_dic1, seq_size_dic2 = readAlignments(input)
    1.81 -sys.stderr.write(my_name + ": done\n")
    1.82 +    sys.stderr.write(my_name + ": reading alignments...\n")
    1.83 +    input = fileinput.input(args[0])
    1.84 +    alignments, seq_size_dic1, seq_size_dic2 = readAlignments(input)
    1.85 +    sys.stderr.write(my_name + ": done\n")
    1.86  
    1.87 -if not alignments:
    1.88 -    sys.exit(my_name + ": there are no alignments")
    1.89 +    if not alignments:
    1.90 +        sys.exit(my_name + ": there are no alignments")
    1.91  
    1.92 -seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
    1.93 -seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
    1.94 +    seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
    1.95 +    seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
    1.96  
    1.97 -sys.stderr.write(my_name + ": choosing bp per pixel...\n")
    1.98 -bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
    1.99 -bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
   1.100 -bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   1.101 -sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
   1.102 +    sys.stderr.write(my_name + ": choosing bp per pixel...\n")
   1.103 +    bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
   1.104 +    bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
   1.105 +    bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   1.106 +    sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
   1.107  
   1.108 -seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
   1.109 -seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
   1.110 -seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
   1.111 -seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
   1.112 +    seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
   1.113 +    seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
   1.114 +    seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
   1.115 +    seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
   1.116  
   1.117 -sys.stderr.write(my_name + ": processing alignments...\n")
   1.118 -hits = alignmentPixels(width, height, alignments, bp_per_pix,
   1.119 -                       seq_start_dic1, seq_start_dic2)
   1.120 -sys.stderr.write(my_name + ": done\n")
   1.121 +    sys.stderr.write(my_name + ": processing alignments...\n")
   1.122 +    hits = alignmentPixels(width, height, alignments, bp_per_pix,
   1.123 +                           seq_start_dic1, seq_start_dic2)
   1.124 +    sys.stderr.write(my_name + ": done\n")
   1.125  
   1.126 -image_size = width, height
   1.127 -im = Image.new(image_mode, image_size, background_color)
   1.128 +    image_size = width, height
   1.129 +    im = Image.new(image_mode, image_size, background_color)
   1.130  
   1.131 -for i in range(height):
   1.132 -    for j in range(width):
   1.133 -        store_value = hits[i * width + j]
   1.134 -        xy = j, i
   1.135 -        if   store_value == 1: im.putpixel(xy, forward_color)
   1.136 -        elif store_value == 2: im.putpixel(xy, reverse_color)
   1.137 -        elif store_value == 3: im.putpixel(xy, overlap_color)
   1.138 +    for i in range(height):
   1.139 +        for j in range(width):
   1.140 +            store_value = hits[i * width + j]
   1.141 +            xy = j, i
   1.142 +            if   store_value == 1: im.putpixel(xy, forward_color)
   1.143 +            elif store_value == 2: im.putpixel(xy, reverse_color)
   1.144 +            elif store_value == 3: im.putpixel(xy, overlap_color)
   1.145  
   1.146 -if opts.fontsize != 0:
   1.147 -    axis1 = get_axis_image(seq_names1, name_sizes1, seq_starts1, seq_pix1)
   1.148 -    axis2 = get_axis_image(seq_names2, name_sizes2, seq_starts2, seq_pix2)
   1.149 -    axis2 = axis2.rotate(270)
   1.150 -    im.paste(axis1, (0, 0))
   1.151 -    im.paste(axis2, (0, 0))
   1.152 +    if opts.fontsize != 0:
   1.153 +        axis1 = get_axis_image(seq_names1, name_sizes1, seq_starts1, seq_pix1)
   1.154 +        axis2 = get_axis_image(seq_names2, name_sizes2, seq_starts2, seq_pix2)
   1.155 +        axis2 = axis2.rotate(270)
   1.156 +        im.paste(axis1, (0, 0))
   1.157 +        im.paste(axis2, (0, 0))
   1.158  
   1.159 -for i in seq_starts1[1:]:
   1.160 -    box = i - pix_tween_seqs, margin2, i, height
   1.161 -    im.paste(border_shade, box)
   1.162 +    for i in seq_starts1[1:]:
   1.163 +        box = i - pix_tween_seqs, margin2, i, height
   1.164 +        im.paste(border_shade, box)
   1.165  
   1.166 -for i in seq_starts2[1:]:
   1.167 -    box = margin1, i - pix_tween_seqs, width, i
   1.168 -    im.paste(border_shade, box)
   1.169 +    for i in seq_starts2[1:]:
   1.170 +        box = margin1, i - pix_tween_seqs, width, i
   1.171 +        im.paste(border_shade, box)
   1.172  
   1.173 -im.save(args[1])
   1.174 +    im.save(args[1])