File: cgr-image.pl

package info (click to toggle)
murasaki 1.68.6-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,928 kB
  • ctags: 3,100
  • sloc: cpp: 16,010; perl: 8,365; makefile: 186
file content (294 lines) | stat: -rwxr-xr-x 7,138 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/perl

#Copyright (C) 2006-2008 Keio University
#(Kris Popendorf) <comp@bio.keio.ac.jp> (2006)
#
#This file is part of Murasaki.
#
#Murasaki is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#Murasaki is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Murasaki.  If not, see <http://www.gnu.org/licenses/>.

###############
## view histogram details as a CGR graph
###############

use strict;
use Image::Magick;
use Getopt::Long;
use Getopt::Std;
use Pod::Usage;
$Getopt::Std::STANDARD_HELP_VERSION=1;

our ($highest,$lowest);
our $res;
our $lasttick;
our $samples;
our $detailfile;
our $seedcount;
our $noIntermediates;

my ($help,$man);
my $err=GetOptions('help|?' => \$help, man => \$man,"samples=i"=>\$samples,"noIntermediates|1"=>\$noIntermediates);
pod2usage(1) if $help or $#ARGV<0 or !$err;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

foreach $detailfile (@ARGV){
  print "Loading seedlist from $detailfile...\n";
  open(INF,"<$detailfile");
  my @pixels;
  my $sample;
  my @keyl;
  while(<INF>){
    my ($keyn,$keya,$count)=split(/\s/,$_);
    $keya=~s/\.//g;
    push(@keyl,[$keya,$count]);
    $seedcount+=$count;
  }
  print "$seedcount seeds total. Mode switch predicted at sample ".log(sqrt($seedcount))/log(2)."\n";
  close(INF);
  die "No keys!" unless $#keyl>=0;
  #  print join("\n",@keyl),"\n";
  my $sample=$keyl[0][0];
  print "Available samples: ".(length($sample))."\n";
  $samples=(length($sample)) unless $samples and $samples<=(length($sample));
  $res=2<<($samples-1);
  my @graph=growGraph(1,\@keyl,0);
  print "Writing $res x $res image to file $detailfile.cgr.png...\n";
  sneakyImage("$detailfile.cgr.png",\@graph);
}

sub grabCount {
  my ($key,$keylr)=@_;
  my $count;
  foreach(grep {$_[0]=~m/$key\$/} @$keylr){
    $count+=$_[1];
  }
  return $count;
}

sub pngGraph {
  print "(using imagemagick pixels may be slow)\n";
  my ($filename,$graph)=@_;
  my $image=Image::Magick->new();
  $image->Set(size=>join("x",$res,$res));
  $image->Read('xc:white');
  my $err;
  #  $err=$image->set(type=>'Grayscale');
  warn $err if $err;
  my ($i,$max)=(0,$res*$res);
  $lasttick=time;
  foreach my $y (0..($res-1)){
    foreach my $x (0..($res-1)){
      my $v=255-int(255*(($$graph[$y][$x])/$highest));
      $v=sprintf('%02x',$v);
      $v="#".$v.$v.$v;
      $err=$image->Set("pixel[$x,$y]"=>$v);
      warn $err if $err;
      ticker($i,$max);
      $i++;
    }
  }
  $err=$image->Write(filename=>$filename);
  warn $err if $err;
}

sub ticker {
  if(time>$lasttick+5){
    $lasttick=time;
    print int($_[0]/$_[1]*100)."%...\n";
  }
}

sub growGraph {
  my ($level,$keylr,$old_graphr)=@_;
  print "Subsample level $level\n";
  my @graph=doubleGraph($old_graphr);
  $highest=0;
  foreach my $keyd (@$keylr){
    my ($key,$count)=(substr($$keyd[0],-$level,$level),$$keyd[1]);
    my @coords=key2coords($key);
    $graph[$coords[0]][$coords[1]]+=$count;
    $highest=$graph[$coords[0]][$coords[1]] if $graph[$coords[0]][$coords[1]]>$highest;
  }

  sneakyImage("$detailfile.cgr.$level.png",\@graph) unless $noIntermediates;

  return (length($$keylr[0][0])>$level and $level<$samples) ?
    growGraph($level+1,$keylr,\@graph):@graph;
}

