changeset 5:58160195728e

Change color setting UI, allow background color setting, limit image size.
author Nick Stoler <nstoler@psu.edu>
date Sun, 02 Mar 2014 22:01:44 -0500
parents 16772d7246e0
children 0c552e103c93
files literal.py literal.xml
diffstat 2 files changed, 39 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/literal.py	Sun Mar 02 15:02:00 2014 -0500
+++ b/literal.py	Sun Mar 02 22:01:44 2014 -0500
@@ -6,7 +6,7 @@
 import argparse
 import fastareader
 
-OPT_DEFAULTS = {'size':'512x512', 'verbose':True,
+OPT_DEFAULTS = {'size':'512x512', 'verbose':True, 'background':'255,255,255',
   'A':'0,255,0', 'T':'255,0,0', 'G':'255,255,255', 'C':'0,0,255'}
 USAGE = "%(prog)s [options] genome.fasta"
 DESCRIPTION = """Convert DNA sequence into a PNG image by representing each base
@@ -40,8 +40,10 @@
   parser.add_argument('-q', '--quiet', action='store_false', dest='verbose',
     help="""Quiet mode.""")
   group = parser.add_argument_group('Color customization', """Use these options
-    to use custom colors for bases. Specify with a comma-delimited RGB value
-    like "100,150,10".""")
+    to use custom colors for the background and the bases. Specify with a
+    comma-delimited RGB value like "100,150,10".""")
+  group.add_argument('-b', '--background', metavar='R,G,B',
+    help="""default: %(default)s""")
   group.add_argument('-A', metavar='R,G,B',
     help="""default: %(default)s""")
   group.add_argument('-T', metavar='R,G,B',
@@ -54,7 +56,7 @@
   args = parser.parse_args()
 
   try:
-    size = parse_size(args.size)
+    size = parse_int_str(args.size, delim='x', num_ints=2)
   except ValueError:
     parser.print_help()
     fail('\nError: Invalid size string "%s".' % args.size)
@@ -67,13 +69,15 @@
     if os.path.exists(outfile) and not args.clobber:
       fail('Error: Output filename already taken: "%s"' % outfile)
 
+  background = parse_int_str(args.background)
+
   colors = {}
-  colors['A'] = parse_rgb(args.A)
-  colors['T'] = parse_rgb(args.T)
-  colors['G'] = parse_rgb(args.G)
-  colors['C'] = parse_rgb(args.C)
+  colors['A'] = parse_int_str(args.A)
+  colors['T'] = parse_int_str(args.T)
+  colors['G'] = parse_int_str(args.G)
+  colors['C'] = parse_int_str(args.C)
 
-  image = Image.new('RGB', size, 'white')
+  image = Image.new('RGB', size, background)
   pixels = image.load()
 
   done = False
@@ -95,26 +99,16 @@
     image.save(outfile, 'PNG')
 
 
-
-def parse_size(size_str):
-  """Parse size string, return a tuple of (width, height).
-  Accepts size strings in the format "640x480".
+def parse_int_str(string, delim=',', num_ints=3):
+  """Parse string of delimited ints, return them in a tuple.
+  Checks that they are ints, they are delimited by "delim", and there are
+  "num_ints" of them.
   If not valid, raises ValueError."""
-  size = map(int, size_str.split('x'))
-  if len(size) != 2:
+  ints = map(int, string.split(delim))
+  if len(ints) != num_ints:
     raise ValueError
   else:
-    return tuple(size)
-
-
-def parse_rgb(rgb_str):
-  """Parse RGB string, return a tuple of (R, G, B).
-  If not valid, raises ValueError."""
-  rgb = map(int, rgb_str.split(','))
-  if len(rgb) != 3:
-    raise ValueError
-  else:
-    return tuple(rgb)
+    return tuple(ints)
 
 
 def outfile_name(infilename):
--- a/literal.xml	Sun Mar 02 15:02:00 2014 -0500
+++ b/literal.xml	Sun Mar 02 22:01:44 2014 -0500
@@ -2,13 +2,13 @@
   <description>Visualize DNA with colored pixels.</description>
   <command interpreter="python">literal.py $input -o $output -c -s ${width}x${height}
   #if $colors.custom:
-    -A $colors.AR,$colors.AG,$colors.AB -T $colors.TR,$colors.TG,$colors.TB -G $colors.GR,$colors.GG,$colors.GB -C $colors.CR,$colors.CG,$colors.CB 
+    -b $colors.background -G $colors.G -C $colors.C -A $colors.A -T $colors.T 
   #end if
   </command>
   <inputs>
     <param name="input" type="data" format="fasta,text" label="Input sequence" />
-    <param name="width" type="integer" value="512" label="Output image width" help="in pixels" />
-    <param name="height" type="integer" value="512" label="Output image height" help="in pixels" />
+    <param name="width" type="integer" value="512" min="0" max="16384" label="Output image width" help="in pixels (max 16384)" />
+    <param name="height" type="integer" value="512" min="0" max="16384" label="Output image height" help="in pixels (max 16384)" />
     <conditional name="colors">
       <param name="custom" type="select" label="Base colors">
         <option value="" selected="True">Use default colors</option>
@@ -18,29 +18,28 @@
         <!-- do nothing -->
       </when>
       <when value="true">
-        <!-- 'A':'0,255,0', 'T':'255,0,0', 'G':'255,255,255', 'C':'0,0,255' -->
-        <param name="AR" type="integer" value="0" label="A red value" />
-        <param name="AG" type="integer" value="255" label="A green value" />
-        <param name="AB" type="integer" value="0" label="A blue value" />
-        <param name="TR" type="integer" value="255" label="T red value" />
-        <param name="TG" type="integer" value="0" label="T green value" />
-        <param name="TB" type="integer" value="0" label="T blue value" />
-        <param name="GR" type="integer" value="255" label="G red value" />
-        <param name="GG" type="integer" value="255" label="G green value" />
-        <param name="GB" type="integer" value="255" label="G blue value" />
-        <param name="CR" type="integer" value="0" label="C red value" />
-        <param name="CG" type="integer" value="0" label="C green value" />
-        <param name="CB" type="integer" value="255" label="C blue value" />
+        <!-- 'G':'255,255,255', 'C':'0,0,255', 'A':'0,255,0', 'T':'255,0,0' -->
+        <param name="G" type="text" value="255,255,255" label="G RGB value" help="Three numbers 0 through 255, separated by commas (no spaces)">
+          <validator type="regex" message='Color must be in the form of "R,G,B"'>^[0-9]+,[0-9]+,[0-9]+$</validator>
+        </param>
+        <param name="C" type="text" value="0,0,255" label="C RGB value" help="Three numbers 0 through 255, separated by commas (no spaces)">
+          <validator type="regex" message='Color must be in the form of "R,G,B"'>^[0-9]+,[0-9]+,[0-9]+$</validator>
+        </param>
+        <param name="A" type="text" value="0,255,0" label="A RGB value" help="Three numbers 0 through 255, separated by commas (no spaces)">
+          <validator type="regex" message='Color must be in the form of "R,G,B"'>^[0-9]+,[0-9]+,[0-9]+$</validator>
+        </param>
+        <param name="T" type="text" value="255,0,0" label="T RGB value" help="Three numbers 0 through 255, separated by commas (no spaces)">
+          <validator type="regex" message='Color must be in the form of "R,G,B"'>^[0-9]+,[0-9]+,[0-9]+$</validator>
+        </param>
+        <param name="background" type="text" value="255,255,255" label="background RGB value" help="Three numbers 0 through 255, separated by commas (no spaces)">
+          <validator type="regex" message='Color must be in the form of "R,G,B"'>^[0-9]+,[0-9]+,[0-9]+$</validator>
+        </param>
       </when>
     </conditional>
   </inputs>
   <outputs>
     <data name="output" format="png"/>
   </outputs>
-  <stdio>
-    <exit_code range="1:" err_level="fatal"/>
-    <exit_code range=":-1" err_level="fatal"/>
-  </stdio>
 
   <help>