Merge branch 'feature/JAL-3143ensemblJSON' into trialMerge
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 29 Jan 2019 11:32:18 +0000 (11:32 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 29 Jan 2019 11:32:18 +0000 (11:32 +0000)
Conflicts:
src/jalview/ext/ensembl/EnsemblFeatures.java

1  2 
src/jalview/analysis/AlignmentUtils.java
src/jalview/ext/ensembl/EnsemblFeatures.java

@@@ -74,12 -74,15 +74,15 @@@ import java.util.TreeMap
   */
  public class AlignmentUtils
  {
    private static final int CODON_LENGTH = 3;
  
    private static final String SEQUENCE_VARIANT = "sequence_variant:";
  
-   private static final String ID = "ID";
+   /*
+    * the 'id' attribute is provided for variant features fetched from
+    * Ensembl using its REST service with JSON format 
+    */
+   public static final String VARIANT_ID = "id";
  
    /**
     * A data model to hold the 'normal' base value at a position, and an optional
        return false;
      }
      String name = seq2.getName();
 -    final DBRefEntry[] xrefs = seq1.getDBRefs();
 +    final List<DBRefEntry> xrefs = seq1.getDBRefs();
      if (xrefs != null)
      {
 -      for (DBRefEntry xref : xrefs)
 +      for (int ix = 0, nx = xrefs.size(); ix < nx; ix++)
        {
 +        DBRefEntry xref = xrefs.get(ix);
          String xrefName = xref.getSource() + "|" + xref.getAccessionId();
          // case-insensitive test, consistent with DBRefEntry.equalRef()
          if (xrefName.equalsIgnoreCase(name))
            // need to
            // synthesize an xref.
  
 -          for (DBRefEntry primRef : dnaDss.getPrimaryDBRefs())
 +          List<DBRefEntry> primrefs = dnaDss.getPrimaryDBRefs();
 +          for (int ip = 0, np = primrefs.size(); ip < np; ip++)
            {
 +                DBRefEntry primRef = primrefs.get(ip);
              /*
               * create a cross-reference from CDS to the source sequence's
               * primary reference and vice versa
  
              dnaSeq.addDBRef(new DBRefEntry(source, version, cdsSeq
                      .getName(), new Mapping(cdsSeqDss, dnaToCdsMap)));
 -
              // problem here is that the cross-reference is synthesized -
              // cdsSeq.getName() may be like 'CDS|dnaaccession' or
              // 'CDS|emblcdsacc'
                      .getInverse()));
              proteinProduct.addDBRef(proteinToCdsRef);
            }
 -
            /*
             * transfer any features on dna that overlap the CDS
             */
      List<DBRefEntry> direct = new ArrayList<>();
      HashSet<String> directSources = new HashSet<>();
  
 -    if (contig.getDBRefs() != null)
 +    List<DBRefEntry> refs = contig.getDBRefs();
 +    if (refs != null)
      {
 -      for (DBRefEntry dbr : contig.getDBRefs())
 +      for (int ib = 0, nb = refs.size(); ib < nb; ib++)
        {
 -        if (dbr.hasMap() && dbr.getMap().getMap().isTripletMap())
 +        DBRefEntry dbr = refs.get(ib);
 +        MapList map;
 +        if (dbr.hasMap() && (map = dbr.getMap().getMap()).isTripletMap())
          {
 -          MapList map = dbr.getMap().getMap();
            // check if map is the CDS mapping
            if (mapping.getMap().equals(map))
            {
          }
        }
      }
 -    DBRefEntry[] onSource = DBRefUtils.selectRefs(
 +    List<DBRefEntry> onSource = DBRefUtils.selectRefs(
              proteinProduct.getDBRefs(),
              directSources.toArray(new String[0]));
      List<DBRefEntry> propagated = new ArrayList<>();
  
      // and generate appropriate mappings
 -    for (DBRefEntry cdsref : direct)
 +    for (int ic = 0, nc = direct.size(); ic < nc; ic++)
      {
 +      DBRefEntry cdsref = direct.get(ic);
 +      Mapping m = cdsref.getMap();
        // clone maplist and mapping
        MapList cdsposmap = new MapList(
                Arrays.asList(new int[][]
                { new int[] { cdsSeq.getStart(), cdsSeq.getEnd() } }),
 -              cdsref.getMap().getMap().getToRanges(), 3, 1);
 -      Mapping cdsmap = new Mapping(cdsref.getMap().getTo(),
 -              cdsref.getMap().getMap());
 +              m.getMap().getToRanges(), 3, 1);
 +      Mapping cdsmap = new Mapping(m.getTo(),m.getMap());
  
        // create dbref
        DBRefEntry newref = new DBRefEntry(cdsref.getSource(),
        int phase = 0;
        try
        {
 -        phase = Integer.parseInt(sf.getPhase());
 +      String s = sf.getPhase();
 +      if (s != null) 
 +      {
 +              phase = Integer.parseInt(s);
 +      }
        } catch (NumberFormatException e)
        {
 -        // ignore
 +        // SwingJS -- need to avoid these.
        }
        /*
         * phase > 0 on first codon means 5' incomplete - skip to the start
      /*
       * variants in first codon base
       */
 -    for (DnaVariant var : codonVariants[0])
 +    for (DnaVariant dnavar : codonVariants[0])
      {
 -      if (var.variant != null)
 +      if (dnavar.variant != null)
        {
 -        String alleles = (String) var.variant.getValue(Gff3Helper.ALLELES);
 +        String alleles = (String) dnavar.variant.getValue(Gff3Helper.ALLELES);
          if (alleles != null)
          {
            for (String base : alleles.split(","))
                        + base3.toLowerCase();
                String canonical = base1.toUpperCase() + base2.toLowerCase()
                        + base3.toLowerCase();
 -              if (addPeptideVariant(peptide, peptidePos, residue, var,
 +              if (addPeptideVariant(peptide, peptidePos, residue, dnavar,
                        codon, canonical))
                {
                  count++;
              peptidePos, var.getSource());
  
      StringBuilder attributes = new StringBuilder(32);
-     String id = (String) var.variant.getValue(ID);
+     String id = (String) var.variant.getValue(VARIANT_ID);
      if (id != null)
      {
        if (id.startsWith(SEQUENCE_VARIANT))
        {
          id = id.substring(SEQUENCE_VARIANT.length());
        }
-       sf.setValue(ID, id);
-       attributes.append(ID).append("=").append(id);
+       sf.setValue(VARIANT_ID, id);
+       attributes.append(VARIANT_ID).append("=").append(id);
        // TODO handle other species variants JAL-2064
        StringBuilder link = new StringBuilder(32);
        try
      SequenceIdMatcher matcher = new SequenceIdMatcher(seqs);
      if (xrefs != null)
      {
 -      for (SequenceI xref : xrefs)
 +      // BH 2019.01.25 streamlined this triply nested loop to remove all iterators
 +      
 +      for (int ix = 0, nx = xrefs.length; ix < nx; ix++)
        {
 -        DBRefEntry[] dbrefs = xref.getDBRefs();
 +      SequenceI xref = xrefs[ix];
 +        List<DBRefEntry> dbrefs = xref.getDBRefs();
          if (dbrefs != null)
          {
 -          for (DBRefEntry dbref : dbrefs)
 +          for (int ir = 0, nir = dbrefs.size(); ir < nir; ir++)
            {
 -            if (dbref.getMap() == null || dbref.getMap().getTo() == null
 -                    || dbref.getMap().getTo().isProtein() != isProtein)
 +                DBRefEntry dbref = dbrefs.get(ir);
 +                Mapping map = dbref.getMap();
 +                SequenceI mto;
 +            if (map == null || (mto = map.getTo()) == null
 +                    || mto.isProtein() != isProtein)
              {
                continue;
              }
 -            SequenceI mappedTo = dbref.getMap().getTo();
 +            SequenceI mappedTo = mto;
              SequenceI match = matcher.findIdMatch(mappedTo);
              if (match == null)
              {
@@@ -27,7 -27,6 +27,7 @@@ import jalview.datamodel.SequenceFeatur
  import jalview.datamodel.SequenceI;
  import jalview.io.gff.SequenceOntologyI;
  import jalview.util.JSONUtils;
 +import jalview.util.Platform;
  
  import java.io.BufferedReader;
  import java.io.IOException;
@@@ -36,8 -35,10 +36,8 @@@ import java.net.URL
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
 +import java.util.Map;
  
 -import org.json.simple.JSONArray;
 -import org.json.simple.JSONObject;
 -import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
  
  /**
@@@ -91,14 -92,16 +91,14 @@@ class EnsemblFeatures extends EnsemblRe
    public AlignmentI getSequenceRecords(String query) throws IOException
    {
      // TODO: use a vararg String... for getSequenceRecords instead?
 +        
      List<String> queries = new ArrayList<>();
      queries.add(query);
 -    BufferedReader fp = getSequenceReader(queries);
 -    if (fp == null)
 -    {
 -      return null;
 -    }
 -
 -    SequenceI seq = parseFeaturesJson(fp);
 +    SequenceI seq = parseFeaturesJson(queries);
 +    if (seq == null)
 +      return null;
      return new Alignment(new SequenceI[] { seq });
 +
    }
  
    /**
     * @param br
     * @return
     */
 -  private SequenceI parseFeaturesJson(BufferedReader br)
 +  @SuppressWarnings("unchecked")
- private SequenceI parseFeaturesJson(List<String> queries)
++  private SequenceI parseFeaturesJson(List<String> queries)
    {
-         
-         
      SequenceI seq = new Sequence("Dummy", "");
--
 -    JSONParser jp = new JSONParser();
      try
      {
-       
 -      JSONArray responses = (JSONArray) jp.parse(br);
 -      Iterator rvals = responses.iterator();
 -      while (rvals.hasNext())
 +      Iterator<Object> rvals = (Iterator<Object>) getJSON(null, queries, -1, MODE_ITERATOR, null);
 +      if (rvals == null)
+       {
 +        return null;
++      }
 +      while (rvals.hasNext())
 +      {         
          try
          {
 -          JSONObject obj = (JSONObject) rvals.next();
 +          Map<String, Object> obj = (Map<String, Object>) rvals.next();
            String type = obj.get("feature_type").toString();
            int start = Integer.parseInt(obj.get("start").toString());
            int end = Integer.parseInt(obj.get("end").toString());
            String source = obj.get("source").toString();
            String strand = obj.get("strand").toString();
            String alleles = JSONUtils
 -                  .arrayToList((JSONArray) obj.get("alleles"));
 +                  .arrayToStringList((List<Object>) obj.get("alleles"));
            String clinSig = JSONUtils
 -                  .arrayToList(
 -                          (JSONArray) obj.get("clinical_significance"));
 +                  .arrayToStringList(
 +                          (List<Object>) obj.get("clinical_significance"));
  
            /*
             * convert 'variation' to 'sequence_variant', and 'cds' to 'CDS'
            {
              type = SequenceOntologyI.CDS;
            }
 -          
 +
            String desc = getFirstNotNull(obj, "alleles", "external_name",
                    JSON_ID);
            SequenceFeature sf = new SequenceFeature(type, desc, start, end,
            sf.setValue("clinical_significance", clinSig);
  
            seq.addSequenceFeature(sf);
 +          
          } catch (Throwable t)
          {
            // ignore - keep trying other features
        }
      } catch (ParseException | IOException e)
      {
 +      e.printStackTrace();
        // ignore
      }
  
      return seq;
    }
  
-   
- /**
-    * Returns the first non-null attribute found (if any) as a string
+   /**
+    * Returns the first non-null attribute found (if any) as a string, formatted
+    * suitably for display as feature description or tooltip. Answers null if
+    * none of the attribute keys is present.
     * 
     * @param obj
     * @param keys
     * @return
     */
 -  protected String getFirstNotNull(JSONObject obj, String... keys)
++  @SuppressWarnings("unchecked")
 +  protected String getFirstNotNull(Map<String, Object> obj, String... keys)
    {
--    String desc = null;
--
      for (String key : keys)
      {
        Object val = obj.get(key);
        if (val != null)
        {
-         String s = val.toString();
 -        String s = val instanceof JSONArray
 -                ? JSONUtils.arrayToList((JSONArray) val)
++        String s = val instanceof List<?>
++                ? JSONUtils.arrayToStringList((List<Object>) val)
+                 : val.toString();
          if (!s.isEmpty())
          {
            return s;
          }
        }
      }
--    return desc;
++    return null;
    }
  
    /**
     * @param obj
     * @param key
     */
 -  protected void setFeatureAttribute(SequenceFeature sf, JSONObject obj,
 +  protected void setFeatureAttribute(SequenceFeature sf, Map<String, Object> obj,
            String key)
    {
      Object object = obj.get(key);