Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / srcjar / fr / orsay / lri / varna / models / templates / RNATemplateMapping.java
1 package fr.orsay.lri.varna.models.templates;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7
8 import fr.orsay.lri.varna.applications.templateEditor.Couple;
9 import fr.orsay.lri.varna.models.rna.ModeleBase;
10 import fr.orsay.lri.varna.models.rna.RNA;
11 import fr.orsay.lri.varna.models.templates.RNATemplate.RNATemplateElement;
12 import fr.orsay.lri.varna.models.templates.RNATemplate.RNATemplateHelix;
13
14 /**
15  * A RNATemplateMapping is a mapping between bases in an RNA sequence
16  * and elements in a RNA template.
17  * A base is mapped to only one template element
18  * but a template element can be mapped to several bases.
19  * This class is designed to be similar to the Mapping class.
20  * 
21  * @author Raphael Champeimont
22  */
23 public class RNATemplateMapping {
24         private Map<Integer, RNATemplateElement> map = new HashMap<Integer, RNATemplateElement>();
25         private Map<RNATemplateElement, ArrayList<Integer>> invmap = new HashMap<RNATemplateElement, ArrayList<Integer>>();
26         
27         /**
28          * Alignment distance.
29          */
30         private double distance;
31         
32         
33         public double getDistance() {
34                 return distance;
35         }
36         
37         public void setDistance(double distance) {
38                 this.distance = distance;
39         }
40         
41         /**
42          * Tell this mapping object that this base index and this element are
43          * mapped with each other. This will throw RNATemplateMappingException
44          * and do nothing if the base index is already in the mapping.
45          */
46         public void addCouple(int baseIndex, RNATemplateElement templateElement) throws RNATemplateMappingException {
47                 if (map.containsKey(baseIndex)) {
48                         throw (new RNATemplateMappingException("Base index already in mapping: " + baseIndex));
49                 }
50                 if (baseIndex < 0) {
51                         throw (new RNATemplateMappingException("Invalid base index: " + baseIndex));
52                 }
53                 map.put(baseIndex, templateElement);
54                 if (!invmap.containsKey(templateElement)) {
55                         invmap.put(templateElement, new ArrayList<Integer>());
56                 }
57                 invmap.get(templateElement).add(baseIndex);
58         }
59
60
61         public String showCompact(RNA r)
62         {
63                 HashMap<String,Couple<Integer,Integer> > ranges = new HashMap<String,Couple<Integer,Integer> >();
64                 for(int i:map.keySet())
65                 {
66                         RNATemplateElement t = map.get(i);
67                         String k = t.getName();
68                         if (t instanceof RNATemplate.RNATemplateHelix)
69                         {
70                                 k += " (" + ((RNATemplateHelix) t).getCaption() + ")";
71                                 ModeleBase mb = r.getBaseAt(i);
72                                 if (mb.getElementStructure()>i)
73                                   k = k+":5'";
74                                 else
75                                   k = k+":3'";
76                         }
77                         if (!ranges.containsKey(k))
78                         {  ranges.put(k, new Couple<Integer,Integer>(Integer.MAX_VALUE,Integer.MIN_VALUE));  }
79                         Couple<Integer,Integer> c = ranges.get(k);
80                         c.first = Math.min(c.first, i);
81                         c.second = Math.max(c.second, i);                       
82                 }
83                 String result = "";
84                 for(String k:ranges.keySet())
85                 {
86                         Couple<Integer,Integer> c = ranges.get(k);
87                         RNATemplateElement t = map.get(c.first);
88                         String type = ((t instanceof RNATemplate.RNATemplateHelix)?"strand":"loop");
89                         if (t instanceof RNATemplate.RNATemplateHelix)
90                         {
91                                 if (k.endsWith("5'"))
92                                 {
93                                         Couple<Integer,Integer> c3 = ranges.get(k.replace("5'", "3'"));
94                                         result += "dummyID\t1\t"+k.replace(":5'", "")+"\t"+type+"\t"+c.first+"-"+c.second+":"+c3.first+"-"+c3.second+"\n";
95                                 }
96                         }
97                         else if (t instanceof RNATemplate.RNATemplateUnpairedSequence)
98                         {
99                           result += "dummyID\t1\t"+k+"\t"+type+"\t"+c.first+"-"+c.second+"\n";
100                         }
101                 }
102                 result += "alignment distance = " + distance;
103                 return result;
104         }
105         
106         
107         /**
108          * If the given base index is in the mapping, return the
109          * corresponding template element, otherwise return null.
110          */
111         public RNATemplateElement getPartner(int baseIndex) {
112                 if (map.containsKey(baseIndex)) {
113                         return map.get(baseIndex);
114                 } else {
115                         return null;
116                 }
117         }
118         
119         
120         
121         /**
122          * If the given template element is in the mapping, return an ArrayList
123          * containing the corresponding base indexes, otherwise return null.
124          * Note that you should not modify the returned ArrayList because
125          * no copy is made, so if you modify it this mapping object will
126          * contain inconsistent data.
127          */
128         public ArrayList<Integer> getAncestor(RNATemplateElement templateElement) {
129                 if (invmap.containsKey(templateElement)) {
130                         return invmap.get(templateElement);
131                 } else {
132                         return null;
133                 }
134         }
135         
136         
137         
138         /**
139          * Return a set containing all the base indexes in the mapping.
140          * You should not modify the returned set.
141          */
142         public Set<Integer> getSourceElemsAsSet() {
143                 return map.keySet();
144         }
145         
146         
147         
148         /**
149          * Return a set containing all the template elements in the mapping.
150          * You should not modify the return set.
151          */
152         public Set<RNATemplateElement> getTargetElemsAsSet() {
153                 return invmap.keySet();
154         }
155         
156         
157         
158 }