ALBD

 view release on metacpan or  search on metacpan

lib/LiteratureBasedDiscovery/Rank.pm  view on Meta::CPAN

    my $npp = shift;

    #get all bc pairs
    my $bcPairsRef = &_getBCPairs($startingMatrixRef, 
				  $explicitMatrixRef, $implicitMatrixRef);

    #get bc pairs scores
    &getBatchAssociationScores(
	$bcPairsRef, $explicitMatrixRef, $measure, $association,
	$n1pRef, $np1Ref, $npp);


    # Find the max explicitCUI,implicitCUI association for each implicit CUI. 
    # The association score is the maximum value between a C term and all 
    # B terms
    my %scores = ();
    my $max;
    my $value;
    my $implicitCui;
    my ($key1,$key2);
    foreach my $pairKey (keys %{$bcPairsRef}) {	

	#only compare association scores that are valid
	if (${$bcPairsRef}{$pairKey} != -1) {
	    ($key1,$key2) = split(/,/,$pairKey);
	    #only use key2, since that is the implicit cui (c term)

	    #update max for this implicit cui or create if needed
	    if (!exists $scores{$key2}) {
		$scores{$key2} = ${$bcPairsRef}{$pairKey};
	    }
	    elsif (${$bcPairsRef}{$pairKey} > $scores{$key2}) {
		$scores{$key2} = ${$bcPairsRef}{$pairKey}
	    }
	}
    }
    
    return \%scores;
}


sub scoreImplicit_minimumWeightAssociation {

}


#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



# Builds a list of B->C term pairs that also co-occurr with A terms
# Only adds B->C term pairs for C terms that are also present in the 
# implicitMatrix.
# The value of the bcPairs Hash is the value in the explicit matrix 
# for that pair.
# input:  $startingMatrixRef <- ref to the starting matrix
#         $explicitMatrixRef <- ref to the explicit matrix
#         $implicitMatrixRef <- ref to the implicit matrix
# output: a hash ref of BC term pairs. Each key is "$bTerm,$cTerm", 
#         value is by default the frequency of BC co-occurrences in the 
#         matrix
sub _getBCPairs {
    my $startingMatrixRef = shift;
    my $explicitMatrixRef = shift;
    my $implicitMatrixRef = shift;

    #get all bTerms
    my %bTerms = ();
    my $rowRef;
    foreach my $rowKey (keys %{$startingMatrixRef}) {
	$rowRef = ${$startingMatrixRef}{$rowKey};
	foreach my $colKey (keys %{$rowRef}) {
	    $bTerms{$colKey} = 1;
	}
    }

    #get all the cTerms (unique column values in the implicit matrix)
    my %cTerms = ();
    foreach my $rowKey(keys %{$implicitMatrixRef}) {
	$rowRef = ${$implicitMatrixRef}{$rowKey};
	foreach my $colKey (keys %{$rowRef}) {
	    $cTerms{$colKey} = 1;
	}
    }

    #get all bc pairs, set value to be the frequency of co-occurrence
    my %bcPairs = ();
    foreach my $bTerm(keys %bTerms) {
	$rowRef = ${$explicitMatrixRef}{$bTerm};
	if ($rowRef) {
	    foreach my $cTerm(keys %{$rowRef}) {
		if (exists $cTerms{$cTerm}) {
		    #add because this a->b->c term (%cTerms) is also a b->c term
		    $bcPairs{"$bTerm,$cTerm"} = ${$rowRef}{$cTerm};
		}
	    }
	}
    }
    return \%bcPairs;
}


# ranks the scores in descending order
# input: $scoresRef <- a hash ref to a hash of cuis and scores (hash{cui} = score)
# output: an array ref of the ranked cuis in descending order
sub rankDescending {
    #grab the input
    my $scoresRef = shift;

    #order in descending order, and use the CUI string as a tiebreaker
    my @rankedCuis = ();
    my @tiedCuis = ();
    my $currentScore = -1;
    foreach my $cui (
	#sort function to sort by value
	sort {${$scoresRef}{$b} <=> ${$scoresRef}{$a}} 
	keys %{$scoresRef}) {

	#see if this cui is tied with previuos
	if (${$scoresRef}{$cui} != $currentScore) {



( run in 1.426 second using v1.01-cache-2.11-cpan-f56aa216473 )