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

Refactor Collection Implementations: Drop Guava for Java Native #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Compiled class file
*.class

# Maven
target/

# Log file
*.log

Expand Down
15 changes: 0 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
</dependencies>

<repositories>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/monarchinitiative/gregor/mendel/Genotype.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.monarchinitiative.gregor.mendel;

import com.google.common.collect.ImmutableList;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
* Representation of a genotype in an individual
Expand Down Expand Up @@ -133,10 +132,12 @@ private boolean hasRefCall(int[] observedCalls) {
}

/**
* @return {@link ImmutableList} of alleles in this genotype
* @return {@link List} of alleles in this genotype
*/
public ImmutableList<Integer> getAlleleNumbers() {
return Arrays.stream(alleleNumbers).boxed().collect(ImmutableList.toImmutableList());
public List<Integer> getAlleleNumbers() {
return Arrays.stream(alleleNumbers)
.boxed()
.toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.monarchinitiative.gregor.mendel;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedMap;

import java.util.Iterator;
import java.util.*;
import java.util.Map.Entry;

/**
Expand All @@ -20,7 +17,7 @@
*/
public final class GenotypeCalls implements Iterable<Entry<String, Genotype>> {

private final static Genotype GT_NO_CALL = new Genotype(ImmutableList.of(Genotype.NO_CALL));
private final static Genotype GT_NO_CALL = new Genotype(List.of(Genotype.NO_CALL));

/**
* Type of the chromosome that the variant lies on (autosomal, X-chromosomal, etc.)
Expand All @@ -29,11 +26,11 @@ public final class GenotypeCalls implements Iterable<Entry<String, Genotype>> {
/**
* Mapping from sample name to {@link Genotype}
*/
private final ImmutableSortedMap<String, Genotype> sampleToGenotype;
private final Map<String, Genotype> sampleToGenotype;
/**
* List of sample names
*/
private final ImmutableList<String> sampleNames;
private final List<String> sampleNames;
/**
* A payload object for later easier reidentification
*/
Expand Down Expand Up @@ -61,8 +58,12 @@ public GenotypeCalls(ChromosomeType chromType, Iterable<? extends Entry<String,
public GenotypeCalls(ChromosomeType chromType, Iterable<? extends Entry<String, Genotype>> sampleToGenotype,
Object payload) {
this.chromType = chromType;
this.sampleToGenotype = ImmutableSortedMap.copyOf(sampleToGenotype);
this.sampleNames = ImmutableList.copyOf(this.sampleToGenotype.keySet());

this.sampleToGenotype = new TreeMap<>();
for (Entry<String, Genotype> entry : sampleToGenotype){
this.sampleToGenotype.put(entry.getKey(), entry.getValue());
}
this.sampleNames = List.copyOf(this.sampleToGenotype.keySet());
this.payload = payload;
}

Expand Down Expand Up @@ -105,14 +106,14 @@ public ChromosomeType getChromType() {
/**
* @return Sample to genotype map
*/
public ImmutableSortedMap<String, Genotype> getSampleToGenotype() {
return sampleToGenotype;
public Map<String, Genotype> getSampleToGenotype() {
return Collections.unmodifiableMap(sampleToGenotype);
}

/**
* @return Sample names
*/
public ImmutableList<String> getSampleNames() {
public List<String> getSampleNames() {
return sampleNames;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.monarchinitiative.gregor.mendel;


import java.io.Serial;

/**
* Thrown when the pedigree does not fit to the {@link GenotypeCalls}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.monarchinitiative.gregor.mendel;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.monarchinitiative.gregor.mendel.impl.*;
import org.monarchinitiative.gregor.pedigree.Pedigree;
import org.monarchinitiative.gregor.pedigree.PedigreeQueryDecorator;

import java.util.Collection;
import java.util.Map;
import java.util.*;

/**
* Facade class for checking lists of {@link GenotypeCalls} for compatibility with mendelian inheritance
Expand All @@ -28,7 +24,7 @@ public final class MendelianInheritanceChecker {
/**
* Mendelian compatibility checker for each sub mode of inheritance
*/
final private ImmutableMap<SubModeOfInheritance, AbstractMendelianChecker> checkers;
final private Map<SubModeOfInheritance, AbstractMendelianChecker> checkers;

/**
* Construct checker with the pedigree to use
Expand All @@ -39,15 +35,15 @@ public MendelianInheritanceChecker(Pedigree pedigree) {
this.pedigree = pedigree;
this.queryPed = new PedigreeQueryDecorator(pedigree);

ImmutableMap.Builder<SubModeOfInheritance, AbstractMendelianChecker> builder = new ImmutableMap.Builder<>();
builder.put(SubModeOfInheritance.AUTOSOMAL_DOMINANT, new MendelianCheckerAD(this));
builder.put(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_COMP_HET, new MendelianCheckerARCompoundHet(this));
builder.put(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_HOM_ALT, new MendelianCheckerARHom(this));
builder.put(SubModeOfInheritance.X_DOMINANT, new MendelianCheckerXD(this));
builder.put(SubModeOfInheritance.X_RECESSIVE_COMP_HET, new MendelianCheckerXRCompoundHet(this));
builder.put(SubModeOfInheritance.X_RECESSIVE_HOM_ALT, new MendelianCheckerXRHom(this));
builder.put(SubModeOfInheritance.MITOCHONDRIAL, new InheritanceCheckerMT(this));
this.checkers = builder.build();
Map<SubModeOfInheritance, AbstractMendelianChecker> map = new LinkedHashMap<>();
map.put(SubModeOfInheritance.AUTOSOMAL_DOMINANT, new MendelianCheckerAD(this));
map.put(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_COMP_HET, new MendelianCheckerARCompoundHet(this));
map.put(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_HOM_ALT, new MendelianCheckerARHom(this));
map.put(SubModeOfInheritance.X_DOMINANT, new MendelianCheckerXD(this));
map.put(SubModeOfInheritance.X_RECESSIVE_COMP_HET, new MendelianCheckerXRCompoundHet(this));
map.put(SubModeOfInheritance.X_RECESSIVE_HOM_ALT, new MendelianCheckerXRHom(this));
map.put(SubModeOfInheritance.MITOCHONDRIAL, new InheritanceCheckerMT(this));
this.checkers = map;
}

/**
Expand All @@ -61,26 +57,26 @@ public MendelianInheritanceChecker(Pedigree pedigree) {
* {@link GenotypeCalls} from <code>list</code>
* @throws IncompatiblePedigreeException if the individuals in <code>calls</code> do not fit to the pedigree
*/
public ImmutableMap<ModeOfInheritance, ImmutableList<GenotypeCalls>> checkMendelianInheritance(
public Map<ModeOfInheritance, List<GenotypeCalls>> checkMendelianInheritance(
Collection<GenotypeCalls> calls, Collection<GenotypeCalls> recessiveCalls) throws IncompatiblePedigreeException {
ImmutableMap.Builder<ModeOfInheritance, ImmutableList<GenotypeCalls>> builder = new ImmutableMap.Builder<>();
Map<ModeOfInheritance, List<GenotypeCalls>> map = new LinkedHashMap<>();
for (ModeOfInheritance mode : ModeOfInheritance.values()) {
if (mode == ModeOfInheritance.ANY) {
builder.put(mode, ImmutableList.copyOf(calls));
map.put(mode, List.copyOf(calls));
} else {
if (mode == ModeOfInheritance.AUTOSOMAL_RECESSIVE || mode == ModeOfInheritance.X_RECESSIVE) {
builder.put(mode, filterCompatibleRecords(recessiveCalls, mode));
map.put(mode, filterCompatibleRecords(recessiveCalls, mode));
} else {
builder.put(mode, filterCompatibleRecords(calls, mode));
map.put(mode, filterCompatibleRecords(calls, mode));
}
}
}
return builder.build();
return map;
}

public ImmutableMap<ModeOfInheritance, ImmutableList<GenotypeCalls>> checkMendelianInheritance(
public Map<ModeOfInheritance, List<GenotypeCalls>> checkMendelianInheritance(
Collection<GenotypeCalls> calls) throws IncompatiblePedigreeException {
return checkMendelianInheritance(calls, calls);
return Collections.unmodifiableMap(checkMendelianInheritance(calls, calls));
}


Expand All @@ -94,26 +90,26 @@ public ImmutableMap<ModeOfInheritance, ImmutableList<GenotypeCalls>> checkMendel
* {@link GenotypeCalls} from <code>list</code>
* @throws IncompatiblePedigreeException if the individuals in <code>calls</code> do not fit to the pedigree
*/
public ImmutableMap<SubModeOfInheritance, ImmutableList<GenotypeCalls>> checkMendelianInheritanceSub(
public Map<SubModeOfInheritance, List<GenotypeCalls>> checkMendelianInheritanceSub(
Collection<GenotypeCalls> calls, Collection<GenotypeCalls> compHetRecessiveCalls) throws IncompatiblePedigreeException {
ImmutableMap.Builder<SubModeOfInheritance, ImmutableList<GenotypeCalls>> builder = new ImmutableMap.Builder<>();
Map<SubModeOfInheritance, List<GenotypeCalls>> map = new LinkedHashMap<>();
for (SubModeOfInheritance mode : SubModeOfInheritance.values()) {
if (mode == SubModeOfInheritance.ANY) {
builder.put(mode, ImmutableList.copyOf(calls));
map.put(mode, List.copyOf(calls));
} else {
if (mode == SubModeOfInheritance.AUTOSOMAL_RECESSIVE_COMP_HET || mode == SubModeOfInheritance.X_RECESSIVE_COMP_HET) {
builder.put(mode, filterCompatibleRecordsSub(compHetRecessiveCalls, mode));
map.put(mode, filterCompatibleRecordsSub(compHetRecessiveCalls, mode));
} else {
builder.put(mode, filterCompatibleRecordsSub(calls, mode));
map.put(mode, filterCompatibleRecordsSub(calls, mode));
}
}
}
return builder.build();
return map;
}

public ImmutableMap<SubModeOfInheritance, ImmutableList<GenotypeCalls>> checkMendelianInheritanceSub(
public Map<SubModeOfInheritance, List<GenotypeCalls>> checkMendelianInheritanceSub(
Collection<GenotypeCalls> calls) throws IncompatiblePedigreeException {
return checkMendelianInheritanceSub(calls, calls);
return Collections.unmodifiableMap(checkMendelianInheritanceSub(calls, calls));
}

/**
Expand All @@ -124,45 +120,41 @@ public ImmutableMap<SubModeOfInheritance, ImmutableList<GenotypeCalls>> checkMen
* @return List of {@link GenotypeCalls} from <code>calls</code> that are compatible with <code>mode</code>
* @throws IncompatiblePedigreeException if the individuals in <code>calls</code> do not fit to the pedigree
*/
public ImmutableList<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeCalls> calls, ModeOfInheritance mode)
public List<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeCalls> calls, ModeOfInheritance mode)
throws IncompatiblePedigreeException {
// Check for compatibility of calls with pedigree
if (!calls.stream().allMatch(c -> isCompatibleWithPedigree(c)))
throw new IncompatiblePedigreeException("GenotypeCalls not compatible with pedigree");
// Filter down to the compatible records
ImmutableSet<GenotypeCalls> calls1;
ImmutableSet<GenotypeCalls> calls2;
ImmutableList.Builder<GenotypeCalls> builder;
Set<GenotypeCalls> calls1;
Set<GenotypeCalls> calls2;
List<GenotypeCalls> result;
switch (mode) {
case AUTOSOMAL_DOMINANT:
return checkers.get(SubModeOfInheritance.AUTOSOMAL_DOMINANT).filterCompatibleRecords(calls);
return Collections.unmodifiableList(checkers.get(SubModeOfInheritance.AUTOSOMAL_DOMINANT).filterCompatibleRecords(calls));
case AUTOSOMAL_RECESSIVE:
calls1 = ImmutableSet.copyOf(
checkers.get(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_HOM_ALT).filterCompatibleRecords(calls));
calls2 = ImmutableSet.copyOf(
checkers.get(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_COMP_HET).filterCompatibleRecords(calls));
builder = new ImmutableList.Builder<>();
calls1 = new HashSet<>(checkers.get(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_HOM_ALT).filterCompatibleRecords(calls));
calls2 = new HashSet<>(checkers.get(SubModeOfInheritance.AUTOSOMAL_RECESSIVE_COMP_HET).filterCompatibleRecords(calls));
result = new ArrayList<>();
for (GenotypeCalls c : calls)
if (calls1.contains(c) || calls2.contains(c))
builder.add(c);
return builder.build();
result.add(c);
return Collections.unmodifiableList(result);
case X_DOMINANT:
return checkers.get(SubModeOfInheritance.X_DOMINANT).filterCompatibleRecords(calls);
return Collections.unmodifiableList(checkers.get(SubModeOfInheritance.X_DOMINANT).filterCompatibleRecords(calls));
case X_RECESSIVE:
calls1 = ImmutableSet
.copyOf(checkers.get(SubModeOfInheritance.X_RECESSIVE_HOM_ALT).filterCompatibleRecords(calls));
calls2 = ImmutableSet
.copyOf(checkers.get(SubModeOfInheritance.X_RECESSIVE_COMP_HET).filterCompatibleRecords(calls));
builder = new ImmutableList.Builder<>();
calls1 = new HashSet<>(checkers.get(SubModeOfInheritance.X_RECESSIVE_HOM_ALT).filterCompatibleRecords(calls));
calls2 = new HashSet<>(checkers.get(SubModeOfInheritance.X_RECESSIVE_COMP_HET).filterCompatibleRecords(calls));
result = new ArrayList<>();
for (GenotypeCalls c : calls)
if (calls1.contains(c) || calls2.contains(c))
builder.add(c);
return builder.build();
result.add(c);
return Collections.unmodifiableList(result);
case MITOCHONDRIAL:
return checkers.get(SubModeOfInheritance.MITOCHONDRIAL).filterCompatibleRecords(calls);
return Collections.unmodifiableList(checkers.get(SubModeOfInheritance.MITOCHONDRIAL).filterCompatibleRecords(calls));
default:
case ANY:
return ImmutableList.copyOf(calls);
return List.copyOf(calls);
}
}

Expand All @@ -174,14 +166,14 @@ public ImmutableList<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeC
* @return List of {@link GenotypeCalls} from <code>calls</code> that are compatible with <code>mode</code>
* @throws IncompatiblePedigreeException if the individuals in <code>calls</code> do not fit to the pedigree
*/
public ImmutableList<GenotypeCalls> filterCompatibleRecordsSub(Collection<GenotypeCalls> calls,
public List<GenotypeCalls> filterCompatibleRecordsSub(Collection<GenotypeCalls> calls,
SubModeOfInheritance subMode) throws IncompatiblePedigreeException {
// Check for compatibility of calls with pedigree
if (!calls.stream().allMatch(c -> isCompatibleWithPedigree(c)))
throw new IncompatiblePedigreeException("GenotypeCalls not compatible with pedigree");
// Filter down to the compatible records
if (subMode == SubModeOfInheritance.ANY)
return ImmutableList.copyOf(calls);
return List.copyOf(calls);
else
return checkers.get(subMode).filterCompatibleRecords(calls);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.monarchinitiative.gregor.mendel.impl;

import com.google.common.collect.ImmutableList;
import org.monarchinitiative.gregor.mendel.GenotypeCalls;
import org.monarchinitiative.gregor.mendel.IncompatiblePedigreeException;
import org.monarchinitiative.gregor.mendel.MendelianInheritanceChecker;
import org.monarchinitiative.gregor.pedigree.Pedigree;
import org.monarchinitiative.gregor.pedigree.PedigreeQueryDecorator;

import java.util.Collection;
import java.util.List;

// TODO: check compatibility of pedigree with GenotypeCalls

Expand Down Expand Up @@ -41,10 +41,10 @@ public AbstractMendelianChecker(MendelianInheritanceChecker parent) {
* Filter list of {@link GenotypeCalls} for fitting to mode
*
* @param calls The list of calls to check for compatibility
* @return Filtered {@link ImmutableList} of {@link GenotypeCalls} objects, subset of <code>calls</code>
* @return Filtered {@link List} of {@link GenotypeCalls} objects, subset of <code>calls</code>
* @throws IncompatiblePedigreeException if <code>calls</code> is incompatible with the pedigree
*/
public abstract ImmutableList<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeCalls> calls)
public abstract List<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeCalls> calls)
throws IncompatiblePedigreeException;

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.monarchinitiative.gregor.mendel.impl;

import com.google.common.collect.ImmutableList;
import org.monarchinitiative.gregor.mendel.*;
import org.monarchinitiative.gregor.pedigree.Disease;
import org.monarchinitiative.gregor.pedigree.Pedigree;
import org.monarchinitiative.gregor.pedigree.Person;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.List;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -40,7 +39,7 @@ public InheritanceCheckerMT(MendelianInheritanceChecker parent) {
}

@Override
public ImmutableList<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeCalls> calls)
public List<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeCalls> calls)
throws IncompatiblePedigreeException {

// Filter to calls on the mitochondrion
Expand All @@ -53,8 +52,7 @@ public ImmutableList<GenotypeCalls> filterCompatibleRecords(Collection<GenotypeC
compatibleCalls = mitoCalls.filter(this::isCompatibleSingleton);
else
compatibleCalls = mitoCalls.filter(this::isCompatibleFamily);
return ImmutableList.copyOf(compatibleCalls.collect(Collectors.toList()));

return compatibleCalls.toList();
}

/**
Expand Down
Loading