JAL-4090 JAL-1551 spotlessApply
[jalview.git] / src / jalview / ext / ensembl / EnsemblSeqProxy.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.ensembl;
22
23 import java.io.IOException;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.json.simple.parser.ParseException;
33
34 import jalview.analysis.AlignmentUtils;
35 import jalview.analysis.Dna;
36 import jalview.bin.Console;
37 import jalview.datamodel.Alignment;
38 import jalview.datamodel.AlignmentI;
39 import jalview.datamodel.DBRefEntry;
40 import jalview.datamodel.DBRefSource;
41 import jalview.datamodel.Mapping;
42 import jalview.datamodel.Sequence;
43 import jalview.datamodel.SequenceFeature;
44 import jalview.datamodel.SequenceI;
45 import jalview.datamodel.features.SequenceFeatures;
46 import jalview.exceptions.JalviewException;
47 import jalview.io.gff.Gff3Helper;
48 import jalview.io.gff.SequenceOntologyFactory;
49 import jalview.io.gff.SequenceOntologyI;
50 import jalview.util.Comparison;
51 import jalview.util.DBRefUtils;
52 import jalview.util.IntRangeComparator;
53 import jalview.util.MapList;
54
55 /**
56  * Base class for Ensembl sequence fetchers
57  * 
58  * @see http://rest.ensembl.org/documentation/info/sequence_id
59  * @author gmcarstairs
60  */
61 public abstract class EnsemblSeqProxy extends EnsemblRestClient
62 {
63   protected static final String DESCRIPTION = "description";
64
65   /*
66    * enum for 'type' parameter to the /sequence REST service
67    */
68   public enum EnsemblSeqType
69   {
70     /**
71      * type=genomic to fetch full dna including introns
72      */
73     GENOMIC("genomic"),
74
75     /**
76      * type=cdna to fetch coding dna including UTRs
77      */
78     CDNA("cdna"),
79
80     /**
81      * type=cds to fetch coding dna excluding UTRs
82      */
83     CDS("cds"),
84
85     /**
86      * type=protein to fetch peptide product sequence
87      */
88     PROTEIN("protein");
89
90     /*
91      * the value of the 'type' parameter to fetch this version of 
92      * an Ensembl sequence
93      */
94     private String type;
95
96     EnsemblSeqType(String t)
97     {
98       type = t;
99     }
100
101     public String getType()
102     {
103       return type;
104     }
105
106   }
107
108   /**
109    * Default constructor (to use rest.ensembl.org)
110    */
111   public EnsemblSeqProxy()
112   {
113     super();
114   }
115
116   /**
117    * Constructor given the target domain to fetch data from
118    */
119   public EnsemblSeqProxy(String d)
120   {
121     super(d);
122   }
123
124   /**
125    * Makes the sequence queries to Ensembl's REST service and returns an
126    * alignment consisting of the returned sequences.
127    */
128   @Override
129   public AlignmentI getSequenceRecords(String query) throws Exception
130   {
131     // TODO use a String... query vararg instead?
132
133     // danger: accession separator used as a regex here, a string elsewhere
134     // in this case it is ok (it is just a space), but (e.g.) '\' would not be
135     List<String> allIds = Arrays
136             .asList(query.split(getAccessionSeparator()));
137     AlignmentI alignment = null;
138     inProgress = true;
139
140     /*
141      * execute queries, if necessary in batches of the
142      * maximum allowed number of ids
143      */
144     int maxQueryCount = getMaximumQueryCount();
145     for (int v = 0, vSize = allIds.size(); v < vSize; v += maxQueryCount)
146     {
147       int p = Math.min(vSize, v + maxQueryCount);
148       List<String> ids = allIds.subList(v, p);
149       try
150       {
151         alignment = fetchSequences(ids, alignment);
152       } catch (Throwable r)
153       {
154         inProgress = false;
155         String msg = "Aborting ID retrieval after " + v
156                 + " chunks. Unexpected problem (" + r.getLocalizedMessage()
157                 + ")";
158         jalview.bin.Console.errPrintln(msg);
159         r.printStackTrace();
160         break;
161       }
162     }
163
164     if (alignment == null)
165     {
166       return null;
167     }
168
169     /*
170      * fetch and transfer genomic sequence features,
171      * fetch protein product and add as cross-reference
172      */
173     for (int i = 0, n = allIds.size(); i < n; i++)
174     {
175       addFeaturesAndProduct(allIds.get(i), alignment);
176     }
177
178     List<SequenceI> seqs = alignment.getSequences();
179     for (int i = 0, n = seqs.size(); i < n; i++)
180     {
181       getCrossReferences(seqs.get(i));
182     }
183
184     return alignment;
185   }
186
187   /**
188    * Fetches Ensembl features using the /overlap REST endpoint, and adds them to
189    * the sequence in the alignment. Also fetches the protein product, maps it
190    * from the CDS features of the sequence, and saves it as a cross-reference of
191    * the dna sequence.
192    * 
193    * @param accId
194    * @param alignment
195    */
196   protected void addFeaturesAndProduct(String accId, AlignmentI alignment)
197   {
198     if (alignment == null)
199     {
200       return;
201     }
202
203     try
204     {
205       /*
206        * get 'dummy' genomic sequence with gene, transcript, 
207        * exon, cds and variation features
208        */
209       SequenceI genomicSequence = null;
210       EnsemblFeatures gffFetcher = new EnsemblFeatures(getDomain());
211       EnsemblFeatureType[] features = getFeaturesToFetch();
212
213       // Platform.timeCheck("ESP.getsequencerec1", Platform.TIME_MARK);
214
215       AlignmentI geneFeatures = gffFetcher.getSequenceRecords(accId,
216               features);
217       if (geneFeatures != null && geneFeatures.getHeight() > 0)
218       {
219         genomicSequence = geneFeatures.getSequenceAt(0);
220       }
221
222       // Platform.timeCheck("ESP.getsequencerec2", Platform.TIME_MARK);
223
224       if (genomicSequence != null)
225       {
226         /*
227          * transfer features to the query sequence
228          */
229         SequenceI querySeq = alignment.findName(accId, true);
230         if (transferFeatures(accId, genomicSequence, querySeq))
231         {
232
233           /*
234            * fetch and map protein product, and add it as a cross-reference
235            * of the retrieved sequence
236            */
237           // Platform.timeCheck("ESP.transferFeatures", Platform.TIME_MARK);
238           addProteinProduct(querySeq);
239         }
240       }
241     } catch (IOException e)
242     {
243       jalview.bin.Console.errPrintln(
244               "Error transferring Ensembl features: " + e.getMessage());
245     }
246     // Platform.timeCheck("ESP.addfeat done", Platform.TIME_MARK);
247   }
248
249   /**
250    * Returns those sequence feature types to fetch from Ensembl. We may want
251    * features either because they are of interest to the user, or as means to
252    * identify the locations of the sequence on the genomic sequence (CDS
253    * features identify CDS, exon features identify cDNA etc).
254    * 
255    * @return
256    */
257   protected abstract EnsemblFeatureType[] getFeaturesToFetch();
258
259   /**
260    * Fetches and maps the protein product, and adds it as a cross-reference of
261    * the retrieved sequence
262    */
263   protected void addProteinProduct(SequenceI querySeq)
264   {
265     String accId = querySeq.getName();
266     try
267     {
268       jalview.bin.Console.outPrintln("Adding protein product for " + accId);
269       AlignmentI protein = new EnsemblProtein(getDomain())
270               .getSequenceRecords(accId);
271       if (protein == null || protein.getHeight() == 0)
272       {
273         jalview.bin.Console
274                 .outPrintln("No protein product found for " + accId);
275         return;
276       }
277       SequenceI proteinSeq = protein.getSequenceAt(0);
278
279       /*
280        * need dataset sequences (to be the subject of mappings)
281        */
282       proteinSeq.createDatasetSequence();
283       querySeq.createDatasetSequence();
284
285       MapList mapList = AlignmentUtils.mapCdsToProtein(querySeq,
286               proteinSeq);
287       if (mapList != null)
288       {
289         // clunky: ensure Uniprot xref if we have one is on mapped sequence
290         SequenceI ds = proteinSeq.getDatasetSequence();
291         // TODO: Verify ensp primary ref is on proteinSeq.getDatasetSequence()
292         Mapping map = new Mapping(ds, mapList);
293         DBRefEntry dbr = new DBRefEntry(getDbSource(),
294                 getEnsemblDataVersion(), proteinSeq.getName(), map);
295         querySeq.getDatasetSequence().addDBRef(dbr);
296         List<DBRefEntry> uprots = DBRefUtils.selectRefs(ds.getDBRefs(),
297                 new String[]
298                 { DBRefSource.UNIPROT });
299         List<DBRefEntry> upxrefs = DBRefUtils
300                 .selectRefs(querySeq.getDBRefs(), new String[]
301                 { DBRefSource.UNIPROT });
302         if (uprots != null)
303         {
304           for (DBRefEntry up : uprots)
305           {
306             // locate local uniprot ref and map
307             List<DBRefEntry> upx = DBRefUtils.searchRefs(upxrefs,
308                     up.getAccessionId());
309             DBRefEntry upxref;
310             if (upx.size() != 0)
311             {
312               upxref = upx.get(0);
313
314               if (upx.size() > 1)
315               {
316                 Console.warn(
317                         "Implementation issue - multiple uniprot acc on product sequence.");
318               }
319             }
320             else
321             {
322               upxref = new DBRefEntry(DBRefSource.UNIPROT,
323                       getEnsemblDataVersion(), up.getAccessionId());
324             }
325
326             Mapping newMap = new Mapping(ds, mapList);
327             upxref.setVersion(getEnsemblDataVersion());
328             upxref.setMap(newMap);
329             if (upx.size() == 0)
330             {
331               // add the new uniprot ref
332               querySeq.getDatasetSequence().addDBRef(upxref);
333             }
334
335           }
336         }
337
338         /*
339          * copy exon features to protein, compute peptide variants from dna 
340          * variants and add as features on the protein sequence ta-da
341          */
342         // JAL-3187 render on the fly instead
343         // AlignmentUtils.computeProteinFeatures(querySeq, proteinSeq, mapList);
344       }
345     } catch (Exception e)
346     {
347       System.err
348               .println(String.format("Error retrieving protein for %s: %s",
349                       accId, e.getMessage()));
350     }
351   }
352
353   /**
354    * Get database xrefs from Ensembl, and attach them to the sequence
355    * 
356    * @param seq
357    */
358   protected void getCrossReferences(SequenceI seq)
359   {
360
361     // Platform.timeCheck("ESP. getdataseq ", Platform.TIME_MARK);
362
363     while (seq.getDatasetSequence() != null)
364     {
365       seq = seq.getDatasetSequence();
366     }
367
368     // Platform.timeCheck("ESP. getxref ", Platform.TIME_MARK);
369
370     EnsemblXref xrefFetcher = new EnsemblXref(getDomain(), getDbSource(),
371             getEnsemblDataVersion());
372     List<DBRefEntry> xrefs = xrefFetcher.getCrossReferences(seq.getName());
373
374     for (int i = 0, n = xrefs.size(); i < n; i++)
375     {
376       // Platform.timeCheck("ESP. getxref + " + (i) + "/" + n,
377       // Platform.TIME_MARK);
378       // BH 2019.01.25 this next method was taking 174 ms PER addition for a
379       // 266-reference example.
380       // DBRefUtils.ensurePrimaries(seq)
381       // was at the end of seq.addDBRef, so executed after ever addition!
382       // This method was moved to seq.getPrimaryDBRefs()
383       seq.addDBRef(xrefs.get(i));
384     }
385
386     // jalview.bin.Console.outPrintln("primaries are " +
387     // seq.getPrimaryDBRefs().toString());
388     /*
389      * and add a reference to itself
390      */
391
392     // Platform.timeCheck("ESP. getxref self ", Platform.TIME_MARK);
393
394     DBRefEntry self = new DBRefEntry(getDbSource(), getEnsemblDataVersion(),
395             seq.getName());
396
397     // Platform.timeCheck("ESP. getxref self add ", Platform.TIME_MARK);
398
399     seq.addDBRef(self);
400
401     // Platform.timeCheck("ESP. seqprox done ", Platform.TIME_MARK);
402   }
403
404   /**
405    * Fetches sequences for the list of accession ids and adds them to the
406    * alignment. Returns the extended (or created) alignment.
407    * 
408    * @param ids
409    * @param alignment
410    * @return
411    * @throws JalviewException
412    * @throws IOException
413    */
414   protected AlignmentI fetchSequences(List<String> ids,
415           AlignmentI alignment) throws JalviewException, IOException
416   {
417     if (!isEnsemblAvailable())
418     {
419       inProgress = false;
420       throw new JalviewException("ENSEMBL Rest API not available.");
421     }
422     // Platform.timeCheck("EnsemblSeqProx.fetchSeq ", Platform.TIME_MARK);
423
424     List<SequenceI> seqs = parseSequenceJson(ids);
425     if (seqs == null)
426       return alignment;
427
428     if (seqs.isEmpty())
429     {
430       throw new IOException("No data returned for " + ids);
431     }
432
433     if (seqs.size() != ids.size())
434     {
435       jalview.bin.Console.outPrintln(String.format(
436               "Only retrieved %d sequences for %d query strings",
437               seqs.size(), ids.size()));
438     }
439
440     if (!seqs.isEmpty())
441     {
442       AlignmentI seqal = new Alignment(
443               seqs.toArray(new SequenceI[seqs.size()]));
444       for (SequenceI seq : seqs)
445       {
446         if (seq.getDescription() == null)
447         {
448           seq.setDescription(getDbName());
449         }
450         String name = seq.getName();
451         if (ids.contains(name)
452                 || ids.contains(name.replace("ENSP", "ENST")))
453         {
454           // TODO JAL-3077 use true accession version in dbref
455           DBRefEntry dbref = DBRefUtils.parseToDbRef(seq, getDbSource(),
456                   getEnsemblDataVersion(), name);
457           seq.addDBRef(dbref);
458         }
459       }
460       if (alignment == null)
461       {
462         alignment = seqal;
463       }
464       else
465       {
466         alignment.append(seqal);
467       }
468     }
469     return alignment;
470   }
471
472   /**
473    * Parses a JSON response for a single sequence ID query
474    * 
475    * @param br
476    * @return a single jalview.datamodel.Sequence
477    * @see http://rest.ensembl.org/documentation/info/sequence_id
478    */
479   @SuppressWarnings("unchecked")
480   protected List<SequenceI> parseSequenceJson(List<String> ids)
481   {
482     List<SequenceI> result = new ArrayList<>();
483     try
484     {
485       /*
486        * for now, assumes only one sequence returned; refactor if needed
487        * in future to handle a JSONArray with more than one
488        */
489       // Platform.timeCheck("ENS seqproxy", Platform.TIME_MARK);
490       Map<String, Object> val = (Map<String, Object>) getJSON(null, ids, -1,
491               MODE_MAP, null);
492       if (val == null)
493         return null;
494       Object s = val.get("desc");
495       String desc = s == null ? null : s.toString();
496       s = val.get("id");
497       String id = s == null ? null : s.toString();
498       s = val.get("seq");
499       String seq = s == null ? null : s.toString();
500       Sequence sequence = new Sequence(id, seq);
501       if (desc != null)
502       {
503         sequence.setDescription(desc);
504       }
505       // todo JAL-3077 make a DBRefEntry with true accession version
506       // s = val.get("version");
507       // String version = s == null ? "0" : s.toString();
508       // DBRefEntry dbref = new DBRefEntry(getDbSource(), version, id);
509       // sequence.addDBRef(dbref);
510       result.add(sequence);
511     } catch (ParseException | IOException e)
512     {
513       jalview.bin.Console.errPrintln(
514               "Error processing JSON response: " + e.toString());
515       // ignore
516     }
517     // Platform.timeCheck("ENS seqproxy2", Platform.TIME_MARK);
518     return result;
519   }
520
521   /**
522    * Returns the URL for the REST call
523    * 
524    * @return
525    * @throws MalformedURLException
526    */
527   @Override
528   protected URL getUrl(List<String> ids) throws MalformedURLException
529   {
530     /*
531      * a single id is included in the URL path
532      * multiple ids go in the POST body instead
533      */
534     StringBuffer urlstring = new StringBuffer(128);
535     urlstring.append(getDomain() + "/sequence/id");
536     if (ids.size() == 1)
537     {
538       urlstring.append("/").append(ids.get(0));
539     }
540     // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
541     urlstring.append("?type=").append(getSourceEnsemblType().getType());
542     urlstring.append(("&Accept=application/json"));
543     urlstring.append(("&content-type=application/json"));
544
545     String objectType = getObjectType();
546     if (objectType != null)
547     {
548       urlstring.append("&").append(OBJECT_TYPE).append("=")
549               .append(objectType);
550     }
551
552     URL url = new URL(urlstring.toString());
553     return url;
554   }
555
556   /**
557    * Override this method to specify object_type request parameter
558    * 
559    * @return
560    */
561   protected String getObjectType()
562   {
563     return null;
564   }
565
566   /**
567    * A sequence/id POST request currently allows up to 50 queries
568    * 
569    * @see http://rest.ensembl.org/documentation/info/sequence_id_post
570    */
571   @Override
572   public int getMaximumQueryCount()
573   {
574     return 50;
575   }
576
577   @Override
578   protected boolean useGetRequest()
579   {
580     return false;
581   }
582
583   /**
584    * 
585    * @return the configured sequence return type for this source
586    */
587   protected abstract EnsemblSeqType getSourceEnsemblType();
588
589   /**
590    * Returns a list of [start, end] genomic ranges corresponding to the sequence
591    * being retrieved.
592    * 
593    * The correspondence between the frames of reference is made by locating
594    * those features on the genomic sequence which identify the retrieved
595    * sequence. Specifically
596    * <ul>
597    * <li>genomic sequence is identified by "transcript" features with
598    * ID=transcript:transcriptId</li>
599    * <li>cdna sequence is identified by "exon" features with
600    * Parent=transcript:transcriptId</li>
601    * <li>cds sequence is identified by "CDS" features with
602    * Parent=transcript:transcriptId</li>
603    * </ul>
604    * 
605    * The returned ranges are sorted to run forwards (for positive strand) or
606    * backwards (for negative strand). Aborts and returns null if both positive
607    * and negative strand are found (this should not normally happen).
608    * 
609    * @param sourceSequence
610    * @param accId
611    * @param start
612    *          the start position of the sequence we are mapping to
613    * @return
614    */
615   protected MapList getGenomicRangesFromFeatures(SequenceI sourceSequence,
616           String accId, int start)
617   {
618     List<SequenceFeature> sfs = getIdentifyingFeatures(sourceSequence,
619             accId);
620     if (sfs.isEmpty())
621     {
622       return null;
623     }
624
625     /*
626      * generously initial size for number of cds regions
627      * (worst case titin Q8WZ42 has c. 313 exons)
628      */
629     List<int[]> regions = new ArrayList<>(100);
630     int mappedLength = 0;
631     int direction = 1; // forward
632     boolean directionSet = false;
633
634     for (SequenceFeature sf : sfs)
635     {
636       int strand = sf.getStrand();
637       strand = strand == 0 ? 1 : strand; // treat unknown as forward
638
639       if (directionSet && strand != direction)
640       {
641         // abort - mix of forward and backward
642         System.err
643                 .println("Error: forward and backward strand for " + accId);
644         return null;
645       }
646       direction = strand;
647       directionSet = true;
648
649       /*
650        * add to CDS ranges, semi-sorted forwards/backwards
651        */
652       if (strand < 0)
653       {
654         regions.add(0, new int[] { sf.getEnd(), sf.getBegin() });
655       }
656       else
657       {
658         regions.add(new int[] { sf.getBegin(), sf.getEnd() });
659       }
660       mappedLength += Math.abs(sf.getEnd() - sf.getBegin() + 1);
661     }
662
663     if (regions.isEmpty())
664     {
665       jalview.bin.Console
666               .outPrintln("Failed to identify target sequence for " + accId
667                       + " from genomic features");
668       return null;
669     }
670
671     /*
672      * a final sort is needed since Ensembl returns CDS sorted within source
673      * (havana / ensembl_havana)
674      */
675     Collections.sort(regions, direction == 1 ? IntRangeComparator.ASCENDING
676             : IntRangeComparator.DESCENDING);
677
678     List<int[]> to = Arrays
679             .asList(new int[]
680             { start, start + mappedLength - 1 });
681
682     return new MapList(regions, to, 1, 1);
683   }
684
685   /**
686    * Answers a list of sequence features that mark positions of the genomic
687    * sequence feature which are within the sequence being retrieved. For
688    * example, an 'exon' feature whose parent is the target transcript marks the
689    * cdna positions of the transcript. For a gene sequence, this is trivially
690    * just the 'gene' feature with matching gene id.
691    * 
692    * @param seq
693    * @param accId
694    * @return
695    */
696   protected abstract List<SequenceFeature> getIdentifyingFeatures(
697           SequenceI seq, String accId);
698
699   int bhtest = 0;
700
701   /**
702    * Transfers the sequence feature to the target sequence, locating its start
703    * and end range based on the mapping. Features which do not overlap the
704    * target sequence are ignored.
705    * 
706    * @param sf
707    * @param targetSequence
708    * @param mapping
709    *          mapping from the sequence feature's coordinates to the target
710    *          sequence
711    * @param forwardStrand
712    */
713   protected void transferFeature(SequenceFeature sf,
714           SequenceI targetSequence, MapList mapping, boolean forwardStrand)
715   {
716     int start = sf.getBegin();
717     int end = sf.getEnd();
718     int[] mappedRange = mapping.locateInTo(start, end);
719
720     if (mappedRange != null)
721     {
722       // Platform.timeCheck(null, Platform.TIME_SET);
723       String group = sf.getFeatureGroup();
724       if (".".equals(group))
725       {
726         group = getDbSource();
727       }
728       int newBegin = Math.min(mappedRange[0], mappedRange[1]);
729       int newEnd = Math.max(mappedRange[0], mappedRange[1]);
730       // Platform.timeCheck(null, Platform.TIME_MARK);
731       bhtest++;
732       // 280 ms/1000 here:
733       SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
734               group, sf.getScore());
735       // 0.175 ms here:
736       targetSequence.addSequenceFeature(copy);
737
738       /*
739        * for sequence_variant on reverse strand, have to convert the allele
740        * values to their complements
741        */
742       if (!forwardStrand && SequenceOntologyFactory.getInstance()
743               .isA(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT))
744       {
745         reverseComplementAlleles(copy);
746       }
747     }
748   }
749
750   /**
751    * Change the 'alleles' value of a feature by converting to complementary
752    * bases, and also update the feature description to match
753    * 
754    * @param sf
755    */
756   static void reverseComplementAlleles(SequenceFeature sf)
757   {
758     final String alleles = (String) sf.getValue(Gff3Helper.ALLELES);
759     if (alleles == null)
760     {
761       return;
762     }
763     StringBuilder complement = new StringBuilder(alleles.length());
764     for (String allele : alleles.split(","))
765     {
766       reverseComplementAllele(complement, allele);
767     }
768     String comp = complement.toString();
769     sf.setValue(Gff3Helper.ALLELES, comp);
770     sf.setDescription(comp);
771   }
772
773   /**
774    * Makes the 'reverse complement' of the given allele and appends it to the
775    * buffer, after a comma separator if not the first
776    * 
777    * @param complement
778    * @param allele
779    */
780   static void reverseComplementAllele(StringBuilder complement,
781           String allele)
782   {
783     if (complement.length() > 0)
784     {
785       complement.append(",");
786     }
787
788     /*
789      * some 'alleles' are actually descriptive terms 
790      * e.g. HGMD_MUTATION, PhenCode_variation
791      * - we don't want to 'reverse complement' these
792      */
793     if (!Comparison.isNucleotideSequence(allele, true))
794     {
795       complement.append(allele);
796     }
797     else
798     {
799       for (int i = allele.length() - 1; i >= 0; i--)
800       {
801         complement.append(Dna.getComplement(allele.charAt(i)));
802       }
803     }
804   }
805
806   /**
807    * Transfers features from sourceSequence to targetSequence
808    * 
809    * @param accessionId
810    * @param sourceSequence
811    * @param targetSequence
812    * @return true if any features were transferred, else false
813    */
814   protected boolean transferFeatures(String accessionId,
815           SequenceI sourceSequence, SequenceI targetSequence)
816   {
817     if (sourceSequence == null || targetSequence == null)
818     {
819       return false;
820     }
821
822     // long start = System.currentTimeMillis();
823     List<SequenceFeature> sfs = sourceSequence.getFeatures()
824             .getPositionalFeatures();
825     MapList mapping = getGenomicRangesFromFeatures(sourceSequence,
826             accessionId, targetSequence.getStart());
827     if (mapping == null)
828     {
829       return false;
830     }
831
832     // Platform.timeCheck("ESP. xfer " + sfs.size(), Platform.TIME_MARK);
833
834     boolean result = transferFeatures(sfs, targetSequence, mapping,
835             accessionId);
836     // jalview.bin.Console.outPrintln("transferFeatures (" + (sfs.size()) + "
837     // --> "
838     // + targetSequence.getFeatures().getFeatureCount(true) + ") to "
839     // + targetSequence.getName() + " took "
840     // + (System.currentTimeMillis() - start) + "ms");
841     return result;
842   }
843
844   /**
845    * Transfer features to the target sequence. The start/end positions are
846    * converted using the mapping. Features which do not overlap are ignored.
847    * Features whose parent is not the specified identifier are also ignored.
848    * 
849    * @param sfs
850    * @param targetSequence
851    * @param mapping
852    * @param parentId
853    * @return
854    */
855   protected boolean transferFeatures(List<SequenceFeature> sfs,
856           SequenceI targetSequence, MapList mapping, String parentId)
857   {
858     final boolean forwardStrand = mapping.isFromForwardStrand();
859
860     /*
861      * sort features by start position (which corresponds to end
862      * position descending if reverse strand) so as to add them in
863      * 'forwards' order to the target sequence
864      */
865     SequenceFeatures.sortFeatures(sfs, forwardStrand);
866
867     boolean transferred = false;
868
869     for (int i = 0, n = sfs.size(); i < n; i++)
870     {
871
872       // if ((i%1000) == 0) {
873       //// Platform.timeCheck("Feature " + bhtest, Platform.TIME_GET);
874       // Platform.timeCheck("ESP. xferFeature + " + (i) + "/" + n,
875       // Platform.TIME_MARK);
876       // }
877
878       SequenceFeature sf = sfs.get(i);
879       if (retainFeature(sf, parentId))
880       {
881         transferFeature(sf, targetSequence, mapping, forwardStrand);
882         transferred = true;
883       }
884     }
885
886     return transferred;
887   }
888
889   /**
890    * Answers true if the feature type is one we want to keep for the sequence.
891    * Some features are only retrieved in order to identify the sequence range,
892    * and may then be discarded as redundant information (e.g. "CDS" feature for
893    * a CDS sequence).
894    */
895   @SuppressWarnings("unused")
896   protected boolean retainFeature(SequenceFeature sf, String accessionId)
897   {
898     return true; // override as required
899   }
900
901   /**
902    * Answers true if the feature has a Parent which refers to the given
903    * accession id, or if the feature has no parent. Answers false if the
904    * feature's Parent is for a different accession id.
905    * 
906    * @param sf
907    * @param identifier
908    * @return
909    */
910   protected boolean featureMayBelong(SequenceFeature sf, String identifier)
911   {
912     String parent = (String) sf.getValue(PARENT);
913     if (parent != null && !parent.equalsIgnoreCase(identifier))
914     {
915       // this genomic feature belongs to a different transcript
916       return false;
917     }
918     return true;
919   }
920
921   /**
922    * Answers a short description of the sequence fetcher
923    */
924   @Override
925   public String getDescription()
926   {
927     return "Ensembl " + getSourceEnsemblType().getType()
928             + " sequence with variant features";
929   }
930
931   /**
932    * Returns a (possibly empty) list of features on the sequence which have the
933    * specified sequence ontology term (or a sub-type of it), and the given
934    * identifier as parent
935    * 
936    * @param sequence
937    * @param term
938    * @param parentId
939    * @return
940    */
941   protected List<SequenceFeature> findFeatures(SequenceI sequence,
942           String term, String parentId)
943   {
944     List<SequenceFeature> result = new ArrayList<>();
945
946     List<SequenceFeature> sfs = sequence.getFeatures()
947             .getFeaturesByOntology(term);
948     for (SequenceFeature sf : sfs)
949     {
950       String parent = (String) sf.getValue(PARENT);
951       if (parent != null && parent.equalsIgnoreCase(parentId))
952       {
953         result.add(sf);
954       }
955     }
956
957     return result;
958   }
959
960   /**
961    * Answers true if the feature type is either 'NMD_transcript_variant' or
962    * 'transcript' (or one of its sub-types in the Sequence Ontology). This is
963    * because NMD_transcript_variant behaves like 'transcript' in Ensembl
964    * although strictly speaking it is not (it is a sub-type of
965    * sequence_variant).
966    * <p>
967    * (This test was needed when fetching transcript features as GFF. As we are
968    * now fetching as JSON, all features have type 'transcript' so the check for
969    * NMD_transcript_variant is redundant. Left in for any future case arising.)
970    * 
971    * @param featureType
972    * @return
973    */
974   public static boolean isTranscript(String featureType)
975   {
976     return SequenceOntologyI.NMD_TRANSCRIPT_VARIANT.equals(featureType)
977             || SequenceOntologyFactory.getInstance().isA(featureType,
978                     SequenceOntologyI.TRANSCRIPT);
979   }
980 }