Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

190bisbis #203

Merged
merged 3 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
143 changes: 65 additions & 78 deletions bin/agat_sp_sensitivity_specificity.pl
Expand Up @@ -248,15 +248,14 @@
# keep track last locationA
my $last_locationA = undef;
$last_locationA = 1 if (scalar @{$flattened_locations1->{$locusID}{$chimere_type}{$level}{$type}} == 0);
print "Last LocationA !!\n" if ( $last_locationA and $verbose);
print "Lets go for last LocationA !!\n" if ( $last_locationA and $verbose);

if ( exists_keys ($flattened_locations2, ($locusID,$chimere_type,$level,$type) ) and
scalar @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}} != 0 ){ # and

my $previous_overlap = 0;
while ( scalar @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}} != 0 ){
if ($verbose) { print " list of location2 $level $type: "; foreach my $array ( @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}}){print "@{$array} - "; } print "\n";}
my $shift_it = 1;

my $location2 = $flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}->[0];
print " location2 investigated: @$location2\n" if ($verbose);
print " Original TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
Expand All @@ -266,110 +265,117 @@
# keep track last locationA
my $last_locationB = undef;
$last_locationB = 1 if (scalar @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}} == 1);
print " Last LocationB !!\n" if ($last_locationB and $verbose);
print " Lets go for last LocationB !!\n" if ($last_locationB and $verbose);

