Skip to content
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
@@ -0,0 +1,134 @@
/*
* Copyright 2025 EMBL - European Bioinformatics Institute
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package uk.ac.ebi.embl.gff3tools.validation.fix;

import static uk.ac.ebi.embl.gff3tools.validation.meta.ValidationType.ANNOTATION;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import uk.ac.ebi.embl.gff3tools.gff3.GFF3Annotation;
import uk.ac.ebi.embl.gff3tools.gff3.GFF3Attributes;
import uk.ac.ebi.embl.gff3tools.gff3.GFF3Feature;
import uk.ac.ebi.embl.gff3tools.utils.ConversionUtils;
import uk.ac.ebi.embl.gff3tools.utils.OntologyClient;
import uk.ac.ebi.embl.gff3tools.utils.OntologyTerm;
import uk.ac.ebi.embl.gff3tools.validation.meta.FixMethod;
import uk.ac.ebi.embl.gff3tools.validation.meta.Gff3Fix;

@Slf4j
@Gff3Fix(
name = "CDS_RNA_LOCUS",
description =
"Transfers gene, gene_synonym, and locus_tag attributes from gene features to their corresponding CDS, rRNA, and tRNA child features based on location overlap.")
public class CdsRnaLocusFix {

private final OntologyClient ontologyClient = ConversionUtils.getOntologyClient();

@FixMethod(
rule = "CDS_RNA_LOCUS",
description =
"Transfers gene, gene_synonym, and locus_tag attributes from gene features to their corresponding CDS, rRNA, and tRNA child features based on location overlap.",
type = ANNOTATION)
public void fix(GFF3Annotation annotation, int line) {
List<GFF3Feature> geneFeatures = new ArrayList<>();
List<GFF3Feature> nonLocusFeatures = new ArrayList<>();
for (GFF3Feature feature : annotation.getFeatures()) {
Optional<String> soIdOpt = ontologyClient.findTermByNameOrSynonym(feature.getName());
if (soIdOpt.isEmpty()) return;

String soId = soIdOpt.get();
// Determine if this is a gene feature
boolean isGene = OntologyTerm.GENE.ID.equals(soId)
|| OntologyTerm.PSEUDOGENE.ID.equals(soId)
|| OntologyTerm.UNITARY_PSEUDOGENE.ID.equals(soId)
|| ontologyClient.isSelfOrDescendantOf(soId, OntologyTerm.PSEUDOGENE.ID)
|| ontologyClient.isSelfOrDescendantOf(soId, OntologyTerm.UNITARY_PSEUDOGENE.ID);

// Determine if this is a relevant CDS/tRNA/rRNA feature
boolean isRelevant = OntologyTerm.CDS.ID.equals(soId)
|| OntologyTerm.TRNA.ID.equals(soId)
|| OntologyTerm.RRNA.ID.equals(soId)
|| ontologyClient.isSelfOrDescendantOf(soId, OntologyTerm.CDS.ID)
|| ontologyClient.isSelfOrDescendantOf(soId, OntologyTerm.TRNA.ID)
|| ontologyClient.isSelfOrDescendantOf(soId, OntologyTerm.RRNA.ID);

// Gene feature: store in gene list
if (isGene) {
geneFeatures.add(feature);
continue;
}

if (isRelevant) {
boolean hasGeneFields = feature.hasAttribute(GFF3Attributes.LOCUS_TAG)
|| feature.hasAttribute(GFF3Attributes.GENE)
|| feature.hasAttribute(GFF3Attributes.GENE_SYNONYM);

if (!hasGeneFields) {
nonLocusFeatures.add(feature);
}
}
}

if (geneFeatures.isEmpty() || nonLocusFeatures.isEmpty()) {
return;
}

// Second pass: propagate gene attributes to relevant features
for (GFF3Feature child : nonLocusFeatures) {

for (GFF3Feature gene : geneFeatures) {

if (isLocationWithin(child.getStart(), child.getEnd(), gene.getStart(), gene.getEnd())) {
propagateGeneAttributes(gene, child, line);
break;
}
}
}
}

private boolean isLocationWithin(long start1, long end1, long start2, long end2) {
return start1 >= start2 && end1 <= end2;
}

private void propagateGeneAttributes(GFF3Feature geneFeature, GFF3Feature childFeature, int line) {
String locusTag = geneFeature.getAttributeByName(GFF3Attributes.LOCUS_TAG);
String gene = geneFeature.getAttributeByName(GFF3Attributes.GENE);
String geneSynonym = geneFeature.getAttributeByName(GFF3Attributes.GENE_SYNONYM);

if (locusTag != null && !childFeature.hasAttribute(GFF3Attributes.LOCUS_TAG)) {
childFeature.setAttribute(GFF3Attributes.LOCUS_TAG, locusTag);
log.info(
"Adding {} from gene {} to {} at line {}",
GFF3Attributes.LOCUS_TAG,
geneFeature.getName(),
childFeature.getName(),
line);
}
if (geneSynonym != null && !childFeature.hasAttribute(GFF3Attributes.GENE_SYNONYM)) {
childFeature.setAttribute(GFF3Attributes.GENE_SYNONYM, geneSynonym);
log.info(
"Adding {} from gene {} to {} at line {}",
GFF3Attributes.GENE_SYNONYM,
geneFeature.getName(),
childFeature.getName(),
line);
}
if (gene != null && !childFeature.hasAttribute(GFF3Attributes.GENE)) {
childFeature.setAttribute(GFF3Attributes.GENE, gene);
log.info(
"Adding {} from gene {} to {} at line {}",
GFF3Attributes.GENE,
geneFeature.getName(),
childFeature.getName(),
line);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import static uk.ac.ebi.embl.gff3tools.validation.meta.ValidationType.FEATURE;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import uk.ac.ebi.embl.gff3tools.gff3.GFF3Attributes;
Expand All @@ -20,26 +22,85 @@
import uk.ac.ebi.embl.gff3tools.validation.meta.Gff3Fix;

@Slf4j
@Gff3Fix(name = "EC_NUMBER_ATTRIBUTE", description = "Remove the EC_number attribute if not matches the pattern")
@Gff3Fix(name = "EC_NUMBER", description = "Remove the EC_number attribute if not matches the pattern")
public class EcNumberValueFix {

private static final Pattern EC_NUMBER_PATTERN =
Pattern.compile("^[0-9]+(\\.(?:[0-9]+|-)){0,2}\\.(?:[0-9]+|-|n[0-9]*)$");
private static final Pattern PRODUCT_EC_NUMBER_PATTERN = Pattern.compile(
"\\(?\\[?(?:EC|ec|Ec)[:=]?\\s*((?:\\d{1,3}|-)\\.(?:\\d{1,3}|-)\\.(?:\\d{1,3}|-)\\.(?:\\d{1,3}|-))]?\\)?");

@FixMethod(
rule = "EC_NUMBER_ATTRIBUTE",
rule = "EC_NUMBER",
description = "Remove the EC_number attribute if not matches the pattern",
type = FEATURE)
public void fixFeature(GFF3Feature feature, int line) {

if (!feature.hasAttribute(GFF3Attributes.EC_NUMBER) && !feature.hasAttribute(GFF3Attributes.PRODUCT)) {
return;
}

String ecNumber = feature.getAttributeByName(GFF3Attributes.EC_NUMBER);
if (ecNumber == null || ecNumber.isBlank()) return;

if (ecNumber.equalsIgnoreCase("deleted") || !isValidECNumber(ecNumber.trim())) {
log.info("Removing invalid values on {} attribute at line: {}", GFF3Attributes.EC_NUMBER, line);
if (ecNumber != null) {
if ("deleted".equalsIgnoreCase(ecNumber) || !isValidECNumber(ecNumber.trim())) {
log.info(
"Fix to remove {} attribute due to invalid value '{}' at line: {}",
GFF3Attributes.EC_NUMBER,
ecNumber,
line);
feature.removeAttribute(GFF3Attributes.EC_NUMBER);
return;
}
}

List<String> productValues = feature.getAttributeValueList(GFF3Attributes.PRODUCT);
if (productValues == null || productValues.isEmpty()) {
return;
}

boolean hasInvalidProduct = productValues.stream()
.anyMatch(p -> p != null
&& ("hypothetical protein".equalsIgnoreCase(p.trim()) || "unknown".equalsIgnoreCase(p.trim())));

if (hasInvalidProduct) {
log.info("Fix: removing EC_NUMBER because product is 'hypothetical protein' or 'unknown' at line {}", line);
feature.removeAttribute(GFF3Attributes.EC_NUMBER);
return;
}

for (String product : productValues) {
if (product == null) continue;
if (hasEcNumber(product)) {
String extracted = getEcNumberFromProduct(product);

if (!extracted.isEmpty() && isValidECNumber(extracted)) {

log.info(
"Fix: setting EC_NUMBER='{}' extracted from product '{}' at line {}",
extracted,
product,
line);

feature.setAttribute(GFF3Attributes.EC_NUMBER, extracted);
return;
}
}
}
}

private String getEcNumberFromProduct(String product) {
Matcher matcher = PRODUCT_EC_NUMBER_PATTERN.matcher(product);
if (matcher.find()) {
return matcher.group(1).trim();
}
return "";
}

private boolean hasEcNumber(String product) {
return PRODUCT_EC_NUMBER_PATTERN.matcher(product).find();
}

private boolean isValidECNumber(String ecNumber) {
return EC_NUMBER_PATTERN.matcher(ecNumber).matches();
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/uk/ac/ebi/embl/gff3tools/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ public static GFF3Feature createGFF3FeatureWithAccession(
);
}

public static GFF3Feature createGFF3Feature(
String featureName, String seqId, long start, long end, Map<String, Object> attributes) {

return new GFF3Feature(
Optional.of(featureName),
Optional.empty(),
seqId,
Optional.empty(),
".",
featureName,
start,
end,
".",
"+",
"",
attributes);
}

public static String defaultAccession() {
return DEFAULT_ACCESSION;
}
Expand Down
Loading