annotate PanExplorer_workflow/Perl/reformatHeatmapSVG.pl @ 2:97e4e3e818b6 draft

Uploaded
author dereeper
date Thu, 30 May 2024 11:48:09 +0000
parents 032f6b3806a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
1 #!/usr/bin/perl
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
2
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
3 use strict;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
4
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
5 my $filein = $ARGV[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
6 my $fileout = $ARGV[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
7 my $matrix = $ARGV[2];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
8
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
9
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
10
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
11
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
12 my $min_x = 100000;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
13 my $max_x = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
14 my $min_y = 100000;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
15 my $max_y = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
16 my $n=0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
17 open(OUT,">$fileout");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
18 open(F,$filein);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
19 while(<F>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
20 if (/<path style=" stroke:none;fill-rule:nonzero;fill/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
21
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
22 if (/d=\"M ([\s\d\.]+) L/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
23 my @infos = split(/ /,$1);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
24 my $x = $infos[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
25 if ($x < $min_x){$min_x = $x;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
26 if ($x > $max_x){$max_x = $x;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
27 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
28 if (/L ([\s\d\.]+) L/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
29 my @infos = split(/ /,$1);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
30 my $y = $infos[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
31 if ($y < $min_y){$min_y = $y;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
32 if ($y > $max_y){$max_y = $y;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
33 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
34 if (/L ([\s\d\.]+) Z/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
35 my @infos = split(/ /,$1);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
36 my $y = $infos[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
37 if ($y < $min_y){$min_y = $y;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
38 if ($y > $max_y){$max_y = $y;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
39 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
40 $n++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
41 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
42 else{
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
43 if (!/\<\/svg\>/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
44 print OUT $_;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
45 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
46 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
47 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
48 close(F);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
49
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
50 my $nb_dispensable_clusters = `grep -P -c '\t0' $matrix`;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
51 my $nb_samples = `awk {'print NF-1'} $matrix | head -1`;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
52
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
53 my $global_width = $max_x - $min_x;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
54 my $width_of_one_block = $global_width / $nb_dispensable_clusters;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
55
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
56 my $global_height = $max_y - $min_y;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
57 my $height_of_one_block = $global_height / $nb_samples;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
58
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
59 ###########################################################
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
60 # get distinct pattern of presence/absence
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
61 ###########################################################
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
62 my %patterns;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
63 my $pattern_order = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
64 my %pattern_orders;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
65 open(M,$matrix);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
66 <M>;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
67 while(<M>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
68 my $line = $_;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
69 $line =~s/\n//g;$line =~s/\r//g;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
70 my @infos = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
71
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
72 my $pattern = "";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
73 for (my $k=1;$k<=$#infos;$k++){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
74 $pattern.=$infos[$k];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
75 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
76
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
77 # print only dispensable (at least one absence)
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
78 if ($pattern =~/0/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
79 if (!$patterns{$pattern}){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
80 $pattern_order++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
81 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
82 $patterns{$pattern}++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
83 $pattern_orders{$pattern_order} = $pattern;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
84 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
85 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
86 close(M);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
87
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
88 print "Number of distinct patterns:";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
89 print scalar keys(%patterns)."\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
90
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
91 my @colors = ("orange","green","red","blue","black","pink","yellow","brown","grey","purple","darkred");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
92
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
93 my $cumul_x = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
94 foreach my $pattern_order(sort {$a<=>$b} keys(%pattern_orders)){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
95 my $pattern = $pattern_orders{$pattern_order};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
96 my $size = $patterns{$pattern};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
97 my $width = $size * $width_of_one_block;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
98 my $x = $max_x - $cumul_x - $width;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
99
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
100 my $modulo = $pattern_order % 2;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
101 print "$pattern_order $pattern $size $modulo\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
102
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
103 #my $color = $colors[$pattern_order-1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
104 my $color = $colors[$modulo];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
105
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
106 my $pattern_y = $min_y-15;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
107 print OUT "<rect y='$pattern_y' x='$x' width='$width' height='10' style=\"fill:$color;stroke-width:3;$color;\"/>";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
108
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
109 $cumul_x += $width;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
110 my @values = split(//,$pattern);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
111 my $cumul_y = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
112 foreach my $val(@values){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
113 my $y = $max_y - $cumul_y - $height_of_one_block;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
114 if ($val){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
115 print OUT "<rect y='$y' x='$x' width='$width' height='$height_of_one_block' style=\"fill:purple;stroke:purple;\"/>";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
116 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
117 $cumul_y += $height_of_one_block;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
118 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
119 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
120
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
121
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
122 print OUT "</svg>\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
123 close(OUT);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
124
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
125 print "Min x : $min_x\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
126 print "Max x : $max_x\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
127 print "Min y : $min_y\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
128 print "Max y : $max_y\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
129 print "$n\n";