X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src2%2Ffr%2Forsay%2Flri%2Fvarna%2Fmodels%2Ftemplates%2FRNATemplateMapping.java;fp=src2%2Ffr%2Forsay%2Flri%2Fvarna%2Fmodels%2Ftemplates%2FRNATemplateMapping.java;h=39d76d05cbe7d295fcc95ec7f24378113c78c22c;hb=9f55415c9f8005e9dcd8243453883ff853fd5b76;hp=0000000000000000000000000000000000000000;hpb=d8e8c742b864e58406d13e21d124699a26b6492f;p=jalview.git diff --git a/src2/fr/orsay/lri/varna/models/templates/RNATemplateMapping.java b/src2/fr/orsay/lri/varna/models/templates/RNATemplateMapping.java new file mode 100644 index 0000000..39d76d0 --- /dev/null +++ b/src2/fr/orsay/lri/varna/models/templates/RNATemplateMapping.java @@ -0,0 +1,158 @@ +package fr.orsay.lri.varna.models.templates; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import fr.orsay.lri.varna.applications.templateEditor.Couple; +import fr.orsay.lri.varna.models.rna.ModeleBase; +import fr.orsay.lri.varna.models.rna.RNA; +import fr.orsay.lri.varna.models.templates.RNATemplate.RNATemplateElement; +import fr.orsay.lri.varna.models.templates.RNATemplate.RNATemplateHelix; + +/** + * A RNATemplateMapping is a mapping between bases in an RNA sequence + * and elements in a RNA template. + * A base is mapped to only one template element + * but a template element can be mapped to several bases. + * This class is designed to be similar to the Mapping class. + * + * @author Raphael Champeimont + */ +public class RNATemplateMapping { + private Map map = new HashMap(); + private Map> invmap = new HashMap>(); + + /** + * Alignment distance. + */ + private double distance; + + + public double getDistance() { + return distance; + } + + public void setDistance(double distance) { + this.distance = distance; + } + + /** + * Tell this mapping object that this base index and this element are + * mapped with each other. This will throw RNATemplateMappingException + * and do nothing if the base index is already in the mapping. + */ + public void addCouple(int baseIndex, RNATemplateElement templateElement) throws RNATemplateMappingException { + if (map.containsKey(baseIndex)) { + throw (new RNATemplateMappingException("Base index already in mapping: " + baseIndex)); + } + if (baseIndex < 0) { + throw (new RNATemplateMappingException("Invalid base index: " + baseIndex)); + } + map.put(baseIndex, templateElement); + if (!invmap.containsKey(templateElement)) { + invmap.put(templateElement, new ArrayList()); + } + invmap.get(templateElement).add(baseIndex); + } + + + public String showCompact(RNA r) + { + HashMap > ranges = new HashMap >(); + for(int i:map.keySet()) + { + RNATemplateElement t = map.get(i); + String k = t.getName(); + if (t instanceof RNATemplate.RNATemplateHelix) + { + k += " (" + ((RNATemplateHelix) t).getCaption() + ")"; + ModeleBase mb = r.getBaseAt(i); + if (mb.getElementStructure()>i) + k = k+":5'"; + else + k = k+":3'"; + } + if (!ranges.containsKey(k)) + { ranges.put(k, new Couple(Integer.MAX_VALUE,Integer.MIN_VALUE)); } + Couple c = ranges.get(k); + c.first = Math.min(c.first, i); + c.second = Math.max(c.second, i); + } + String result = ""; + for(String k:ranges.keySet()) + { + Couple c = ranges.get(k); + RNATemplateElement t = map.get(c.first); + String type = ((t instanceof RNATemplate.RNATemplateHelix)?"strand":"loop"); + if (t instanceof RNATemplate.RNATemplateHelix) + { + if (k.endsWith("5'")) + { + Couple c3 = ranges.get(k.replace("5'", "3'")); + result += "dummyID\t1\t"+k.replace(":5'", "")+"\t"+type+"\t"+c.first+"-"+c.second+":"+c3.first+"-"+c3.second+"\n"; + } + } + else if (t instanceof RNATemplate.RNATemplateUnpairedSequence) + { + result += "dummyID\t1\t"+k+"\t"+type+"\t"+c.first+"-"+c.second+"\n"; + } + } + result += "alignment distance = " + distance; + return result; + } + + + /** + * If the given base index is in the mapping, return the + * corresponding template element, otherwise return null. + */ + public RNATemplateElement getPartner(int baseIndex) { + if (map.containsKey(baseIndex)) { + return map.get(baseIndex); + } else { + return null; + } + } + + + + /** + * If the given template element is in the mapping, return an ArrayList + * containing the corresponding base indexes, otherwise return null. + * Note that you should not modify the returned ArrayList because + * no copy is made, so if you modify it this mapping object will + * contain inconsistent data. + */ + public ArrayList getAncestor(RNATemplateElement templateElement) { + if (invmap.containsKey(templateElement)) { + return invmap.get(templateElement); + } else { + return null; + } + } + + + + /** + * Return a set containing all the base indexes in the mapping. + * You should not modify the returned set. + */ + public Set getSourceElemsAsSet() { + return map.keySet(); + } + + + + /** + * Return a set containing all the template elements in the mapping. + * You should not modify the return set. + */ + public Set getTargetElemsAsSet() { + return invmap.keySet(); + } + + + +}