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

fix common elements count algorithm - Comparison API #28

Merged
merged 2 commits into from
Aug 15, 2023
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
27 changes: 24 additions & 3 deletions src/main/java/uk/ac/ebi/eva/evaseqcol/service/SeqColService.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,8 @@ public SeqColComparisonResultEntity compareSeqCols(
// "elements" attribute | "a-and-b-same-order"
// LENGTHS
if (lessThanTwoOverlappingElements(seqColALengths, seqColBLengths) || unbalancedDuplicatesPresent(seqColALengths, seqColBLengths)) {
System.out.println("More than two overlapping elements: !!!");
comparisonResult.putIntoElements("a-and-b-same-order", "lengths", null);
} else {
System.out.println("seqColALengths Size: " + seqColALengths.size() + " SECOND Element: " + seqColALengths.get(1));
System.out.println("seqColBLengths Size: " + seqColBLengths.size() + " SECOND Element: " + seqColBLengths.get(1));
boolean lengthsSameOrder = seqColALengths.equals(seqColBLengths);
comparisonResult.putIntoElements("a-and-b-same-order", "lengths", lengthsSameOrder);
}
Expand Down Expand Up @@ -298,6 +295,30 @@ public List<String> getCommonFieldsDistinct(List<String> seqColAFields, List<Str
return commonFieldsDistinct;
}

/**
* Return the number of common elements between listA and listB
* Note: Time complexity for this method is about O(n²)*/
public Integer getCommonElementsCount(List<String> listA, List<String> listB) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks correct to me, just need to call it in the compare method!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

int count = 0;
// Looping over the smallest list will sometimes be time saver
if (listA.size() < listB.size()) {
for (String element : listA) {
if (listB.contains(element)) {
count ++;
listB.remove(element);
}
}
} else {
for (String element : listB) {
if (listA.contains(element)) {
count++;
listA.remove(element);
}
}
}
return count;
}

/**
* Return true if there are less than two overlapping elements
* @see 'https://github.com/ga4gh/seqcol-spec/blob/master/docs/decision_record.md#same-order-specification'*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -89,4 +90,24 @@ void unbalancedDuplicatesPresentTest() {
assertFalse(seqColService.unbalancedDuplicatesPresent(A2, B2));
assertTrue(seqColService.unbalancedDuplicatesPresent(A3, B3));
}

@Test
void getCommonFieldsDistinctTest() {
List<String> A1 = new ArrayList<>();
List<String> B1 = new ArrayList<>();
A1.add("1");A1.add("2");A1.add("1"); // A1 = ["1", "2", "1"]
B1.add("1");B1.add("1");B1.add("2"); // B1 = ["1", "1", "2"]

List<String> A2 = new ArrayList<>();
List<String> B2 = new ArrayList<>();
A2.add("1");A2.add("2");A2.add("1"); // A2 = ["1", "2", "1"]
B2.add("1"); B2.add("2"); // B2 = ["1", "2"]

Integer common1Count = seqColService.getCommonElementsCount(A1, A2);
Integer common2Count = seqColService.getCommonElementsCount(A2, B2);

assertEquals(3, common1Count);
assertEquals(2, common2Count);

}
}