JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / models / templates / RNANodeValue2TemplateDistance.java
1 package fr.orsay.lri.varna.models.templates;
2
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;
6
7 /**
8  * Distance between an RNANodeValue2 and an RNANodeValueTemplate.
9  * 
10  * @author Raphael Champeimont
11  *
12  */
13 public class RNANodeValue2TemplateDistance implements TreeAlignLabelDistanceAsymmetric<RNANodeValue2,RNANodeValueTemplate> {
14         public double delete(RNANodeValue2 v) {
15                 if (v == null) {
16                         // deleting nothing costs nothing
17                         return 0;
18                 } else {
19                         if (v.isSingleNode()) {
20                                 if (v.getNode().getRightBasePosition() < 0) {
21                                         // delete one base (that comes from a broken base pair)
22                                         return 1;
23                                 } else {
24                                         // delete a base pair
25                                         return 2;
26                                 }
27                         } else {
28                                 // delete a sequence
29                                 return v.getNodes().size();
30                         }
31                 }
32         }
33         
34         public double insert(RNANodeValueTemplate v) {
35                 if (v == null) {
36                         // inserting nothing costs nothing
37                         return 0;
38                 } else {
39                         if (v instanceof RNANodeValueTemplateSequence) {
40                                 return ((RNANodeValueTemplateSequence) v).getSequence().getLength();
41                         } else if (v instanceof RNANodeValueTemplateBrokenBasePair) {
42                                 // insert one base
43                                 return 1;
44                         } else { // this is a base pair
45                                 // delete the base pair
46                                 return 2;
47                         }
48                 }
49         }
50         
51         public double f(RNANodeValue2 v1, RNANodeValueTemplate v2) {
52                 if (v1 == null) {
53                         return insert(v2);
54                 } else if (v2 == null) {
55                         return delete(v1);
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());
60                         } else {
61                                 // a sequence cannot be changed in something else
62                                 return Double.POSITIVE_INFINITY;
63                         }
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
67                                 return 0;
68                         } else {
69                                 // a base pair cannot be changed in something else
70                                 return Double.POSITIVE_INFINITY;
71                         }
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
82                                         return 0.0;
83                                 } else {
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;
87                                 }
88                         } else {
89                                 return Double.POSITIVE_INFINITY;
90                         }
91                 }
92         }
93 }