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

TASK-7170 - Fix addIndividual function in VariantMetadataManager #283

Open
wants to merge 1 commit into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.opencb.biodata.models.variant.metadata.VariantFileMetadata;
import org.opencb.biodata.models.variant.metadata.VariantMetadata;
import org.opencb.biodata.models.variant.metadata.VariantStudyMetadata;
import org.opencb.biodata.tools.variant.converters.avro.VCFHeaderToVariantFileHeaderConverter;
import org.opencb.biodata.tools.variant.converters.avro.VCFHeaderToVariantFileMetadataConverter;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.utils.FileUtils;
Expand All @@ -59,6 +58,7 @@
public class VariantMetadataManager {

private VariantMetadata variantMetadata;
private Set<String> individualSet = new HashSet<>();

private ObjectMapper mapper;
private Logger logger;
Expand Down Expand Up @@ -109,6 +109,7 @@ public void load(Path path) throws IOException {
for (VariantStudyMetadata variantStudyMetadata: variantMetadata.getStudies()) {
if (variantStudyMetadata.getIndividuals() != null) {
for (Individual individual : variantStudyMetadata.getIndividuals()) {
individualSet.add(individual.getId());
for (Sample sample : individual.getSamples()) {
sample.getAnnotations().put(INDIVIDUAL_ID, individual.getId());
sample.getAnnotations().put(INDIVIDUAL_FAMILY, individual.getFamily());
Expand Down Expand Up @@ -272,6 +273,7 @@ public void addFile(VariantFileMetadata fileMetadata, String studyId) {
individual.setSamples(samples);

variantStudyMetadata.getIndividuals().add(individual);
individualSet.add(individual.getId());
}


Expand Down Expand Up @@ -383,14 +385,12 @@ public void addIndividual(Individual individual, String studyId) {
if (variantStudyMetadata.getIndividuals() == null) {
variantStudyMetadata.setIndividuals(new ArrayList<>());
}
for (Individual indi: variantStudyMetadata.getIndividuals()) {
if (indi.getId() != null && indi.getId().equals(individual.getId())) {
logger.error("Individual with id '{}' already exists in study '{}'", individual.getId(),
studyId);
return;
}
if (individualSet.contains(individual.getId())) {
logger.error("Individual with id '{}' already exists in study '{}'", individual.getId(), studyId);
return;
}
variantStudyMetadata.getIndividuals().add(individual);
individualSet.add(individual.getId());
}

/**
Expand Down Expand Up @@ -430,6 +430,7 @@ public void removeIndividual(String individualId, String studyId) {
for (int i = 0; i < variantStudyMetadata.getIndividuals().size(); i++) {
if (individualId.equals(variantStudyMetadata.getIndividuals().get(i).getId())) {
variantStudyMetadata.getIndividuals().remove(i);
individualSet.remove(individualId);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.opencb.biodata.tools.variant.metadata;

import org.apache.commons.collections4.map.HashedMap;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.opencb.biodata.models.metadata.Individual;
import org.opencb.biodata.models.metadata.Sample;
import org.opencb.biodata.models.variant.metadata.VariantMetadata;
import org.opencb.biodata.models.variant.metadata.VariantStudyMetadata;
import org.opencb.biodata.tools.variant.metadata.VariantMetadataManager;
import org.opencb.commons.datastore.core.Query;

import java.io.IOException;
Expand Down Expand Up @@ -184,4 +184,38 @@ public void load() {
}
}

@Test
public void testAddIndividual() {
try {
Path path = Paths.get("/tmp/ds.meta.json");
if (path.toFile().exists()) {
path.toFile().delete();
}
manager.save(path, true);

manager = new VariantMetadataManager();
manager.load(path);

String studyId = variantMetadata.getStudies().get(0).getId();
Assert.assertEquals(3, manager.getVariantStudyMetadata(studyId).getIndividuals().size());

manager.addIndividual("Person_0", "Sample_0_0", studyId);

String newIndividualId = "Person_100";
manager.addIndividual(newIndividualId, "Sample_100_0", studyId);
Assert.assertEquals(4, manager.getVariantStudyMetadata(studyId).getIndividuals().size());

manager.addIndividual(newIndividualId, "Sample_100_0", studyId);
Assert.assertEquals(4, manager.getVariantStudyMetadata(studyId).getIndividuals().size());

manager.removeIndividual(newIndividualId, studyId);
Assert.assertEquals(3, manager.getVariantStudyMetadata(studyId).getIndividuals().size());

manager.addIndividual(newIndividualId, "Sample_100_0", studyId);
Assert.assertEquals(4, manager.getVariantStudyMetadata(studyId).getIndividuals().size());
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
}
Loading