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

Improve alignment scores #1701

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,45 +385,45 @@ public static double getAlignmentScore(Double testMz, Float testRt, Float testMo
mobilityRange == null || mobilityRange.equals(Range.all()) ? null : mobilityRange;
ccsRange = ccsRange == null || ccsRange.equals(Range.all()) ? null : ccsRange;

int scorers = 0;
double totalWeight = 0;

double score = 0f;
double score = 0;
// values are "matched" if the given value exists in this class and falls within the tolerance.
if (mzWeight > 0 && mzRange != null && testMz != null) {
final double exactMass = RangeUtils.rangeCenter(mzRange);
double diff = Math.abs(testMz - exactMass);
double maxAllowedDiff = rangeLength(mzRange) / 2;
// no negative numbers
score += Math.max(0, (1 - diff / maxAllowedDiff) * mzWeight);
scorers += (int) Math.round(mzWeight);
score += Math.max(0, (1 - diff / maxAllowedDiff)) * mzWeight;
totalWeight += mzWeight;
}

if (rtWeight > 0 && rtRange != null && testRt != null) {
final Float rt = RangeUtils.rangeCenter(rtRange);
float diff = Math.abs(testRt - rt);
score += Math.max(0, 1 - (diff / (rangeLength(rtRange) / 2)) * rtWeight);
scorers += (int) Math.round(rtWeight);
score += Math.max(0, 1 - (diff / (rangeLength(rtRange) / 2))) * rtWeight;
totalWeight += rtWeight;
}

if (mobilityWeight > 0 && mobilityRange != null && testMobility != null) {
final Float mobility = RangeUtils.rangeCenter(mobilityRange);
float diff = Math.abs(testMobility - mobility);
score += Math.max(0, 1 - (diff / (rangeLength(mobilityRange) / 2)) * mobilityWeight);
scorers += (int) Math.round(mobilityWeight);
score += Math.max(0, 1 - (diff / (rangeLength(mobilityRange) / 2))) * mobilityWeight;
totalWeight += mobilityWeight;
}

if (ccsWeight > 0 && ccsRange != null && testCCS != null) {
final Float ccs = RangeUtils.rangeCenter(ccsRange);
float diff = Math.abs(testCCS - ccs);
score += Math.max(0, 1 - (diff / (rangeLength(ccsRange) / 2)) * ccsWeight);
scorers += (int) Math.round(ccsWeight);
score += Math.max(0, 1 - (diff / (rangeLength(ccsRange) / 2))) * ccsWeight;
totalWeight += ccsWeight;
}

if (scorers == 0) {
if (totalWeight == 0) {
return 0f;
}

return score / scorers;
return score / totalWeight;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.github.mzmine.datamodel.features.types.numbers.RTType;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -115,4 +116,17 @@ void binarySearch() {
assertEquals(10f, rows.get(FeatureListUtils.binarySearch(rows, 10)).getAverageRT());
}


@Test
void getAlignmentScore() {
double score = FeatureListUtils.getAlignmentScore(200.2, 5.1f, null, null,
Range.closed(200d, 201d), Range.closed(5f, 5.5f), null, null, 3, 1, 0, 0);
Assertions.assertTrue(score > 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check the expected test results?

Assertions.assertTrue(score < 1);

score = FeatureListUtils.getAlignmentScore(200.5, 5.25f, null, null,
Range.closed(200d, 201d), Range.closed(5f, 5.5f), null, null, 3, 1, 0, 0);
Assertions.assertTrue(score > 0);
Assertions.assertTrue(score < 1);
}
}
Loading