2 VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3 Copyright (C) 2008 Kevin Darty, Alain Denise and Yann Ponty.
4 electronic mail : Yann.Ponty@lri.fr
5 paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
7 This file is part of VARNA version 3.1.
8 VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16 If not, see http://www.gnu.org/licenses.
18 package fr.orsay.lri.varna.models.rna;
20 import java.io.Serializable;
21 import java.util.Enumeration;
22 import java.util.Hashtable;
24 import fr.orsay.lri.varna.exceptions.MappingException;
26 public class Mapping implements Serializable {
31 private static final long serialVersionUID = -3031358968555310380L;
33 public static final int UNKNOWN = -1;
35 Hashtable<Integer, Integer> _mapping = new Hashtable<Integer, Integer>();
36 Hashtable<Integer, Integer> _invMapping = new Hashtable<Integer, Integer>();
42 public void addCouple(int i, int j) throws MappingException {
43 if (_mapping.containsKey(i) || _invMapping.containsKey(j)) {
44 throw new MappingException(
45 MappingException.MULTIPLE_PARTNERS_DEFINITION_ATTEMPT);
48 _mapping.put(new Integer(i), new Integer(j));
49 _invMapping.put(new Integer(j), new Integer(i));
52 public int getPartner(int i) {
53 if (!_mapping.containsKey(i))
56 return _mapping.get(i);
59 public int getAncestor(int j) {
60 if (!_invMapping.containsKey(j))
63 return _invMapping.get(j);
66 public int[] getSourceElems() {
67 int[] elems = new int[_mapping.size()];
68 Enumeration<Integer> en = _mapping.keys();
70 while (en.hasMoreElements()) {
71 int a = en.nextElement();
78 public int[] getTargetElems() {
79 int[] elems = new int[_invMapping.size()];
80 Enumeration<Integer> en = _invMapping.keys();
82 while (en.hasMoreElements()) {
83 int a = en.nextElement();
90 public static Mapping readMappingFromAlignment(String m, String n)
91 throws MappingException {
92 Mapping map = new Mapping();
93 if (m.length() != n.length()) {
94 throw new MappingException(MappingException.BAD_ALIGNMENT_INPUT);
98 for (int k = 0; k < m.length(); k++) {
100 char b = n.charAt(k);
101 if ((a != '-') && (a != ':') && (b != '-') && (b != ':')) {
105 if ((a != '-') && (a != ':')) {
108 if ((b != '-') && (b != ':')) {
115 public static Mapping DefaultMapping(int n, int m) {
116 Mapping map = new Mapping();
118 for (int i = 0; i < Math.min(n, m); i++) {
121 } catch (MappingException e) {
127 public static Mapping DefaultOutermostMapping(int n, int m) {
128 Mapping map = new Mapping();
130 int k = Math.min(n, m);
134 while (i <= (k - 1) - j) {
139 map.addCouple(n - 1 - j, m - 1 - j);
144 } catch (MappingException e) {
147 // System.println(map);
151 public String toString() {
152 Enumeration<Integer> en = _mapping.keys();
157 while (en.hasMoreElements()) {
158 Integer i = en.nextElement();
159 Integer j = _mapping.get(i);
162 tmp += "("+i+","+j+")";
164 maxIndex = Math.max(maxIndex,Math.max(l1,l2));
167 en = _mapping.keys();
171 while (en.hasMoreElements()) {
172 Integer a = en.nextElement();
173 Integer b = _mapping.get(a);
190 return tmp+"\n"+tmp1+"\n"+tmp2;