4 use Data
::Dump
qw(ddx);
6 die "Usage: $0 <block file>\n" if @ARGV<1;
12 $in = './example18/simVir4_grep/blocks.txt.gz';
13 my $chrOnly = 'chr18';
14 my $viruschr = 'gi|59585|emb|X04615.1|';
20 my $Paint_Width = $IMG_Width - 2*$IMG_Boder;
21 my $Paint_Height = $IMG_Height - 2*$IMG_Boder;
22 my $Stroke_Width = $Paint_Width/240;
27 if ($inf =~ /\.gz$/i) {
28 open $FH,'-|',"gzip -dc $inf" or die "Error opening $inf: $!\n";
29 } elsif ($inf =~ /\.xz$/i) {
30 open $FH,'-|',"xz -dc $inf" or die "Error opening $inf: $!\n";
31 } elsif ($inf =~ /\.bz2$/i) {
32 open $FH,'-|',"bzip2 -dc $inf" or die "Error opening $inf: $!\n";
34 open $FH,'<',$inf or die "Error opening $inf: $!\n";
39 my $fhin = openFH
($in);
41 my ($id,$chr,$range,$cnt,$vchr,$vrange,$vcnt);
45 my @keys = sort { $inh->{$b} <=> $inh->{$a} } keys %{$inh};
49 sub CIAGRdepth
($$$$) {
50 my ($CIAGR,$leftPos,$DatHash,$SimInfo) = @_;
51 my $curPos = $leftPos;
53 while ($CIAGR =~ /(\d+)(\D)/g) {
55 my $end = $curPos + $1 -1;
56 for ($curPos .. $end) {
58 if (defined $SimInfo) {
59 if ($_ >= $$SimInfo[0] and $_ < $$SimInfo[1]) {
61 } elsif ($_ >= $$SimInfo[1] and $_ <= $$SimInfo[2]) {
72 die "[x]CIAGR[$1 $2] not allowed.\n" if $2 ne 'I';
76 $ret = getmaxkey
(\
%LR);
83 my (%HumDepth,%VirusDepth,%sEdges,%pEdges,%cpItemCache);
84 %pEdges = ('LL'=>0, 'RR'=>0);
85 my (%HumDep,%VirDep,%HumCut,%HumLeft,%HumRight,%VirStrand,%VirLeft,%VirRight);
88 my ($qname,$flag,$rname,$pos,$mapq,$CIAGR,undef,undef,undef,undef,undef,@opts) = split /\t/;
89 $qname =~ /_Ref_(\d+)_(\d+)_(\d+)_Vir_([+-])_(\d+)_(\d+)_/ or die;
96 my @XAs = grep(/^[SX]A:Z:/,@opts);
98 my @t = grep(/\b[fr](\Q$chrOnly\E|\Q$viruschr\E),/,@XAs);
101 #print "--- $doflag, @t\n";
102 $cpItemCache{"$qname\t$rname\t$pos"} = $CIAGR;
105 return unless $doflag; # in fact, all items have $doflag==1;
106 my ($humLeft,$humCut,$humRight,$virStrand,$virLeft,$virRight);
107 $humLeft = getmaxkey
(\
%HumLeft);
108 $humCut = getmaxkey
(\
%HumCut);
109 $humRight = getmaxkey
(\
%HumRight);
110 $virStrand = getmaxkey
(\
%VirStrand);
111 $virLeft = getmaxkey
(\
%VirLeft);
112 $virRight = getmaxkey
(\
%VirRight);
113 my $SimInfo = [$humLeft,$humCut,$humRight,$virStrand,$virLeft,$virRight];
114 print ">>> $humLeft,$humCut,$humRight,$virStrand,$virLeft,$virRight $doflag:\n";
116 my ($qname,$flag,$rname,$pos,$mapq,$CIAGR,$MRNM,$MPOS,undef,undef,undef,@opts) = split /\t/;
117 my ($LR1,$LR2,@itemsEdges,@itempEdges);
118 $MRNM = $rname if $MRNM eq '=';
119 if ($rname eq $chrOnly) {
120 $LR1 = CIAGRdepth
($CIAGR,$pos,\
%HumDepth,$SimInfo);
121 next unless defined $LR1;
124 CIAGRdepth
($CIAGR,$pos,\
%VirusDepth,undef);
126 if (exists $cpItemCache{"$qname\t$MRNM\t$MPOS"}) {
127 my $MCIAGR = $cpItemCache{"$qname\t$MRNM\t$MPOS"};
128 if ($MRNM eq $chrOnly) {
129 $LR2 = CIAGRdepth
($MCIAGR,$MPOS,\
%HumDepth,$SimInfo);
132 CIAGRdepth
($MCIAGR,$MPOS,\
%VirusDepth,undef);
135 my @t = sort ($LR1,$LR2);
136 ++$pEdges{join('',@t)};
141 if (/^[SX]A:Z:([\w\,\;\|\.\+\-]+)$/) {
142 push @XAHit,split(/;/,$1);
148 my ($XAchr,$XApos,$XAcigar,$XAnm) = split /,/;
149 if ($XAchr =~ /^[fr](\Q$chrOnly\E|\Q$viruschr\E)$/) {
150 #print "$1\t$XAchr,$XApos,$XAcigar,$XAnm\n";
152 if ($1 eq $chrOnly) {
153 $LR3 = CIAGRdepth
($XAcigar,abs($XApos),\
%HumDepth,$SimInfo) if $LR1 eq 'V';
154 } elsif ($1 eq $viruschr and $LR1 ne 'V') {
156 CIAGRdepth
($XAcigar,abs($XApos),\
%VirusDepth,undef);
159 my @t = sort ($LR1,$LR3);
160 push @itemsEdges,join('',@t);
165 my $weight = 1 / @itemsEdges;
166 $sEdges{$_} += $weight for @itemsEdges;
170 if (exists $sEdges{'LV'} and exists $sEdges{'RV'}) {
171 ddx
[\
%sEdges,\
%pEdges];
172 return (\
%HumDepth,\
%VirusDepth,\
%sEdges,\
%pEdges,$SimInfo);
180 my ($fh,$HumDepth,$VirusDepth,$sEdges,$pEdges,$SimInfo) = @_;
181 my ($MaxDepth,$HumMin,$HumMax,$VirMin,$VirMax)=(0,@
$SimInfo[0,2,4,5]);
182 my @HumCovered = sort {$a <=> $b} keys %{$HumDepth};
183 my @VirCovered = sort {$a <=> $b} keys %{$VirusDepth};
185 $MaxDepth = $HumDepth->{$_} if $MaxDepth < $HumDepth->{$_};
188 $MaxDepth = $VirusDepth->{$_} if $MaxDepth < $VirusDepth->{$_};
190 $HumMin = $HumCovered[0] if $HumMin > $HumCovered[0];
191 $HumMax = $HumCovered[-1] if $HumMax < $HumCovered[-1];
192 $VirMin = $VirCovered[0] if $VirMin > $VirCovered[0];
193 $VirMax = $VirCovered[-1] if $VirMax < $VirCovered[-1];
194 my $HumRange = $HumMax - $HumMin +1;
195 my $VirRange = $VirMax - $VirMin +1;
196 my $RatioH = $Paint_Width / $HumRange;
197 my $RatioV = $Paint_Width / $VirRange;
198 my $XRatio = ($RatioH < $RatioV)?
$RatioH:$RatioV;
200 for (@
$SimInfo[0,1,2]) {
201 push @Cuts,($_ - $HumMin);
203 for (@
$SimInfo[4,5]) {
204 push @Cuts,($_ - $VirMin);
206 print "Hum:$HumCovered[0] - $HumCovered[-1]\nVir:$VirCovered[0] - $VirCovered[-1]\n@$SimInfo\t@Cuts\n";
207 $_ *= $XRatio for @Cuts;
208 my @BarHeight = ($Paint_Height*3/8, $Paint_Height*5/8, $Paint_Height*7/16);
209 my $YRatio = ($Paint_Height*3/8)/$MaxDepth;
210 my $HumLeft = ($Paint_Width - $HumRange*$XRatio)/2;
211 my $VirLeft = ($Paint_Width - $VirRange*$XRatio)/2;
212 print $fh '<line stroke="gray" stroke-width="',$Stroke_Width,'" x1="',$VirLeft+$IMG_Boder,'" y1="',$IMG_Boder+$BarHeight[0]+$Stroke_Width/2,'" x2="',$VirLeft+$IMG_Boder+$VirRange*$XRatio,'" y2="',$IMG_Boder+$BarHeight[0]+$Stroke_Width/2,'"/>',"\n";
213 print $fh '<line stroke="gray" stroke-width="',$Stroke_Width,'" x1="',$HumLeft+$IMG_Boder,'" y1="',$IMG_Boder+$BarHeight[1]-$Stroke_Width/2,'" x2="',$HumLeft+$IMG_Boder+$HumRange*$XRatio,'" y2="',$IMG_Boder+$BarHeight[1]-$Stroke_Width/2,'"/>',"\n";
215 print $fh '<line stroke="firebrick" stroke-width="',$Stroke_Width,'" x1="',$Cuts[3]+$VirLeft+$IMG_Boder,'" y1="',$IMG_Boder+$BarHeight[0]+$Stroke_Width,'" x2="',$Cuts[4]+$VirLeft+$IMG_Boder,'" y2="',$IMG_Boder+$BarHeight[0]+$Stroke_Width,'"/>',"\n";
216 print $fh '<line stroke="forestgreen" stroke-width="',$Stroke_Width,'" x1="',$Cuts[0]+$HumLeft+$IMG_Boder,'" y1="',$IMG_Boder+$BarHeight[1]-$Stroke_Width,'" x2="',$Cuts[1]+$HumLeft+$IMG_Boder,'" y2="',$IMG_Boder+$BarHeight[1]-$Stroke_Width,'"/>',"\n";
217 print $fh '<line stroke="mediumblue" stroke-width="',$Stroke_Width,'" x1="',$Cuts[1]+$HumLeft+$IMG_Boder,'" y1="',$IMG_Boder+$BarHeight[1]-$Stroke_Width,'" x2="',$Cuts[2]+$HumLeft+$IMG_Boder,'" y2="',$IMG_Boder+$BarHeight[1]-$Stroke_Width,'"/>',"\n";
219 print $fh "<defs>\n";
220 print $fh '<path id="pLV" d="M',$Cuts[0]+$HumLeft+$IMG_Boder+$Stroke_Width/2,',',$IMG_Boder+$BarHeight[1]-2*$Stroke_Width,' Q',$HumLeft+$IMG_Boder,',',$IMG_Boder+$BarHeight[2],' ',$Cuts[3]+$VirLeft+$IMG_Boder,',',$IMG_Boder+$BarHeight[0]+2*$Stroke_Width,'"
221 fill="none" stroke="limegreen" stroke-width="',$Stroke_Width,'" />',"\n";
222 print $fh '<path id="sLV" d="M',$VirLeft+$IMG_Boder+($Cuts[4]+15*$Cuts[3])/16,',',$IMG_Boder+$BarHeight[0]+2*$Stroke_Width,' Q',$HumLeft+$IMG_Boder+$Cuts[1],',',$IMG_Boder+$BarHeight[2],' ',$Cuts[1]+$HumLeft+$IMG_Boder-$Stroke_Width/2,',',$IMG_Boder+$BarHeight[1]-2*$Stroke_Width,'"
223 fill="none" stroke="limegreen" stroke-width="',$Stroke_Width,'" />',"\n";
225 print $fh '<path id="sRV" d="M',$Cuts[1]+$HumLeft+$IMG_Boder+$Stroke_Width/2,',',$IMG_Boder+$BarHeight[1]-2*$Stroke_Width,' Q',$HumLeft+$IMG_Boder+$Cuts[1],',',$IMG_Boder+$BarHeight[2],' ',$VirLeft+$IMG_Boder+$Cuts[3]+($Cuts[4]-$Cuts[3])*15/16,',',$IMG_Boder+$BarHeight[0]+2*$Stroke_Width,'"
226 fill="none" stroke="royalblue" stroke-width="',$Stroke_Width,'" />',"\n";
227 print $fh '<path id="pRV" d="M',$Cuts[4]+$VirLeft+$IMG_Boder,',',$IMG_Boder+$BarHeight[0]+2*$Stroke_Width,' Q',$HumLeft+$IMG_Boder+$Cuts[2],',',$IMG_Boder+$BarHeight[2],' ',$Cuts[2]+$HumLeft+$IMG_Boder-$Stroke_Width/2,',',$IMG_Boder+$BarHeight[1]-2*$Stroke_Width,'"
228 fill="none" stroke="royalblue" stroke-width="',$Stroke_Width,'" />',"\n";
229 print $fh "</defs>\n<use xlink:href=\"#pLV\"/>\n<use xlink:href=\"#sLV\"/>\n<use xlink:href=\"#pRV\"/>\n<use xlink:href=\"#sRV\"/>\n";
230 print $fh '<text text-anchor="middle" dominant-baseline="text-before-edge" font-family="Verdana" font-size="20">',"\n";
231 print $fh "<textPath startOffset=\"50%\" xlink:href=\"#pLV\">pLV: ${$pEdges}{'LV'}</textPath>\n";
232 print $fh "<textPath startOffset=\"50%\" xlink:href=\"#sLV\">sLV: ${$sEdges}{'LV'}</textPath>\n";
233 print $fh "<textPath startOffset=\"50%\" xlink:href=\"#pRV\">pRV: ${$pEdges}{'RV'}</textPath>\n";
234 print $fh "<textPath startOffset=\"50%\" xlink:href=\"#sRV\">sRV: ${$sEdges}{'RV'}</textPath>\n";
235 print $fh '</text>',"\n";
236 print $fh '<text x="',0.5*($Cuts[0]+$Cuts[1])+$HumLeft+$IMG_Boder,'" y="',$IMG_Boder+$BarHeight[1]-2*$Stroke_Width,'" font-family="Verdana" font-size="20" text-anchor="middle" alignment-baseline="text-after-edge">pLL: ',${$pEdges}{'LL'},'</text>',"\n";
237 print $fh '<text x="',0.5*($Cuts[1]+$Cuts[2])+$HumLeft+$IMG_Boder,'" y="',$IMG_Boder+$BarHeight[1]-2*$Stroke_Width,'" font-family="Verdana" font-size="20" text-anchor="middle" alignment-baseline="text-after-edge">pRR: ',${$pEdges}{'RR'},'</text>',"\n";
238 print $fh '<text x="',0.5*($Cuts[3]+$Cuts[4])+$VirLeft+$IMG_Boder,'" y="',$IMG_Boder+$BarHeight[0]+$Stroke_Width,'" font-family="Verdana" font-size="20" text-anchor="middle" alignment-baseline="text-before-edge">pVV: ',${$pEdges}{'VV'},'</text>',"\n";
240 for my $bp (@VirCovered) {
241 my $depth = $VirusDepth->{$bp};
242 my $x = ($bp - $VirMin +0.4) * $XRatio +$VirLeft+$IMG_Boder;
243 #print "$bp:$depth -> $x\n";
244 print $fh '<line stroke="red" stroke-width="',$XRatio*0.8,'" x1="',$x,'" y1="',$IMG_Boder+$BarHeight[0],'" x2="',$x,'" y2="',$IMG_Boder+$BarHeight[0]-$depth*$YRatio,'"/>'," $bp\n";
246 #print "@VirCovered\n";
247 for my $bp (@HumCovered) {
248 my $depth = $HumDepth->{$bp};
249 my $x = ($bp - $HumMin +0.4) * $XRatio +$HumLeft+$IMG_Boder;
250 #print "$bp:$depth -> $x\n";
251 print $fh '<line stroke="blue" stroke-width="',$XRatio*0.8,'" x1="',$x,'" y1="',$IMG_Boder+$BarHeight[1],'" x2="',$x,'" y2="',$IMG_Boder+$BarHeight[1]+$depth*$YRatio,'"/>'," $bp\n";
258 my ($HumDepth,$VirusDepth,$sEdges,$pEdges,$SimInfo);
259 if (defined $id and defined $vchr) {
260 #print "[$id]\t$chr,$range,$cnt\t$vchr,$vrange,$vcnt\n",join("\n",@cache),"\n\n\n";
262 ($HumDepth,$VirusDepth,$sEdges,$pEdges,$SimInfo) = getdepth
(\
@cache);
263 if (defined $HumDepth) {
265 open $fh,'>',"./plots/$id.svg";
266 print $fh '<?xml version="1.0"?>
267 <svg width="',$IMG_Width,'" height="',$IMG_Height,'" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
269 doplot
($fh,$HumDepth,$VirusDepth,$sEdges,$pEdges,$SimInfo);
270 print $fh "</svg>\n";
273 if ($id eq 'B1553') {
282 } elsif (/^HostRange=([\w\:\-\,\|\.]+)$/) {
284 $id = undef if $t =~ /,/;
285 ($chr,$range,$cnt) = split /:/,$t;
286 $id = undef if $chr ne $chrOnly;
287 } elsif (/^VirusRange=([\w\:\-\,\|\.]+)$/) {
289 $id = undef if $t =~ /,/;
290 ($vchr,$vrange,$vcnt) = split /:/,$t;
291 $id = undef if $vchr ne $viruschr;
292 } elsif (/^SamFS=/) {
295 push @cache,$_ unless /^$/;