+
+ /**
+ * Test whether the given sequence is substitutable for one or more dummy
+ * sequences in this mapping
+ *
+ * @param map
+ * @param seq
+ * @return
+ */
+ public boolean isRealisableWith(SequenceI seq)
+ {
+ return realiseWith(seq, false) > 0;
+ }
+
+ /**
+ * Replace any matchable mapped dummy sequences with the given real one.
+ * Returns the count of sequence mappings instantiated.
+ *
+ * @param seq
+ * @return
+ */
+ public int realiseWith(SequenceI seq)
+ {
+ return realiseWith(seq, true);
+ }
+
+ /**
+ * Returns the number of mapped dummy sequences that could be replaced with
+ * the given real sequence.
+ *
+ * @param seq
+ * a dataset sequence
+ * @param doUpdate
+ * if true, performs replacements, else only counts
+ * @return
+ */
+ protected int realiseWith(SequenceI seq, boolean doUpdate)
+ {
+ SequenceI ds = seq.getDatasetSequence() != null ? seq
+ .getDatasetSequence() : seq;
+ int count = 0;
+
+ /*
+ * check for replaceable DNA ('map from') sequences
+ */
+ for (SequenceToSequenceMapping ssm : mappings)
+ {
+ SequenceI dna = ssm.fromSeq;
+ if (dna instanceof SequenceDummy
+ && dna.getName().equals(ds.getName()))
+ {
+ Mapping mapping = ssm.mapping;
+ int mapStart = mapping.getMap().getFromLowest();
+ int mapEnd = mapping.getMap().getFromHighest();
+ boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
+ if (mappable)
+ {
+ count++;
+ if (doUpdate)
+ {
+ // TODO: new method ? ds.realise(dna);
+ // might want to copy database refs as well
+ ds.setSequenceFeatures(dna.getSequenceFeatures());
+ // dnaSeqs[i] = ds;
+ ssm.fromSeq = ds;
+ System.out.println("Realised mapped sequence " + ds.getName());
+ }
+ }
+ }
+
+ /*
+ * check for replaceable protein ('map to') sequences
+ */
+ Mapping mapping = ssm.mapping;
+ SequenceI prot = mapping.getTo();
+ int mapStart = mapping.getMap().getToLowest();
+ int mapEnd = mapping.getMap().getToHighest();
+ boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
+ if (mappable)
+ {
+ count++;
+ if (doUpdate)
+ {
+ // TODO: new method ? ds.realise(dna);
+ // might want to copy database refs as well
+ ds.setSequenceFeatures(dna.getSequenceFeatures());
+ ssm.mapping.setTo(ds);
+ }
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Helper method to test whether a 'real' sequence could replace a 'dummy'
+ * sequence in the map. The criteria are that they have the same name, and
+ * that the mapped region overlaps the candidate sequence.
+ *
+ * @param existing
+ * @param replacement
+ * @param mapStart
+ * @param mapEnd
+ * @return
+ */
+ protected static boolean couldRealiseSequence(SequenceI existing,
+ SequenceI replacement, int mapStart, int mapEnd)
+ {
+ if (existing instanceof SequenceDummy
+ && !(replacement instanceof SequenceDummy)
+ && existing.getName().equals(replacement.getName()))
+ {
+ int start = replacement.getStart();
+ int end = replacement.getEnd();
+ boolean mappingOverlapsSequence = (mapStart >= start && mapStart <= end)
+ || (mapEnd >= start && mapEnd <= end);
+ if (mappingOverlapsSequence)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Change any mapping to the given sequence to be to its dataset sequence
+ * instead. For use when mappings are created before their referenced
+ * sequences are instantiated, for example when parsing GFF data.
+ *
+ * @param seq
+ */
+ public void updateToDataset(SequenceI seq)
+ {
+ if (seq == null || seq.getDatasetSequence() == null)
+ {
+ return;
+ }
+ SequenceI ds = seq.getDatasetSequence();
+
+ for (SequenceToSequenceMapping ssm : mappings)
+ /*
+ * 'from' sequences
+ */
+ {
+ if (ssm.fromSeq == seq)
+ {
+ ssm.fromSeq = ds;
+ }
+
+ /*
+ * 'to' sequences
+ */
+ if (ssm.mapping.to == seq)
+ {
+ ssm.mapping.to = ds;
+ }
+ }
+ }
+
+ /**
+ * Answers true if this object contains no mappings
+ *
+ * @return
+ */
+ public boolean isEmpty()
+ {
+ return mappings.isEmpty();
+ }