# ===================== CASE 1 =====================
# location A ----------------
# location B ---------------
if ($location2->[1] < $location1->[0]){
print " shift location2 because before location A!\n" if ($verbose);
my $FP = $location2->[1] - $location2->[0] + 1; #size
$all{$chimere_type}{$level}{$type}{'FP'} += $FP;
print " +FP => $FP\n" if ($verbose);
$previous_overlap = 0;
$all{$chimere_type}{$level}{$type}{'FP'} += $FP;

print "End1 TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
print "End1 FN: ".$all{$chimere_type}{$level}{$type}{'FN'}."\n" if $verbose;
print "End1 FP: ".$all{$chimere_type}{$level}{$type}{'FP'}."\n" if $verbose;
print " Case1 - Next location2!\n\n" if ($verbose);
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
}


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ------------ OVERLAP -----------
## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
elsif( ($location1->[0] <= $location2->[1]) and ($location1->[1] >= $location2->[0])){
$previous_overlap = 1;
print " location @$location1 and @$location2 overlap !!!!\n" if $verbose;
my ($FN, $FP, $TP) = get_snsp_for_overlaps ($location1, $location2);
print " FN=$FN, FP=$FP, TP=$TP\n" if $verbose;


my $locationB_remain = 0 ;
my $locationA_remain = 0 ;
# check vvvv
# location A -------
# location B --------------
# but only if not the last location
my $locationB_remain = 0 ;
my $locationA_remain = 0 ;
my $clean_cut = 0 ;
if( $location2->[1] > $location1->[1] ){
$locationB_remain = $location2->[1] - $location1->[1] + 1;
$shift_it = undef;
}
# check vvvv
# location A -----------
# location B --------------
elsif( $location1->[1] > $location2->[1] ){
$locationA_remain = 1;
}
# v
# location A -------- <
# location B -------------- <
else{
$clean_cut = 1;
}

# location A -------------
# location B ----------- -- ------------

print "locationA_remain $locationA_remain \n" if $verbose;
print "locationB_remain $locationB_remain \n" if $verbose;
print "clean_cut $clean_cut \n" if $verbose;
if ($locationA_remain and !$last_locationA){

if ($locationA_remain and !$last_locationA and !$last_locationB){
# TP must always be added
$all{$chimere_type}{$level}{$type}{'TP'} += $TP;
$all{$chimere_type}{$level}{$type}{'FN'} -= $TP;
$all{$chimere_type}{$level}{$type}{'FP'} += $FP;
print " TP: ADDING ".$TP."\n" if $verbose;
print " FN: removing ".$TP."\n" if $verbose;
print " FP: ADDING ".$FP."\n" if $verbose;
print " TP: ADDING ".$TP."\n" if $verbose;
print " Case2 A - Next location B \n" if $verbose;
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
}
elsif ($locationB_remain and !$last_locationB){

elsif ($locationB_remain and !$last_locationA and !$last_locationB){
# TP must always be added
$all{$chimere_type}{$level}{$type}{'TP'} += $TP;
$all{$chimere_type}{$level}{$type}{'FN'} += $FN;
$all{$chimere_type}{$level}{$type}{'FP'} -= $TP;

print " TP: ADDING ".$TP."\n" if $verbose;
print " FN: ADDING ".$FN."\n" if $verbose;
print " FP: removing ".$TP."\n" if $verbose;
print " TP: ADDING ".$TP."\n" if $verbose;
print " Case2 B - Next location A\n" if $verbose;
last;
}
else{

else{ #clean cut or end of one type of location (1 or 2)
$all{$chimere_type}{$level}{$type}{'FN'} += $FN;
$all{$chimere_type}{$level}{$type}{'FP'} += $FP;
$all{$chimere_type}{$level}{$type}{'TP'} += $TP;
print " TP: ADDING ".$TP."\n" if $verbose;
print " FN: ADDING ".$FN."\n" if $verbose;
print " FP: ADDING ".$FP."\n" if $verbose;
print " TP: ADDING ".$TP."\n" if $verbose;
}

# From previous locationA analysis
if($locationB_remain){
# No more locationA
if( $last_locationA ){
print " No more location A 1\n" if $verbose;
# No more locationB
if( $last_locationB ){
print " No more location B\n" if $verbose;
print " Case2 C ------\n" if $verbose;
print " End TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
print " End FN: ".$all{$chimere_type}{$level}{$type}{'FN'}."\n" if $verbose;
print " End FP: ".$all{$chimere_type}{$level}{$type}{'FP'}."\n\n" if $verbose;

if( $last_locationB and !$last_locationA){
print " Case2 C2 - Remove last location B\n" if ($verbose);
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
}
elsif ($last_locationA and !$last_locationB){
print " Case2 C3 - No more location A - LAST\n" if $verbose;
last;
}
elsif ($last_locationA and $last_locationB){
print " Case2 C4 - No more locationA neither locationB. Removing locationB and LAST.\n" if ($verbose);
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
last;
}
# clean cut v
# location A -------- <
# location B -------------- <
# No more locationA
elsif(!$locationA_remain and !$locationB_remain){
print " Clean cut !!! Removing LocationB and next Location A\n" if $verbose;
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
last; # next locationA
}
print " Next location B\n" if $verbose;
$shift_it = 1;
}
else{
print " Next location A\n" if $verbose;
last; # next locationA
}
}
elsif( $clean_cut ){
print " Clean cut !!! Removing LocationB and next Location A\n" if $verbose;
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
print " End3 TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
print " End3 FN: ".$all{$chimere_type}{$level}{$type}{'FN'}."\n" if $verbose;
print " End3 FP: ".$all{$chimere_type}{$level}{$type}{'FP'}."\n\n" if $verbose;
last; # next locationA
}
}

# ===================== CASE 3 =====================
# location A -------------------------
# location B -------------------------
else{
Expand All @@ -378,43 +384,23 @@
my $FN = $location1->[1] - $location1->[0] + 1; #size
$all{$chimere_type}{$level}{$type}{'FN'} += $FN;
print " Take into account the current locationA! +FN: $FN;\n" if ($verbose);
$shift_it = undef;

print "End2 TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
print "End2 FN: ".$all{$chimere_type}{$level}{$type}{'FN'}."\n" if $verbose;
print "End2 FP: ".$all{$chimere_type}{$level}{$type}{'FP'}."\n\n" if $verbose;


# no more location1
if(scalar @{$flattened_locations1->{$locusID}{$chimere_type}{$level}{$type}} == 0 ){
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
print "No more location A 2\n" if $verbose;
next;
}
else{
$previous_overlap = 0;
last; # next locationA
}
}

#
# - END -
#
if ($shift_it){
my $tothrow = shift @{$flattened_locations2->{$locusID}{$chimere_type}{$level}{$type}};# Throw location B
print " Removing location2: @$tothrow\n" if ($verbose);
print " End2 TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
print " End2 FN: ".$all{$chimere_type}{$level}{$type}{'FN'}."\n" if $verbose;
print " End2 FP: ".$all{$chimere_type}{$level}{$type}{'FP'}."\n" if $verbose;
print " Case3 - Next location A \n\n" if $verbose;
last; # next locationA
}
print "End1 TP: ".$all{$chimere_type}{$level}{$type}{'TP'}."\n" if $verbose;
print "End1 FN: ".$all{$chimere_type}{$level}{$type}{'FN'}."\n" if $verbose;
print "End1 FP: ".$all{$chimere_type}{$level}{$type}{'FP'}."\n\n" if $verbose;
}# END WHILE until location B is after A
}

# ===================== CASE 4 =====================
# The list of locationB is empty now
else{
my $FN += $location1->[1] - $location1->[0] + 1; #size
print " LocationA only => +FN:$FN\n" if ($verbose);
$all{$chimere_type}{$level}{$type}{'FN'} += $FN;
print " Case4 - Next location A \n\n" if $verbose;
}
}
}
Expand All @@ -425,6 +411,7 @@
my $FN=0;
foreach my $location ( @{$flattened_locations1->{$locusID}{$chimere_type}{$level}{$type}} ){ # here the location are supposed to be sorted
$FN += $location->[1] - $location->[0] + 1; #size
print " Case5 - Next location A \n" if $verbose;
}
$all{$chimere_type}{$level}{$type}{'FN'} += $FN;
}
Expand Down
24 changes: 22 additions & 2 deletions t/scripts_output.t
Expand Up @@ -4,7 +4,7 @@ use strict;
use warnings;
use File::Path;

