JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / ext / ensembl / EnsemblGene.java
index cdcfa96..75a7154 100644 (file)
@@ -23,7 +23,6 @@ package jalview.ext.ensembl;
 import jalview.api.FeatureColourI;
 import jalview.api.FeatureSettingsModelI;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GeneLociI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
@@ -34,6 +33,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,8 +51,6 @@ 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
@@ -64,6 +62,8 @@ public class EnsemblGene extends EnsemblSeqProxy
       EnsemblFeatureType.exon, EnsemblFeatureType.cds,
       EnsemblFeatureType.variation };
 
+  private static final String CHROMOSOME = "chromosome";
+
   /**
    * Default constructor (to use rest.ensembl.org)
    */
@@ -100,6 +100,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 +140,6 @@ public class EnsemblGene extends EnsemblSeqProxy
      * convert to a non-duplicated list of gene identifiers
      */
     List<String> geneIds = getGeneIds(query);
-
     AlignmentI al = null;
     for (String geneId : geneIds)
     {
@@ -146,9 +151,11 @@ public class EnsemblGene extends EnsemblSeqProxy
       {
         continue;
       }
-      
+
       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);
       }
@@ -179,7 +186,7 @@ public class EnsemblGene extends EnsemblSeqProxy
     if (geneLoci != null)
     {
       seq.setGeneLoci(geneLoci.getSpeciesId(), geneLoci.getAssemblyId(),
-              geneLoci.getChromosomeId(), geneLoci.getMap());
+              geneLoci.getChromosomeId(), geneLoci.getMapping());
     }
     else
     {
@@ -201,7 +208,7 @@ public class EnsemblGene extends EnsemblSeqProxy
       return false;
     }
     String[] tokens = description.split(":");
-    if (tokens.length == 6 && tokens[0].startsWith(DBRefEntry.CHROMOSOME))
+    if (tokens.length == 6 && tokens[0].startsWith(CHROMOSOME))
     {
       String ref = tokens[1];
       String chrom = tokens[2];
@@ -235,7 +242,7 @@ public class EnsemblGene extends EnsemblSeqProxy
    */
   List<String> getGeneIds(String accessions)
   {
-    List<String> geneIds = new ArrayList<String>();
+    List<String> geneIds = new ArrayList<>();
 
     for (String acc : accessions.split(getAccessionSeparator()))
     {
@@ -311,8 +318,8 @@ public class EnsemblGene extends EnsemblSeqProxy
         SequenceOntologyI.NMD_TRANSCRIPT_VARIANT,
         SequenceOntologyI.TRANSCRIPT, SequenceOntologyI.EXON,
         SequenceOntologyI.CDS };
-    List<SequenceFeature> sfs = gene.getFeatures().getFeaturesByOntology(
-            soTerms);
+    List<SequenceFeature> sfs = gene.getFeatures()
+            .getFeaturesByOntology(soTerms);
     for (SequenceFeature sf : sfs)
     {
       gene.deleteFeature(sf);
@@ -358,7 +365,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<SequenceFeature> splices = findFeatures(gene,
             SequenceOntologyI.EXON, parentId);
     if (splices.isEmpty())
@@ -370,7 +377,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<int[]> mappedFrom = new ArrayList<int[]>();
+    List<int[]> mappedFrom = new ArrayList<>();
 
     for (SequenceFeature sf : splices)
     {
@@ -389,7 +396,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);
@@ -412,7 +419,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<int[]> mapTo = new ArrayList<int[]>();
+    List<int[]> mapTo = new ArrayList<>();
     mapTo.add(new int[] { 1, transcriptLength });
     MapList mapping = new MapList(mappedFrom, mapTo, 1, 1);
     EnsemblCdna cdna = new EnsemblCdna(getDomain());
@@ -452,7 +459,7 @@ public class EnsemblGene extends EnsemblSeqProxy
       return;
     }
 
-    MapList geneMapping = loci.getMap();
+    MapList geneMapping = loci.getMapping();
 
     List<int[]> exons = mapping.getFromRanges();
     List<int[]> transcriptLoci = new ArrayList<>();
@@ -462,8 +469,9 @@ public class EnsemblGene extends EnsemblSeqProxy
       transcriptLoci.add(geneMapping.locateInTo(exon[0], exon[1]));
     }
 
-    List<int[]> transcriptRange = Arrays.asList(new int[] {
-        transcript.getStart(), transcript.getEnd() });
+    List<int[]> transcriptRange = Arrays
+            .asList(new int[]
+            { transcript.getStart(), transcript.getEnd() });
     MapList mapList = new MapList(transcriptRange, transcriptLoci, 1, 1);
 
     transcript.setGeneLoci(loci.getSpeciesId(), loci.getAssemblyId(),
@@ -478,7 +486,7 @@ public class EnsemblGene extends EnsemblSeqProxy
    */
   protected String getTranscriptId(SequenceFeature feature)
   {
-    return (String) feature.getValue("transcript_id");
+    return (String) feature.getValue(JSON_ID);
   }
 
   /**
@@ -498,9 +506,9 @@ public class EnsemblGene extends EnsemblSeqProxy
   protected List<SequenceFeature> getTranscriptFeatures(String accId,
           SequenceI geneSequence)
   {
-    List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
+    List<SequenceFeature> transcriptFeatures = new ArrayList<>();
 
-    String parentIdentifier = GENE_PREFIX + accId;
+    String parentIdentifier = accId;
 
     List<SequenceFeature> sfs = geneSequence.getFeatures()
             .getFeaturesByOntology(SequenceOntologyI.TRANSCRIPT);
@@ -510,7 +518,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);
       }
@@ -531,29 +539,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<SequenceFeature> getIdentifyingFeatures(SequenceI seq,
+          String accId)
   {
-    if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
-            SequenceOntologyI.GENE))
+    List<SequenceFeature> result = new ArrayList<>();
+    List<SequenceFeature> 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;
   }
 
   /**
@@ -575,7 +588,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;
       }
@@ -584,17 +597,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
    */
@@ -627,10 +629,10 @@ public class EnsemblGene extends EnsemblSeqProxy
       SequenceOntologyI so = SequenceOntologyFactory.getInstance();
 
       @Override
-      public boolean isFeatureDisplayed(String type)
+      public boolean isFeatureHidden(String type)
       {
-        return (so.isA(type, SequenceOntologyI.EXON)
-                || so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT));
+        return (!so.isA(type, SequenceOntologyI.EXON)
+                && !so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT));
       }
 
       @Override