#!/usr/bin/perl # wedges are an arc along the outer edge of the plasmid from a starting angle to an ending angle; # segments are runs of wedges use Cwd; my @g_segments; my $g_outpath; my $g_outfile; my $g_extension; my $g_line_color; my $g_line_thickness; my $g_label_color; my @g_color; my $g_font_name; my $g_font_size; my $g_pi; my $g_radius_1; my $g_radius_2; my $g_name_offset_1; my $g_name_offset_2; my $g_margin; my $g_x_offset; my $g_y_offset; my $g_segment_length; my $g_segment_name; my $g_segment_color_index; my $g_segment_count; my $g_wedge_count; my $g_wedge_angle; # ======================================================================= main(); sub main() { #print "\n\nstarting...\n"; $| = 1; # turn on autoflush to force things to print immediately rather than being buffered. foreach $dropped_file(@ARGV) { clear_data(); load_data($dropped_file); #print_data(); count_segments_and_wedges(); start_output(); draw_plasmid(); write_file($dropped_file); } sleep 2; # wait a few seconds before closing the app #print "done!\n\n"; } # ======================================================================= sub load_data($in_file) { my $in_file = shift; my $in_path; my $the_line; my $i; $g_pi = 3.14159265; $g_extension = "eps"; open(IN_FILE, "<$in_file") || print "Error--Can't open file $in_file for input.\n"; $i = 0; while() { $the_line = $_; # if # the line doesn't start with a comment character: $the_line !~ m/^\#.*$/ # the line isn't just white spaces: $the_line !~ m/^[ \t\n\r\f].*/ # then if ($the_line !~ m/^\#.*$/ && $the_line !~ m/^[ \t\n\r\f].*/) { $i++; chomp($the_line); if ($i == 1) { $g_outpath = $the_line; } elsif ($i == 2) { $g_outfile = $the_line; } elsif ($i == 3) { $g_radius_1 = $the_line; } elsif ($i == 4) { $g_radius_2 = $the_line; } elsif ($i == 5) { $g_name_offset_1 = $the_line; } elsif ($i == 6) { $g_name_offset_2 = $the_line; } elsif ($i == 7) { $g_margin = $the_line; } elsif ($i == 8) { $g_line_color = $the_line; } elsif ($i == 9) { $g_line_thickness = $the_line; } elsif ($i == 10) { $g_label_color = $the_line; } elsif ($i == 11) { $g_font_name = $the_line; } elsif ($i == 12) { $g_font_size = $the_line; } elsif ($i == 13) { $g_color[1] = $the_line; } elsif ($i == 14) { $g_color[2] = $the_line; } elsif ($i == 15) { $g_color[3] = $the_line; } elsif ($i == 16) { $g_color[4] = $the_line; } elsif ($i == 17) { $g_color[5] = $the_line; } elsif ($i == 18) { $g_color[6] = $the_line; } elsif ($i == 19) { $g_color[7] = $the_line; } elsif ($i == 20) { $g_color[8] = $the_line; } elsif ($i == 21) { $g_color[9] = $the_line; } elsif ($i == 22) { $g_color[10] = $the_line; } else { push @g_segments, $the_line; } } } close(IN_FILE); $g_x_offset = $g_radius_1 * $g_name_offset_2 + $g_margin; $g_y_offset = $g_radius_1 * $g_name_offset_2 + $g_margin; } # ======================================================================= sub clear_data() { $g_outpath = 0; $g_outfile = 0; $g_radius_1 = 0; $g_radius_2 = 0; $g_name_offset_1 = 0; $g_name_offset_2 = 0; $g_margin = 0; $g_line_color = 0; $g_line_thickness = 0; $g_label_color = 0; $g_font_name = 0; $g_font_size = 0; @g_color = (); @g_segments = (); $g_x_offset = 0; $g_y_offset = 0; $g_segment_length = 0; $g_segment_name = 0; $g_segment_color_index = 0; $g_segment_count = 0; $g_wedge_count = 0; $g_wedge_angle = 0; } # ======================================================================= sub count_segments_and_wedges() { my $segment_record; my $segment_name; my $segment_color_index; my $the_value; my $i; $g_segment_count = 0; $g_wedge_count = 0; foreach $segment_record(@g_segments) { ($segment_length, $segment_color_index, $segment_name) = split /,/, $segment_record; $g_segment_count++; $g_wedge_count = $g_wedge_count + $segment_length; } if(0) { print "g_segment_count: $g_segment_count\n"; print "g_wedge_count: $g_wedge_count\n"; } } # ======================================================================= sub start_output() { my $bbox_x; my $bbox_y; $bbox_x = $g_x_offset * 2; $bbox_y = $g_y_offset * 2; $g_the_output = "%!PS-Adobe-3.0 EPSF-3.0\n"; $g_the_output .= "%%BoundingBox: 0 0 $bbox_x $bbox_y % LLx LLy URx URy\n"; $g_the_output .= "%%Pages: 1\n"; $g_the_output .= "\n"; $g_the_output .= "/$g_font_name findfont\n"; $g_the_output .= "$g_font_size scalefont\n"; $g_the_output .= "setfont\n"; $g_the_output .= "\n"; } # ======================================================================= sub draw_plasmid() { my $segment_length; my $segment_name; my $segment_color_index; my $the_starting_wedge; my $the_ending_wedge; my $the_wedge_index; my $percent_complete; my $i; my $j; $g_wedge_angle = 360 / $g_wedge_count; # print "g_wedge_angle: .$g_wedge_angle.\ng_wedge_count: .$g_wedge_count .\n\n"; $the_starting_wedge = 1; foreach $segment_record(@g_segments) { ($segment_length, $segment_color_index, $segment_name) = split /,/, $segment_record; if (0) { print "length: .$segment_length.\n"; print "name: .$segment_name.\n"; print "color: .$segment_color_index.\n\n"; } $the_ending_wedge = $the_starting_wedge + $segment_length - 1; for ($the_wedge_index = $the_starting_wedge; $the_wedge_index <= $the_ending_wedge; $the_wedge_index++) { if (0) { print "wedge index: $the_wedge_index\n"; print "color index: $segment_color_index\n"; } draw_the_wedge($the_wedge_index, $segment_color_index); # set up and update progress bar $percent_complete = $the_wedge_index / $g_wedge_count * 100; if ($percent_complete - int($percent_complete) >= 0.5) { $percent_complete = int($percent_complete) + 1; } else { $percent_complete = int($percent_complete); } print "PROGRESS:$percent_complete\n"; # slow it down to test progress bar # for ($i = 1; $i <=100000; $i++) { $j = $j*2; } } if ($segment_name ne "") { add_segment_name($the_starting_wedge, $segment_name); } $the_starting_wedge = $the_ending_wedge + 1; } } # ======================================================================= sub draw_the_wedge($the_wedge_index, $the_color_index) { my $the_wedge_index = shift; my $the_color_index = shift; my $the_point_radians; my $x1; my $y1; my $x2; my $y2; my $x3; my $y3; my $x4; my $y4; my $the_color; if (0) { print "wedge index: $the_wedge_index\n"; print "color index: $the_color_index\n"; } $the_point_radians = (($the_wedge_index - 1) * $g_wedge_angle) / 180 * $g_pi; $x1 = cos($the_point_radians) * $g_radius_1 + $g_x_offset; $y1 = sin($the_point_radians) * $g_radius_1 + $g_y_offset; if (0) { print "wedge angle: $g_wedge_angle\n"; print "pi: $g_pi\n"; print "the_point_radians: $the_point_radians\n"; print "g_radius_1: $g_radius_1\n"; print "g_x_offset: $g_x_offset\n"; print "g_y_offset: $g_y_offset\n"; print "x1: $x1\n"; print "y1: $y1\n"; } $the_point_radians = ($the_wedge_index * $g_wedge_angle) / 180 * $g_pi; $x2 = cos($the_point_radians) * $g_radius_1 + $g_x_offset; $y2 = sin($the_point_radians) * $g_radius_1 + $g_y_offset; $the_point_radians = ($the_wedge_index * $g_wedge_angle) / 180 * $g_pi; $x3 = cos($the_point_radians) * $g_radius_2 + $g_x_offset; $y3 = sin($the_point_radians) * $g_radius_2 + $g_y_offset; $the_point_radians = (($the_wedge_index - 1) * $g_wedge_angle) / 180 * $g_pi; $x4 = cos($the_point_radians) * $g_radius_2 + $g_x_offset; $y4 = sin($the_point_radians) * $g_radius_2 + $g_y_offset; # print "wi:.$the_wedge_index.\n.$g_wedge_angle.\n.$g_pi.\n.$x1.\n.$y1.\n.$x2.\n.$y2.\n.$x3.\n.$y3.\n.$x4.\n.$y4.\n"; # print "ci: $the_color_index\n"; $the_color = $g_color[$the_color_index]; $g_the_output .= "$x1 $y1 moveto\n"; $g_the_output .= "$x2 $y2 lineto\n"; $g_the_output .= "$x3 $y3 lineto\n"; $g_the_output .= "$x4 $y4 lineto\n"; $g_the_output .= "$x1 $y1 lineto\n"; $g_the_output .= "$g_line_thickness setlinewidth\n"; $g_the_output .= "$g_line_color setrgbcolor\n"; $g_the_output .= "gsave\n"; $g_the_output .= "stroke\n"; $g_the_output .= "grestore\n"; $g_the_output .= "$the_color setrgbcolor\n"; $g_the_output .= "fill\n"; $g_the_output .= "\n"; } # ======================================================================= sub add_segment_name($the_wedge_index, $the_name) { my $the_wedge_index = shift; my $the_name = shift; my $x1; my $y1; my $x2; my $y2; my $the_point_radians; my $the_char_count; $the_point_radians = (($the_wedge_index - 1) * $g_wedge_angle) / 180 * $g_pi; $x1 = cos($the_point_radians) * ($g_radius_1 * $g_name_offset_1) + $g_x_offset; $y1 = sin($the_point_radians) * ($g_radius_1 * $g_name_offset_1) + $g_y_offset; $x2 = cos($the_point_radians) * ($g_radius_1 * $g_name_offset_2) + $g_x_offset; $y2 = sin($the_point_radians) * ($g_radius_1 * $g_name_offset_2) + $g_y_offset; $g_the_output .= "$x1 $y1 moveto\n"; $g_the_output .= "$x2 $y2 lineto\n"; $g_the_output .= "$g_line_thickness setlinewidth\n"; $g_the_output .= "$g_line_color setrgbcolor\n"; $g_the_output .= "stroke\n"; $g_the_output .= "\n"; $g_the_output .= "gsave\n"; $g_the_output .= "$g_label_color setrgbcolor\n"; if ($g_name_offset_1 >= 1) { if ($the_point_radians <= $g_pi/2) { $g_the_output .= "$x2 $y2 moveto\n"; } elsif ($the_point_radians <= $g_pi) { $g_the_output .= "($the_name) stringwidth pop $x2 exch sub $y2 moveto\n"; } elsif ($the_point_radians <= $g_pi*1.5) { $y2 -= $g_font_size; $g_the_output .= "($the_name) stringwidth pop $x2 exch sub $y2 moveto\n"; } elsif ($the_point_radians <= $g_pi*2) { $y2 -= $g_font_size; $g_the_output .= "$x2 $y2 moveto\n"; } else { print "Error--point radians > 2pi\n\n"; $g_the_output .= "$x2 $y2 moveto\n"; } } else { if ($the_point_radians <= $g_pi/2) { $y2 -= $g_font_size; $g_the_output .= "($the_name) stringwidth pop $x2 exch sub $y2 moveto\n"; } elsif ($the_point_radians <= $g_pi) { $y2 -= $g_font_size; $g_the_output .= "$x2 $y2 moveto\n"; } elsif ($the_point_radians <= $g_pi*1.5) { $g_the_output .= "$x2 $y2 moveto\n"; } elsif ($the_point_radians <= $g_pi*2) { $g_the_output .= "($the_name) stringwidth pop $x2 exch sub $y2 moveto\n"; } else { print "Error--point radians > 2pi\n\n"; $g_the_output .= "$x2 $y2 moveto\n"; } } $g_the_output .= "($the_name) show\n"; $g_the_output .= "grestore\n"; $g_the_output .= "\n"; } # ======================================================================= sub write_file($in_file) { my $in_file = shift; my $temp_file_name; my $sec; my $min; my $hour; my $mday; my $mon; my $year; my $wday; my $yday; my $isdst; my @the_path_parts; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($sec < 10) { $sec = "0$sec"; } if ($min < 10) { $min = "0$min"; } if ($hour < 10) { $hour = "0$hour"; } if ($mday < 10) { $mday = "0$mday"; } if ($mon < 10) { $mon = "0$mon"; } $year += 1900; if ($g_outpath eq "TO_DESKTOP") { # get path to desktop $g_outpath = $ENV{'HOME'}; $g_outpath .= "/Desktop/"; # print "$g_outpath\n"; } if ($g_outfile eq "USE_INPUT_FILENAME") { @the_path_parts = split /\//, $in_file; $g_outfile = pop(@the_path_parts); #print "$g_outfile\n"; } $temp_file_name = $g_outpath.$g_outfile."_"."$year$mon$mday"."_"."$hour$min$sec.$g_extension"; open(OUT_FILE, ">$temp_file_name") || print "Error--Can't open file for output.\n"; print OUT_FILE $g_the_output; close(OUT_FILE); } # ======================================================================= sub print_data() { my $the_line; my $i; print "g_outpath: $g_outpath\n"; print "g_outfile: $g_outfile\n"; print "g_radius_1: $g_radius_1\n"; print "g_radius_2: $g_radius_2\n"; print "g_name_offset_1: $g_name_offset_1\n"; print "g_name_offset_2: $g_name_offset_2\n"; print "g_margin: $g_margin\n"; print "g_line_color: $g_line_color\n"; print "g_line_thickness: $g_line_thickness\n"; print "g_label_color: $g_label_color\n"; print "g_font_name: $g_font_name\n"; print "g_font_size: $g_font_size\n"; print "g_color[1]: $g_color[1]\n"; print "g_color[2]: $g_color[2]\n"; print "g_color[3]: $g_color[3]\n"; print "g_color[4]: $g_color[4]\n"; print "g_color[5]: $g_color[5]\n"; print "g_color[6]: $g_color[6]\n"; print "g_color[7]: $g_color[7]\n"; print "g_color[8]: $g_color[8]\n"; print "g_color[9]: $g_color[9]\n"; print "g_color[10]: $g_color[10]\n"; $i = 0; foreach $the_line(@g_segments) { ($the_a, $the_b, $the_c) = split /,/, $the_line; $i++; print "$i: .$the_a.$the_b.$the_c.\n"; } } # =======================================================================