1 package fr.orsay.lri.varna.models.templates;
3 import java.util.ArrayList;
4 import java.util.HashMap;
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;
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.
21 * @author Raphael Champeimont
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>>();
30 private double distance;
33 public double getDistance() {
37 public void setDistance(double distance) {
38 this.distance = distance;
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.
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));
51 throw (new RNATemplateMappingException("Invalid base index: " + baseIndex));
53 map.put(baseIndex, templateElement);
54 if (!invmap.containsKey(templateElement)) {
55 invmap.put(templateElement, new ArrayList<Integer>());
57 invmap.get(templateElement).add(baseIndex);
61 public String showCompact(RNA r)
63 HashMap<String,Couple<Integer,Integer> > ranges = new HashMap<String,Couple<Integer,Integer> >();
64 for(int i:map.keySet())
66 RNATemplateElement t = map.get(i);
67 String k = t.getName();
68 if (t instanceof RNATemplate.RNATemplateHelix)
70 k += " (" + ((RNATemplateHelix) t).getCaption() + ")";
71 ModeleBase mb = r.getBaseAt(i);
72 if (mb.getElementStructure()>i)
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);
84 for(String k:ranges.keySet())
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)
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";
97 else if (t instanceof RNATemplate.RNATemplateUnpairedSequence)
99 result += "dummyID\t1\t"+k+"\t"+type+"\t"+c.first+"-"+c.second+"\n";
102 result += "alignment distance = " + distance;
108 * If the given base index is in the mapping, return the
109 * corresponding template element, otherwise return null.
111 public RNATemplateElement getPartner(int baseIndex) {
112 if (map.containsKey(baseIndex)) {
113 return map.get(baseIndex);
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.
128 public ArrayList<Integer> getAncestor(RNATemplateElement templateElement) {
129 if (invmap.containsKey(templateElement)) {
130 return invmap.get(templateElement);
139 * Return a set containing all the base indexes in the mapping.
140 * You should not modify the returned set.
142 public Set<Integer> getSourceElemsAsSet() {
149 * Return a set containing all the template elements in the mapping.
150 * You should not modify the return set.
152 public Set<RNATemplateElement> getTargetElemsAsSet() {
153 return invmap.keySet();