App-Dazz
view release on metacpan or search on metacpan
lib/App/Dazz/Common.pm view on Meta::CPAN
my %name_of;
for my $i ( 0 .. $#lines ) {
$name_of{ $serials->[$i] } = $lines[$i];
}
return \%name_of;
}
sub judge_distance {
my $d_ref = shift;
my $max_dis = shift || 5000;
return 0 unless defined $d_ref;
my $sum = 0;
my $min = $d_ref->[0];
my $max = $min;
for my $d ( @{$d_ref} ) {
$sum += $d;
if ( $d < $min ) { $min = $d; }
if ( $d > $max ) { $max = $d; }
}
my $avg = $sum / scalar( @{$d_ref} );
return 0 if $avg > $max_dis;
return 0 if $avg == 0;
# max k-mer is 127.
# For k-unitigs, overlaps are less than k-mer
my $v = $max - $min;
if ( $v < 20 or abs( $v / $avg ) < 0.2 ) {
return 1;
}
else {
return 0;
}
}
sub g2gv {
require GraphViz;
#@type Graph
my $g = shift;
my $fn = shift;
my $gv = GraphViz->new( directed => 1 );
for my $v ( $g->vertices ) {
$gv->add_node($v);
}
for my $e ( $g->edges ) {
if ( $g->has_edge_weight( @{$e} ) ) {
$gv->add_edge( @{$e}, label => $g->get_edge_weight( @{$e} ) );
}
else {
$gv->add_edge( @{$e} );
}
}
Path::Tiny::path($fn)->spew_raw( $gv->as_png );
}
sub g2gv0 {
require GraphViz;
#@type Graph
my $g = shift;
my $fn = shift;
my $gv = GraphViz->new( directed => 0 );
for my $v ( $g->vertices ) {
$gv->add_node($v);
}
for my $e ( $g->edges ) {
$gv->add_edge( @{$e} );
}
Path::Tiny::path($fn)->spew_raw( $gv->as_png );
}
sub transitive_reduction {
#@type Graph
my $g = shift;
my $count = 0;
my $prev_count;
while (1) {
last if defined $prev_count and $prev_count == $count;
$prev_count = $count;
for my $v ( $g->vertices ) {
next if $g->out_degree($v) < 2;
my @s = sort { $a cmp $b } $g->successors($v);
for my $i ( 0 .. $#s ) {
for my $j ( 0 .. $#s ) {
next if $i == $j;
if ( $g->is_reachable( $s[$i], $s[$j] ) ) {
$g->delete_edge( $v, $s[$j] );
$count++;
}
}
}
}
}
return $count;
}
# https://metacpan.org/source/GSULLIVAN/String-LCSS-1.00/lib/String/LCSS.pm
# `undef` is returned if the susbstring length is one char or less.
# In scalar context, returns the substring.
# In array context, returns the index of the match root in the two args.
sub lcss {
my $solns0 = ( _lcss( $_[0], $_[1] ) )[0];
return unless $solns0;
my @match = @{$solns0};
return if length $match[0] == 1;
return wantarray ? @match : $match[0];
}
sub _lcss {
# Return array-of-arrays of longest substrings and indices
my ( $r1, $r2 ) = @_;
my ( $l1, $l2, ) = ( length $r1, length $r2, );
( $r1, $r2, $l1, $l2, ) = ( $r2, $r1, $l2, $l1, ) if $l1 > $l2;
my ( $best, @solns ) = 0;
for my $start ( 0 .. $l2 - 1 ) {
for my $l ( reverse 1 .. $l1 - $start ) {
my $substr = substr( $r1, $start, $l );
my $o = index( $r2, $substr );
next if $o < 0;
if ( $l > $best ) {
$best = length $substr;
( run in 0.769 second using v1.01-cache-2.11-cpan-744e820c463 )