Skip to content

Commit

Permalink
EVA-3425 make sure reference and alternate are always uppercase (#107)
Browse files Browse the repository at this point in the history
* make sure reference and alternate are always uppercase
  • Loading branch information
nitin-ebi authored Oct 26, 2023
1 parent 1a8ff72 commit 923accf
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public AbstractVariant(String chromosome, long start, long end, String reference
if (chromosome == null || chromosome.trim().equals("")) {
throw new IllegalArgumentException("Chromosome name cannot be empty");
}
reference = Objects.nonNull(reference) ? reference.toUpperCase() : null;
alternate = Objects.nonNull(alternate) ? alternate.toUpperCase() : null;
this.chromosome = chromosome;
this.setCoordinates(start, end);
this.setReference(reference);
Expand Down Expand Up @@ -200,11 +202,11 @@ public String getMainId() {
}

private void setReference(String reference) {
this.reference = (reference != null) ? reference : "";
this.reference = (reference != null) ? reference.toUpperCase() : "";
}

private void setAlternate(String alternate) {
this.alternate = (alternate != null) ? alternate : "";
this.alternate = (alternate != null) ? alternate.toUpperCase() : "";
}

private void setCoordinates(long start, long end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Entry that associates a variant and a file in a variant archive. It contains
Expand Down Expand Up @@ -65,7 +66,7 @@ public AbstractVariantSourceEntry(String fileId, String studyId, String[] second
this.fileId = fileId;
this.studyId = studyId;
if (secondaryAlternates != null) {
this.secondaryAlternates = Arrays.copyOf(secondaryAlternates, secondaryAlternates.length);
setSecondaryAlternates(secondaryAlternates);
} else {
this.secondaryAlternates = new String[]{};
}
Expand Down Expand Up @@ -105,7 +106,10 @@ public String[] getSecondaryAlternates() {
}

public void setSecondaryAlternates(String[] secondaryAlternates) {
this.secondaryAlternates = secondaryAlternates;
this.secondaryAlternates = Arrays.stream(secondaryAlternates)
.map(a->a.toUpperCase())
.collect(Collectors.toList())
.toArray(new String[0]);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public VariantStatistics(String referenceAllele, String alternateAlleles,
int numMissingAlleles, int numMissingGenotypes, int numMendelErrors,
float percentCasesDominant,
float percentControlsDominant, float percentCasesRecessive, float percentControlsRecessive) {
this.refAllele = referenceAllele;
this.altAllele = alternateAlleles;
this.refAllele = Objects.nonNull(referenceAllele) ? referenceAllele.toUpperCase() : null;
this.altAllele = Objects.nonNull(alternateAlleles) ? alternateAlleles.toUpperCase() : null;
this.variantType = variantType;

this.maf = maf;
Expand Down Expand Up @@ -142,15 +142,15 @@ public String getRefAllele() {
}

public void setRefAllele(String refAllele) {
this.refAllele = refAllele;
this.refAllele = Objects.nonNull(refAllele) ? refAllele.toUpperCase() : null;
}

public String getAltAllele() {
return altAllele;
}

public void setAltAllele(String altAllele) {
this.altAllele = altAllele;
this.altAllele = Objects.nonNull(altAllele) ? altAllele.toUpperCase() : null;
}

public VariantType getVariantType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ public boolean isIncludeIds() {
}

protected String getReference(String[] fields) {
return fields[3].equals(".") ? "" : fields[3];
return fields[3].equals(".") ? "" : fields[3].toUpperCase();
}

protected String[] getAlternateAlleles(String[] fields) {
return fields[4].split(",");
return Arrays.stream(fields[4].split(","))
.map(a->a.toUpperCase())
.collect(Collectors.toList())
.toArray(new String[0]);
}

protected float getQuality(String[] fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public Genotype(String genotype) {
}

public Genotype(String genotype, String ref, String alt) {
this.reference = ref;
this.alternate = alt;
this.reference = Objects.nonNull(ref) ? ref.toUpperCase() : null;
this.alternate = Objects.nonNull(alt) ? alt.toUpperCase() : null;
this.phased = genotype.contains("|");
this.count = 0;
parseGenotype(genotype);
Expand Down Expand Up @@ -96,15 +96,15 @@ public String getReference() {
}

void setReference(String reference) {
this.reference = reference;
this.reference = Objects.nonNull(reference) ? reference.toUpperCase() : null;
}

public String getAlternate() {
return alternate;
}

void setAlternate(String alternate) {
this.alternate = alternate;
this.alternate = Objects.nonNull(alternate) ? alternate.toUpperCase() : null;
}

public int getAllele(int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public void testRefAltAssignmentInConstructor() {
assertEquals("T", variant.getAlternate());
}

@Test
public void testChangeRefAltToUpperCase() {
Variant variant = new Variant("1", 1, 1, "c", "t");
assertEquals("C", variant.getReference());
assertEquals("T", variant.getAlternate());
}

@Test
public void testRefAltAssignmentInRenormalization() {
Variant variant = new Variant("1", 1, 1, "", "T");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.ac.ebi.eva.commons.core.models;

import org.junit.Test;
import uk.ac.ebi.eva.commons.core.models.genotype.Genotype;

import static org.junit.Assert.assertEquals;

public class GenotypeTest {

@Test
public void testChangeRefAltToUpperCase() {
Genotype genotype = new Genotype("", "a", "t");

assertEquals("A", genotype.getReference());
assertEquals("T", genotype.getAlternate());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.ac.ebi.eva.commons.core.models;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class VariantStatisticsTest {

@Test
public void testChangeRefAltToUpperCase() {
VariantStatistics variantStatistics = new VariantStatistics("a", "t", null,
-1, -1, "", "", -1, -1, -1,
-1, -1, -1, -1);
assertEquals("A", variantStatistics.getRefAllele());
assertEquals("T", variantStatistics.getAltAllele());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ public void testVariantIds() {
Collections.singleton("rs123,rs456"));
}

@Test
public void testChangeRefAltToUpperCase(){
String line = "chr1\t1000\t.\tt\tg\t.\t.\t.";
List<Variant> expResult = Collections.singletonList(new Variant("chr1", 1000, 1000, "T", "G"));
List<Variant> result = factory.create(FILE_ID, STUDY_ID, line);
assertEquals(expResult, result);
}

private void checkIds(VariantVcfFactory variantVcfFactory, String vcfLine, Set<String> expectedIds) {
List<Variant> expectedVariants = new LinkedList<>();
expectedVariants.add(new Variant("1", 1000, 1000, "C", "T"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -201,8 +202,8 @@ public VariantMongo(String id, VariantType type, String chromosome, long start,
this.start = start;
this.end = end;
this.length = length;
this.reference = reference;
this.alternate = alternate;
this.reference = Objects.nonNull(reference) ? reference.toUpperCase() : null;
this.alternate = Objects.nonNull(alternate) ? alternate.toUpperCase() : null;
this.at = at;
this.hgvs = new LinkedHashSet<>();
if (hgvs != null && !hgvs.isEmpty()) {
Expand All @@ -229,6 +230,8 @@ public VariantMongo(String id, VariantType type, String chromosome, long start,

public static String buildVariantId(String chromosome, long start, String reference, String alternate) {
StringBuilder builder = new StringBuilder(chromosome);
reference = reference.toUpperCase();
alternate = alternate.toUpperCase();
builder.append("_");
builder.append(start);
builder.append("_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static uk.ac.ebi.eva.commons.mongodb.entities.VariantMongo.ALTERNATE_FIELD;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class SimplifiedVariant {

public SimplifiedVariant(VariantType variantType, String chromosome, long start, long end, int length,
String reference, String alternate, Map<String, Set<String>> hgvs) {
reference = Objects.nonNull(reference) ? reference.toUpperCase() : null;
alternate = Objects.nonNull(alternate) ? alternate.toUpperCase() : null;
this.id = buildVariantId(chromosome, start, reference, alternate);
this.variantType = variantType;
this.chromosome = chromosome;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -100,8 +101,10 @@ public VariantSourceEntryMongo(String fileId, String studyId, String[] alternate
this.fileId = fileId;
this.studyId = studyId;
if (alternates != null && alternates.length > 0) {
this.alternates = new String[alternates.length];
System.arraycopy(alternates, 0, this.alternates, 0, alternates.length);
this.alternates = Arrays.stream(alternates)
.map(a->a.toUpperCase())
.collect(Collectors.toList())
.toArray(new String[0]);
}

if (attributes != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Class for building filters for querying using the VariantRepository
Expand Down Expand Up @@ -114,21 +115,22 @@ public FilterBuilder withVariantTypes(VariantType variantType) {

public FilterBuilder withAlternates(List<String> alternates) {
if (alternates != null && !alternates.isEmpty()) {
filters.add(new VariantRepositoryAlternateFilter(alternates));
List<String> alternatesUppercase = alternates.stream().map(a->a.toUpperCase()).collect(Collectors.toList());
filters.add(new VariantRepositoryAlternateFilter(alternatesUppercase));
}
return this;
}

public FilterBuilder withAlternates(String alternate) {
if (alternate != null) {
filters.add(new VariantRepositoryAlternateFilter(Collections.singletonList(alternate)));
filters.add(new VariantRepositoryAlternateFilter(Collections.singletonList(alternate.toUpperCase())));
}
return this;
}

public FilterBuilder withReferenceBases(String referenceBases) {
if (referenceBases != null) {
filters.add(new VariantRepositoryReferenceBasesFilter(Collections.singletonList(referenceBases)));
filters.add(new VariantRepositoryReferenceBasesFilter(Collections.singletonList(referenceBases.toUpperCase())));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.lordofthejars.nosqlunit.mongodb.MongoDbConfigurationBuilder;
import com.lordofthejars.nosqlunit.mongodb.MongoDbRule;

import org.bson.BsonArray;
import org.bson.BsonString;
import org.bson.Document;
Expand All @@ -31,7 +30,6 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import uk.ac.ebi.eva.commons.core.models.pipeline.VariantSourceEntry;
import uk.ac.ebi.eva.commons.core.models.ws.VariantSourceEntryWithSampleNames;
import uk.ac.ebi.eva.commons.core.models.ws.VariantWithSamplesAndAnnotation;
Expand Down Expand Up @@ -180,7 +178,7 @@ private Document buildMongoVariantWithFiles() {

private Document buildMongoBasicVariant() {
VariantWithSamplesAndAnnotation variant = new VariantWithSamplesAndAnnotation(CHROMOSOME, START, END, REFERENCE,
ALTERNATE, null);
ALTERNATE, null);
Document mongoVariant = new Document("_id", VARIANT_ID)
.append(VariantMongo.IDS_FIELD, Collections.singleton(RS_666))
.append(VariantMongo.TYPE_FIELD, variant.getType().name())
Expand Down Expand Up @@ -249,4 +247,13 @@ public void testConvertToDataModelTypeEmptyIds() {
assertNotNull(variant.getIds());
assertTrue(variant.getIds().isEmpty());
}

@Test
public void testChangeRefAltToUpperCase() {
VariantMongo variantMongo = new VariantMongo(new VariantWithSamplesAndAnnotation("chr1", START,
END, "a", "t", null));
Assert.assertEquals("A", variantMongo.getReference());
Assert.assertEquals("T", variantMongo.getAlternate());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package uk.ac.ebi.eva.commons.mongodb;

import org.junit.Test;
import uk.ac.ebi.eva.commons.mongodb.entities.projections.SimplifiedVariant;

import java.lang.reflect.Field;

import static org.junit.Assert.assertEquals;

public class SimplifiedVariantTest {

@Test
public void testChangeRefAltToUpperCase() throws NoSuchFieldException, IllegalAccessException {
SimplifiedVariant simplifiedVariant = new SimplifiedVariant(null, "", -1, -1,
-1, "a", "t", null);

Field refField = SimplifiedVariant.class.getDeclaredField("reference");
Field altField = SimplifiedVariant.class.getDeclaredField("alternate");
refField.setAccessible(true);
altField.setAccessible(true);
String refValue = (String) refField.get(simplifiedVariant);
String altValue = (String) altField.get(simplifiedVariant);

assertEquals("A", refValue);
assertEquals("T", altValue);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,19 @@ public void testConvertFromVariantSourceEntryMongoToVariantSourceEntry() {
Assert.assertEquals("1/1", variantSourceEntryWithSampleNames.getSamplesDataMap().get("NA003").get("GT"));
}

@Test
public void testChangeRefAltToUpperCaseVariantSourceEntry() {
VariantSourceEntry variantSourceEntry = new VariantSourceEntry(null, "", new String[]{"a"},
null, null, null, null);
Assert.assertEquals("A", variantSourceEntry.getSecondaryAlternates()[0]);

}

@Test
public void testChangeRefAltToUpperCaseVariantEntrySourceMongo() {
VariantSourceEntryMongo variantSourceEntryMongo = new VariantSourceEntryMongo(new VariantSourceEntry(null,
"", new String[]{"a"}, null, null, null, null));
Assert.assertEquals( "A", variantSourceEntryMongo.getSecondaryAlternates()[0]);
}

}
Loading

0 comments on commit 923accf

Please sign in to comment.