1 package fr.orsay.lri.varna.models.templates;
3 import fr.orsay.lri.varna.models.treealign.RNANodeValue;
4 import fr.orsay.lri.varna.models.treealign.RNANodeValue2;
5 import fr.orsay.lri.varna.models.treealign.TreeAlignLabelDistanceAsymmetric;
8 * Distance between an RNANodeValue2 and an RNANodeValueTemplate.
10 * @author Raphael Champeimont
13 public class RNANodeValue2TemplateDistance implements TreeAlignLabelDistanceAsymmetric<RNANodeValue2,RNANodeValueTemplate> {
14 public double delete(RNANodeValue2 v) {
16 // deleting nothing costs nothing
19 if (v.isSingleNode()) {
20 if (v.getNode().getRightBasePosition() < 0) {
21 // delete one base (that comes from a broken base pair)
29 return v.getNodes().size();
34 public double insert(RNANodeValueTemplate v) {
36 // inserting nothing costs nothing
39 if (v instanceof RNANodeValueTemplateSequence) {
40 return ((RNANodeValueTemplateSequence) v).getSequence().getLength();
41 } else if (v instanceof RNANodeValueTemplateBrokenBasePair) {
44 } else { // this is a base pair
45 // delete the base pair
51 public double f(RNANodeValue2 v1, RNANodeValueTemplate v2) {
54 } else if (v2 == null) {
56 } else if (!v1.isSingleNode()) { // v1 is a sequence
57 if (v2 instanceof RNANodeValueTemplateSequence) {
58 // the cost is the difference between the sequence lengths
59 return Math.abs(v1.getNodes().size() - ((RNANodeValueTemplateSequence) v2).getSequence().getLength());
61 // a sequence cannot be changed in something else
62 return Double.POSITIVE_INFINITY;
64 } else if (v1.getNode().getRightBasePosition() >= 0) { // v1 is a base pair
65 if (v2 instanceof RNANodeValueTemplateBasePair) {
66 // ok, a base pair can be mapped to a base pair
69 // a base pair cannot be changed in something else
70 return Double.POSITIVE_INFINITY;
72 } else { // v1 is a broken base pair
73 if (v2 instanceof RNANodeValueTemplateBrokenBasePair) {
74 RNANodeValueTemplateBrokenBasePair brokenBasePair = ((RNANodeValueTemplateBrokenBasePair) v2);
75 boolean strand5onTemplateSide = brokenBasePair.getPositionInHelix() < brokenBasePair.getHelix().getLength();
76 boolean strand3onTemplateSide = ! strand5onTemplateSide;
77 boolean strand5onRNASide = (v1.getNode().getOrigin() == RNANodeValue.Origin.BASE_FROM_HELIX_STRAND5);
78 boolean strand3onRNASide = (v1.getNode().getOrigin() == RNANodeValue.Origin.BASE_FROM_HELIX_STRAND3);
79 if ((strand5onTemplateSide && strand5onRNASide)
80 || (strand3onTemplateSide && strand3onRNASide)) {
81 // Ok they can be mapped together
84 // A base on a 5' strand of an helix
85 // cannot be mapped to base on a 3' strand of an helix
86 return Double.POSITIVE_INFINITY;
89 return Double.POSITIVE_INFINITY;