#! /usr/bin/perl #use lib '/home/satanur/software/Math-Combinatorics-0.09/blib/lib/'; #use Math::Combinatorics open(FILE,$ARGV[0]) || die "unable to open file"; $i=0; $p_pattern=""; while() { $line = $_; chomp $line; @a=split(/\t/,$line); @sd=@a[3..$#a]; @numbers = (0..$#sd); #print "$min\t$ss\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"; #print @numbers , "\n"; $min = 1000; if($i==0){@strains = @sd;$ss_start=1} else{ $chr=$a[0]; for($j=0;$j<=$#sd;$j++) { if($sd[$j] > 0) { push(@share,$strains[$j]); push(@non_zero_sd,$sd[$j]); $flag = 1; } } if($flag==1) { push (@comb,join(",", @$_)) for combinations(0..$#non_zero_sd); for($j=0;$j<=$#comb;$j++) { @strain_ind = split(/\,/,$comb[$j]); $comb_num = @strain_ind; if($comb_num > 0) { $exp = 100 / $comb_num; for($x=0;$x<=$#strain_ind;$x++) { $ind = $strain_ind[$x]; #print $sd[$ind] , "\t"; $c = (($non_zero_sd[$ind] - $exp) * ($non_zero_sd[$ind] - $exp)) / $exp; $chi = $chi + $c; push(@sss,$share[$ind]); } $ssss=join(":",@sss); $cs[0]=$chi; $cs[1]=$ssss; push @AoAcs, [ @cs ]; #if($chi < $min){$min = $chi; $ss=join(":",@sss);} @sss = (); $chi = 0; $c = 0; } } } else { $ss = ""; @share =(); @non_zero_sd =(); @sss = (); } #print "RRRRRRRRR\n"; @AoAcs_sorted = sort { $a->[0] <=> $b->[0] } @AoAcs; $si=0; for $aref ( @AoAcs_sorted ) { $score=@$aref[0]; $strain_share= @$aref[1]; if($si==0) { $array_score[$si] = $score; $array_strain_share[$si] = $strain_share; } if($si>0) { if($score == $array_score[0]) { $array_score[$si] = $score; $array_strain_share[$si] = $strain_share; } if($score > $array_score[0]) { last; } } $si++; } $num_min = @array_score; if($num_strain == 1) { $ss=$array_strain_share[0]; } else { for($ashj=0;$ashj<=$#array_strain_share;$ashj++) { @pst=split(/\:/,$array_strain_share[$ashj]); for($pstj=0;$pstj<=$#pst;$pstj++) { $hst{$pst[$pstj]} = "AAAA"; } } @shash = keys %hst; @sorted_hash = sort { lc($a) cmp lc($b) } @shash; $ss=join(":",@sorted_hash); } @sorted_hash = (); %hst = (); @pst =(); @cs=(); @array_score =(); @array_strain_share = (); @AoAcs_sorted =(); @AoAcs = (); @share =(); $flag = 0; #print "ABCD\t$min\t$ss\n"; #$ss=join(':',@share); #print "$ss\n"; if(($ss eq $p_pattern) && ($chr eq $p_chr)) { $ss_end = $a[2]; @sss = (); @share =(); @non_zero_sd =(); @comb =(); } elsif(($ss ne $p_pattern) || ($chr ne $p_chr)) { $length = ($ss_end - $ss_start) + 1; $num_seg = $length / 10000; @ov_strain=split(/\:/,$p_pattern); $num_strain=@ov_strain; print "$p_chr\t$ss_start\t$ss_end\t$length\t$num_seg\t$num_strain\t$p_pattern\n"; @share =(); @comb=(); @non_zero_sd = (); $ss_start=$a[1]; $ss_end=$a[2]; $ss_chr=$a[0]; } } $i++; $p_pattern=$ss; $p_chr = $chr; $p_end = $ss_end; @share =(); @non_zero_sd =(); $flag = 0; @comb=(); } $length = ($ss_end - $ss_start) + 1; $num_seg = $length / 10000; @ov_strain=split(/\:/,$p_pattern); $num_strain=@ov_strain; print "$p_chr\t$ss_start\t$ss_end\t$length\t$num_seg\t$num_strain\t$p_pattern\n"; sub combinations { return [] unless @_; my $first = shift; my @rest = combinations(@_); return @rest, map { [$first, @$_] } @rest; }