+ protected boolean useGetRequest()
+ {
+ return false;
+ }
+
+ /**
+ *
+ * @return the configured sequence return type for this source
+ */
+ protected abstract EnsemblSeqType getSourceEnsemblType();
+
+ /**
+ * Returns a list of [start, end] genomic ranges corresponding to the sequence
+ * being retrieved.
+ *
+ * The correspondence between the frames of reference is made by locating
+ * those features on the genomic sequence which identify the retrieved
+ * sequence. Specifically
+ * <ul>
+ * <li>genomic sequence is identified by "transcript" features with
+ * ID=transcript:transcriptId</li>
+ * <li>cdna sequence is identified by "exon" features with
+ * Parent=transcript:transcriptId</li>
+ * <li>cds sequence is identified by "CDS" features with
+ * Parent=transcript:transcriptId</li>
+ * </ul>
+ *
+ * The returned ranges are sorted to run forwards (for positive strand) or
+ * backwards (for negative strand). Aborts and returns null if both positive
+ * and negative strand are found (this should not normally happen).
+ *
+ * @param sourceSequence
+ * @param accId
+ * @param start
+ * the start position of the sequence we are mapping to
+ * @return
+ */
+ protected MapList getGenomicRangesFromFeatures(SequenceI sourceSequence,
+ String accId, int start)
+ {
+ List<SequenceFeature> sfs = getIdentifyingFeatures(sourceSequence,
+ accId);
+ if (sfs.isEmpty())
+ {
+ return null;
+ }
+
+ /*
+ * generously initial size for number of cds regions
+ * (worst case titin Q8WZ42 has c. 313 exons)
+ */
+ List<int[]> regions = new ArrayList<>(100);
+ int mappedLength = 0;
+ int direction = 1; // forward
+ boolean directionSet = false;
+
+ for (SequenceFeature sf : sfs)
+ {
+ int strand = sf.getStrand();
+ strand = strand == 0 ? 1 : strand; // treat unknown as forward
+
+ if (directionSet && strand != direction)
+ {
+ // abort - mix of forward and backward
+ System.err
+ .println("Error: forward and backward strand for " + accId);
+ return null;
+ }
+ direction = strand;
+ directionSet = true;
+
+ /*
+ * add to CDS ranges, semi-sorted forwards/backwards
+ */
+ if (strand < 0)
+ {
+ regions.add(0, new int[] { sf.getEnd(), sf.getBegin() });
+ }
+ else
+ {
+ regions.add(new int[] { sf.getBegin(), sf.getEnd() });
+ }
+ mappedLength += Math.abs(sf.getEnd() - sf.getBegin() + 1);
+ }
+
+ if (regions.isEmpty())
+ {
+ System.out.println("Failed to identify target sequence for " + accId
+ + " from genomic features");
+ return null;
+ }
+
+ /*
+ * a final sort is needed since Ensembl returns CDS sorted within source
+ * (havana / ensembl_havana)
+ */
+ Collections.sort(regions, direction == 1 ? IntRangeComparator.ASCENDING
+ : IntRangeComparator.DESCENDING);
+
+ List<int[]> to = Arrays
+ .asList(new int[]
+ { start, start + mappedLength - 1 });
+
+ return new MapList(regions, to, 1, 1);
+ }
+
+ /**
+ * Answers a list of sequence features that mark positions of the genomic
+ * sequence feature which are within the sequence being retrieved. For
+ * example, an 'exon' feature whose parent is the target transcript marks the
+ * cdna positions of the transcript. For a gene sequence, this is trivially
+ * just the 'gene' feature with matching gene id.
+ *
+ * @param seq
+ * @param accId
+ * @return
+ */
+ protected abstract List<SequenceFeature> getIdentifyingFeatures(
+ SequenceI seq, String accId);
+
+ int bhtest = 0;
+
+ /**
+ * Transfers the sequence feature to the target sequence, locating its start
+ * and end range based on the mapping. Features which do not overlap the
+ * target sequence are ignored.
+ *
+ * @param sf
+ * @param targetSequence
+ * @param mapping
+ * mapping from the sequence feature's coordinates to the target
+ * sequence
+ * @param forwardStrand
+ */
+ protected void transferFeature(SequenceFeature sf,
+ SequenceI targetSequence, MapList mapping, boolean forwardStrand)
+ {
+ int start = sf.getBegin();
+ int end = sf.getEnd();
+ int[] mappedRange = mapping.locateInTo(start, end);
+
+ if (mappedRange != null)
+ {
+// Platform.timeCheck(null, Platform.TIME_SET);
+ String group = sf.getFeatureGroup();
+ if (".".equals(group))
+ {
+ group = getDbSource();
+ }
+ int newBegin = Math.min(mappedRange[0], mappedRange[1]);
+ int newEnd = Math.max(mappedRange[0], mappedRange[1]);
+// Platform.timeCheck(null, Platform.TIME_MARK);
+ bhtest++;
+ // 280 ms/1000 here:
+ SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd, group, sf.getScore());
+ // 0.175 ms here:
+ targetSequence.addSequenceFeature(copy);
+
+ /*
+ * for sequence_variant on reverse strand, have to convert the allele
+ * values to their complements
+ */
+ if (!forwardStrand && SequenceOntologyFactory.getInstance()
+ .isA(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT))
+ {
+ reverseComplementAlleles(copy);
+ }
+ }
+ }
+
+ /**
+ * Change the 'alleles' value of a feature by converting to complementary
+ * bases, and also update the feature description to match
+ *
+ * @param sf
+ */
+ static void reverseComplementAlleles(SequenceFeature sf)