use Test::More tests => 58;
use Test::More tests => 61;

=head1 DESCRIPTION

Expand Down Expand Up @@ -486,11 +486,31 @@ unlink $outtmp;

$script = $script_prefix."bin/agat_sp_sensitivity_specificity.pl";
$result = "$output_folder/agat_sp_sensitivity_specificity_2.txt";
system(" $script --gff1 $input_folder/agat_sp_sensitivity_specificity/1.gff --gff2 $input_folder/agat_sp_sensitivity_specificity/2.gff -o $outtmp 2>&1 1>/dev/null");
system(" $script --gff1 $input_folder/agat_sp_sensitivity_specificity/ref0.gff3 --gff2 $input_folder/agat_sp_sensitivity_specificity/query0.gff3 -o $outtmp 2>&1 1>/dev/null");
#run test
ok( system("diff $result $outtmp") == 0, "output $script");
unlink $outtmp;

$script = $script_prefix."bin/agat_sp_sensitivity_specificity.pl";
$result = "$output_folder/agat_sp_sensitivity_specificity_3.txt";
system(" $script --gff1 $input_folder/agat_sp_sensitivity_specificity/ref1.gff3 --gff2 $input_folder/agat_sp_sensitivity_specificity/query1.gff3 -o $outtmp 2>&1 1>/dev/null");
#run test
ok( system("diff $result $outtmp") == 0, "output $script");
unlink $outtmp;

$script = $script_prefix."bin/agat_sp_sensitivity_specificity.pl";
$result = "$output_folder/agat_sp_sensitivity_specificity_4.txt";
system(" $script --gff1 $input_folder/agat_sp_sensitivity_specificity/ref2.gff3 --gff2 $input_folder/agat_sp_sensitivity_specificity/query2.gff3 -o $outtmp 2>&1 1>/dev/null");
#run test
ok( system("diff $result $outtmp") == 0, "output $script");
unlink $outtmp;

$script = $script_prefix."bin/agat_sp_sensitivity_specificity.pl";
$result = "$output_folder/agat_sp_sensitivity_specificity_5.txt";
system(" $script --gff1 $input_folder/agat_sp_sensitivity_specificity/ref3.gff3 --gff2 $input_folder/agat_sp_sensitivity_specificity/query3.gff3 -o $outtmp 2>&1 1>/dev/null");
#run test
ok( system("diff $result $outtmp") == 0, "output $script");
unlink $outtmp;
# --------check agat_sp_split_by_level2_feature.pl-------------

$script = $script_prefix."bin/agat_sp_separate_by_record_type.pl";
Expand Down