sub doubleGraph {
  my $src=pop;
  return ([0,0],[0,0]) unless ref($src) eq "ARRAY";
  my @ret;
  my $buckets=@$src;
  $buckets*=$buckets;
  my $redist=$buckets<$seedcount;
  print $redist ? "Doubling graph and distributing...\n":"Doubling graph...\n";
  foreach my $row (@$src){
    if($row){
      my @drow;
      foreach(@$row){
	$_>>=2 if $redist;
	push(@drow,$_,$_);
      }
      push(@ret,\@drow,[@drow]);
    }else{
      push(@ret,[],[]);
    }
  }
  @{$src}=(); #we can clear the old map after that.
  return @ret;
}

sub selectnext {
  my @ret;
  foreach(qw{G C A T}){
    push(@ret,[map {substr($_,0,length($_)-1)} grep(/$_\$/,@_)]);
  }
  return @ret;
}

sub findLow {
  my $graph=shift;
  $lowest=$highest;
  foreach my $row (@$graph){
    if($row){
      foreach my $val (@$row){
	$lowest=$val if $val<$lowest;
      }
    }
  }
  return $lowest;
}

sub firstOver {
  my $limit=shift;
  for(0..$#_){
    return $_ if $limit<$_[$_];
  }
  return -1;
}

sub firstUnder {
  my $limit=shift;
  for(0..$#_){
    return $#_-$_ if $limit>$_[$#_-$_];
  }
  return -1;
}

sub findNiceRange {
  my $graph=shift;
  my @uberlist;
  foreach my $row (@$graph){
    push(@uberlist,@$row) if $row;
  }
  @uberlist=sort {$a<=>$b} @uberlist;
  $highest=$uberlist[$#uberlist];
  $lowest=$uberlist[0];
  return ($uberlist[int($#uberlist*.01)],$uberlist[int($#uberlist*.99)+1]);
}

sub writePGM { #holy crap PGMs are easy
  my ($filename,$graph)=@_;
  open(PGM,">$filename") or die "Couldnt write to $filename";
  my $localres=@$graph;
  my $maxout=65535;
  print PGM <<ENDHEADER;
P2
#$filename
$localres $localres
$maxout
ENDHEADER

  my ($bottom,$top)=findNiceRange($graph);
  my $range=$top-$bottom;
  print "Low: $lowest High: $highest\n";
  print "White: $bottom. Black: $top Range: $range\n";
  $range=1 unless $range; #no divide by 0 screw ups
  my ($i,$max)=(0,$localres*$localres);
  $lasttick=time;
  foreach my $y (0..($localres-1)){
    foreach my $x (0..($localres-1)){
      my $v=$maxout-int($maxout*(($$graph[$y][$x]-$bottom)/$range));
      $v=0 if $v<0;
      $v=$maxout if $v>$maxout;
      print PGM $v." ";
    }
    print PGM "\n";
    ticker($y,$localres-1);
  }
  close PGM;
}

sub sneakyImage {
  my ($filename,$graph)=@_;
  #make PGM version
  print "Writing uncompressed PGM version...\n";
  writePGM("$filename.pgm",$graph);
  print "Converting PGM to $filename with ImageMagick.\n";
  #make PNG version
  my $image=Image::Magick->new();
  my $err=$image->Set(depth=>16); #this seems to be ignored anyway
  warn $err if $err;
#  $image->Set(size=>join("x",$res,$res));
  $image->Read("$filename.pgm");
  $image->Write($filename);
  print "Erasing temporary PGM file.\n";
  system("rm $filename.pgm");
}

sub showmap {
  return map {join(" ",@$_)."\n"} @_;
}

sub key2coords {
  my $key=shift;
  $key=reverse $key;
  my @coord=(0,0);
  my %field=(C => [0,0],
	     G => [0,1],
	     A => [1,0],
	     T => [1,1]);
  foreach my $bit (split(//,$key)){
    next if $bit eq '.';
    $coord[0]<<=1;
    $coord[1]<<=1;
    my @add=@{$field{$bit}};
    $coord[0]+=$add[0];
    $coord[1]+=$add[1];
  }
  return @coord;
}

sub HELP_MESSAGE(){
  pod2usage(1)
}

__END__

=head1 NAME

cgr-image.pl -- Chaos Graph Representation histogram viewer

=head1 SYNOPSIS

cgr-iamge.pl [options] <alignment1.histogram.details> [alignment2.histogram.details> ...]

=head1 OPTIONS

   --samples=n -> limits to n subsamples (remember resolution is 2^n)
   --nointermediates|1 => skip all the intermediate outputs
=cut