X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Fensembl%2FEnsemblGene.java;h=63a6a6ca7a0589f44034d89dfd5508087128686e;hb=990ed4ffbaa7a95b2ebb6bf6ab0440310f6e83ab;hp=afff4c2630609be55291630e560b3c23e0bdbf05;hpb=a57976ba40e1abe6d7c1940386e1a25419ef9c9d;p=jalview.git diff --git a/src/jalview/ext/ensembl/EnsemblGene.java b/src/jalview/ext/ensembl/EnsemblGene.java index afff4c2..63a6a6c 100644 --- a/src/jalview/ext/ensembl/EnsemblGene.java +++ b/src/jalview/ext/ensembl/EnsemblGene.java @@ -34,6 +34,7 @@ import jalview.io.gff.SequenceOntologyI; import jalview.schemes.FeatureColour; import jalview.schemes.FeatureSettingsAdapter; import jalview.util.MapList; +import jalview.util.Platform; import java.awt.Color; import java.io.UnsupportedEncodingException; @@ -51,13 +52,11 @@ import com.stevesoft.pat.Regex; */ public class EnsemblGene extends EnsemblSeqProxy { - private static final String GENE_PREFIX = "gene:"; - /* * accepts anything as we will attempt lookup of gene or * transcript id or gene name */ - private static final Regex ACCESSION_REGEX = new Regex(".*"); + private static Regex ACCESSION_REGEX; private static final EnsemblFeatureType[] FEATURES_TO_FETCH = { EnsemblFeatureType.gene, EnsemblFeatureType.transcript, @@ -100,6 +99,12 @@ public class EnsemblGene extends EnsemblSeqProxy return EnsemblSeqType.GENOMIC; } + @Override + protected String getObjectType() + { + return OBJECT_TYPE_GENE; + } + /** * Returns an alignment containing the gene(s) for the given gene or * transcript identifier, or external identifier (e.g. Uniprot id). If given a @@ -134,7 +139,6 @@ public class EnsemblGene extends EnsemblSeqProxy * convert to a non-duplicated list of gene identifiers */ List geneIds = getGeneIds(query); - AlignmentI al = null; for (String geneId : geneIds) { @@ -147,10 +151,11 @@ public class EnsemblGene extends EnsemblSeqProxy continue; } - parseChromosomeLocations(geneAlignment); - if (geneAlignment.getHeight() == 1) { + // ensure id has 'correct' case for the Ensembl identifier + geneId = geneAlignment.getSequenceAt(0).getName(); + findGeneLoci(geneAlignment.getSequenceAt(0), geneId); getTranscripts(geneAlignment, geneId); } if (al == null) @@ -166,42 +171,64 @@ public class EnsemblGene extends EnsemblSeqProxy } /** + * Calls the /lookup/id REST service, parses the response for gene + * coordinates, and if successful, adds these to the sequence. If this fails, + * fall back on trying to parse the sequence description in case it is in + * Ensembl-gene format e.g. chromosome:GRCh38:17:45051610:45109016:1. + * + * @param seq + * @param geneId + */ + void findGeneLoci(SequenceI seq, String geneId) + { + GeneLociI geneLoci = new EnsemblLookup(getDomain()).getGeneLoci(geneId); + if (geneLoci != null) + { + seq.setGeneLoci(geneLoci.getSpeciesId(), geneLoci.getAssemblyId(), + geneLoci.getChromosomeId(), geneLoci.getMap()); + } + else + { + parseChromosomeLocations(seq); + } + } + + /** * Parses and saves fields of an Ensembl-style description e.g. * chromosome:GRCh38:17:45051610:45109016:1 * - * @param alignment + * @param seq */ - private void parseChromosomeLocations(AlignmentI alignment) + boolean parseChromosomeLocations(SequenceI seq) { - for (SequenceI seq : alignment.getSequences()) + String description = seq.getDescription(); + if (description == null) { - String description = seq.getDescription(); - if (description == null) + return false; + } + String[] tokens = description.split(":"); + if (tokens.length == 6 && tokens[0].startsWith(DBRefEntry.CHROMOSOME)) + { + String ref = tokens[1]; + String chrom = tokens[2]; + try { - continue; - } - String[] tokens = description.split(":"); - if (tokens.length == 6 && tokens[0].startsWith(DBRefEntry.CHROMOSOME)) + int chStart = Integer.parseInt(tokens[3]); + int chEnd = Integer.parseInt(tokens[4]); + boolean forwardStrand = "1".equals(tokens[5]); + String species = ""; // not known here + int[] from = new int[] { seq.getStart(), seq.getEnd() }; + int[] to = new int[] { forwardStrand ? chStart : chEnd, + forwardStrand ? chEnd : chStart }; + MapList map = new MapList(from, to, 1, 1); + seq.setGeneLoci(species, ref, chrom, map); + return true; + } catch (NumberFormatException e) { - String ref = tokens[1]; - String chrom = tokens[2]; - try - { - int chStart = Integer.parseInt(tokens[3]); - int chEnd = Integer.parseInt(tokens[4]); - boolean forwardStrand = "1".equals(tokens[5]); - String species = ""; // dunno yet! - int[] from = new int[] { seq.getStart(), seq.getEnd() }; - int[] to = new int[] { forwardStrand ? chStart : chEnd, - forwardStrand ? chEnd : chStart }; - MapList map = new MapList(from, to, 1, 1); - seq.setGeneLoci(species, ref, chrom, map); - } catch (NumberFormatException e) - { - System.err.println("Bad integers in description " + description); - } + System.err.println("Bad integers in description " + description); } } + return false; } /** @@ -214,7 +241,7 @@ public class EnsemblGene extends EnsemblSeqProxy */ List getGeneIds(String accessions) { - List geneIds = new ArrayList(); + List geneIds = new ArrayList<>(); for (String acc : accessions.split(getAccessionSeparator())) { @@ -337,7 +364,7 @@ public class EnsemblGene extends EnsemblSeqProxy * look for exon features of the transcript, failing that for CDS * (for example ENSG00000124610 has 1 CDS but no exon features) */ - String parentId = "transcript:" + accId; + String parentId = accId; List splices = findFeatures(gene, SequenceOntologyI.EXON, parentId); if (splices.isEmpty()) @@ -349,7 +376,7 @@ public class EnsemblGene extends EnsemblSeqProxy int transcriptLength = 0; final char[] geneChars = gene.getSequence(); int offset = gene.getStart(); // to convert to 0-based positions - List mappedFrom = new ArrayList(); + List mappedFrom = new ArrayList<>(); for (SequenceFeature sf : splices) { @@ -368,7 +395,7 @@ public class EnsemblGene extends EnsemblSeqProxy * Ensembl has gene name as transcript Name * EnsemblGenomes doesn't, but has a url-encoded description field */ - String description = (String) transcriptFeature.getValue(NAME); + String description = transcriptFeature.getDescription(); if (description == null) { description = (String) transcriptFeature.getValue(DESCRIPTION); @@ -391,7 +418,7 @@ public class EnsemblGene extends EnsemblSeqProxy * transfer features to the new sequence; we use EnsemblCdna to do this, * to filter out unwanted features types (see method retainFeature) */ - List mapTo = new ArrayList(); + List mapTo = new ArrayList<>(); mapTo.add(new int[] { 1, transcriptLength }); MapList mapping = new MapList(mappedFrom, mapTo, 1, 1); EnsemblCdna cdna = new EnsemblCdna(getDomain()); @@ -457,7 +484,7 @@ public class EnsemblGene extends EnsemblSeqProxy */ protected String getTranscriptId(SequenceFeature feature) { - return (String) feature.getValue("transcript_id"); + return (String) feature.getValue(JSON_ID); } /** @@ -477,9 +504,9 @@ public class EnsemblGene extends EnsemblSeqProxy protected List getTranscriptFeatures(String accId, SequenceI geneSequence) { - List transcriptFeatures = new ArrayList(); + List transcriptFeatures = new ArrayList<>(); - String parentIdentifier = GENE_PREFIX + accId; + String parentIdentifier = accId; List sfs = geneSequence.getFeatures() .getFeaturesByOntology(SequenceOntologyI.TRANSCRIPT); @@ -489,7 +516,7 @@ public class EnsemblGene extends EnsemblSeqProxy for (SequenceFeature sf : sfs) { String parent = (String) sf.getValue(PARENT); - if (parentIdentifier.equals(parent)) + if (parentIdentifier.equalsIgnoreCase(parent)) { transcriptFeatures.add(sf); } @@ -510,29 +537,34 @@ public class EnsemblGene extends EnsemblSeqProxy @Override public String getTestQuery() { - return "ENSG00000157764"; // BRAF, 5 transcripts, reverse strand + return Platform.isJS() ? "ENSG00000123569" : "ENSG00000157764"; + // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand + // ENSG00000157764 // BRAF, 5 transcripts, reverse strand // ENSG00000090266 // NDUFB2, 15 transcripts, forward strand // ENSG00000101812 // H2BFM histone, 3 transcripts, forward strand - // ENSG00000123569 // H2BFWT histone, 2 transcripts, reverse strand } /** - * Answers true for a feature of type 'gene' (or a sub-type of gene in the - * Sequence Ontology), whose ID is the accession we are retrieving + * Answers a list of sequence features (if any) whose type is 'gene' (or a + * subtype of gene in the Sequence Ontology), and whose ID is the accession we + * are retrieving */ @Override - protected boolean identifiesSequence(SequenceFeature sf, String accId) + protected List getIdentifyingFeatures(SequenceI seq, + String accId) { - if (SequenceOntologyFactory.getInstance().isA(sf.getType(), - SequenceOntologyI.GENE)) + List result = new ArrayList<>(); + List sfs = seq.getFeatures() + .getFeaturesByOntology(SequenceOntologyI.GENE); + for (SequenceFeature sf : sfs) { - String id = (String) sf.getValue(ID); - if ((GENE_PREFIX + accId).equals(id)) + String id = (String) sf.getValue(JSON_ID); + if (accId.equalsIgnoreCase(id)) { - return true; + result.add(sf); } } - return false; + return result; } /** @@ -545,7 +577,7 @@ public class EnsemblGene extends EnsemblSeqProxy @Override protected boolean retainFeature(SequenceFeature sf, String accessionId) { - SequenceOntologyI so = SequenceOntologyFactory.getInstance(); + SequenceOntologyI so = SequenceOntologyFactory.getSequenceOntology(); String type = sf.getType(); if (so.isA(type, SequenceOntologyI.GENE)) { @@ -554,7 +586,7 @@ public class EnsemblGene extends EnsemblSeqProxy if (isTranscript(type)) { String parent = (String) sf.getValue(PARENT); - if (!(GENE_PREFIX + accessionId).equals(parent)) + if (!accessionId.equalsIgnoreCase(parent)) { return false; } @@ -563,17 +595,6 @@ public class EnsemblGene extends EnsemblSeqProxy } /** - * Answers false. This allows an optimisation - a single 'gene' feature is all - * that is needed to identify the positions of the gene on the genomic - * sequence. - */ - @Override - protected boolean isSpliceable() - { - return false; - } - - /** * Override to do nothing as Ensembl doesn't return a protein sequence for a * gene identifier */ @@ -585,6 +606,10 @@ public class EnsemblGene extends EnsemblSeqProxy @Override public Regex getAccessionValidator() { + if (ACCESSION_REGEX == null) + { + ACCESSION_REGEX = Platform.newRegex(".*", null); + } return ACCESSION_REGEX; } @@ -603,7 +628,7 @@ public class EnsemblGene extends EnsemblSeqProxy { return new FeatureSettingsAdapter() { - SequenceOntologyI so = SequenceOntologyFactory.getInstance(); + SequenceOntologyI so = SequenceOntologyFactory.getSequenceOntology(); @Override public boolean isFeatureDisplayed(String type)