+ 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)
+ {
+ final String alleles = (String) sf.getValue(Gff3Helper.ALLELES);
+ if (alleles == null)
+ {
+ return;
+ }
+ StringBuilder complement = new StringBuilder(alleles.length());
+ for (String allele : alleles.split(","))
+ {
+ reverseComplementAllele(complement, allele);
+ }
+ String comp = complement.toString();
+ sf.setValue(Gff3Helper.ALLELES, comp);
+ sf.setDescription(comp);
+ }
+
+ /**
+ * Makes the 'reverse complement' of the given allele and appends it to the
+ * buffer, after a comma separator if not the first
+ *
+ * @param complement
+ * @param allele
+ */
+ static void reverseComplementAllele(StringBuilder complement,
+ String allele)
+ {
+ if (complement.length() > 0)
+ {
+ complement.append(",");
+ }
+
+ /*
+ * some 'alleles' are actually descriptive terms
+ * e.g. HGMD_MUTATION, PhenCode_variation
+ * - we don't want to 'reverse complement' these
+ */
+ if (!Comparison.isNucleotideSequence(allele, true))
+ {
+ complement.append(allele);
+ }
+ else
+ {
+ for (int i = allele.length() - 1; i >= 0; i--)
+ {
+ complement.append(Dna.getComplement(allele.charAt(i)));
+ }
+ }
+ }
+
+ /**
+ * Transfers features from sourceSequence to targetSequence
+ *
+ * @param accessionId
+ * @param sourceSequence
+ * @param targetSequence
+ * @return true if any features were transferred, else false
+ */
+ protected boolean transferFeatures(String accessionId,
+ SequenceI sourceSequence, SequenceI targetSequence)
+ {
+ if (sourceSequence == null || targetSequence == null)
+ {
+ return false;
+ }
+
+ // long start = System.currentTimeMillis();
+ List<SequenceFeature> sfs = sourceSequence.getFeatures()
+ .getPositionalFeatures();
+ MapList mapping = getGenomicRangesFromFeatures(sourceSequence,
+ accessionId, targetSequence.getStart());
+ if (mapping == null)
+ {
+ return false;
+ }
+
+ // Platform.timeCheck("ESP. xfer " + sfs.size(), Platform.TIME_MARK);
+
+ boolean result = transferFeatures(sfs, targetSequence, mapping,
+ accessionId);
+ // System.out.println("transferFeatures (" + (sfs.size()) + " --> "
+ // + targetSequence.getFeatures().getFeatureCount(true) + ") to "
+ // + targetSequence.getName() + " took "
+ // + (System.currentTimeMillis() - start) + "ms");
+ return result;
+ }
+
+ /**
+ * Transfer features to the target sequence. The start/end positions are
+ * converted using the mapping. Features which do not overlap are ignored.
+ * Features whose parent is not the specified identifier are also ignored.
+ *
+ * @param sfs
+ * @param targetSequence
+ * @param mapping
+ * @param parentId
+ * @return
+ */
+ protected boolean transferFeatures(List<SequenceFeature> sfs,
+ SequenceI targetSequence, MapList mapping, String parentId)
+ {
+ final boolean forwardStrand = mapping.isFromForwardStrand();
+
+ /*
+ * sort features by start position (which corresponds to end
+ * position descending if reverse strand) so as to add them in
+ * 'forwards' order to the target sequence
+ */
+ SequenceFeatures.sortFeatures(sfs, forwardStrand);
+
+ boolean transferred = false;
+
+ for (int i = 0, n = sfs.size(); i < n; i++)
+ {
+
+ // if ((i%1000) == 0) {
+ //// Platform.timeCheck("Feature " + bhtest, Platform.TIME_GET);
+ // Platform.timeCheck("ESP. xferFeature + " + (i) + "/" + n,
+ // Platform.TIME_MARK);
+ // }
+
+ SequenceFeature sf = sfs.get(i);
+ if (retainFeature(sf, parentId))
+ {
+ transferFeature(sf, targetSequence, mapping, forwardStrand);
+ transferred = true;
+ }
+ }
+
+ return transferred;
+ }
+
+ /**
+ * Answers true if the feature type is one we want to keep for the sequence.
+ * Some features are only retrieved in order to identify the sequence range,
+ * and may then be discarded as redundant information (e.g. "CDS" feature for
+ * a CDS sequence).
+ */
+ @SuppressWarnings("unused")
+ protected boolean retainFeature(SequenceFeature sf, String accessionId)
+ {
+ return true; // override as required
+ }
+
+ /**
+ * Answers true if the feature has a Parent which refers to the given
+ * accession id, or if the feature has no parent. Answers false if the
+ * feature's Parent is for a different accession id.
+ *
+ * @param sf
+ * @param identifier
+ * @return
+ */
+ protected boolean featureMayBelong(SequenceFeature sf, String identifier)
+ {
+ String parent = (String) sf.getValue(PARENT);
+ if (parent != null && !parent.equalsIgnoreCase(identifier))
+ {
+ // this genomic feature belongs to a different transcript
+ return false;
+ }