JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / models / treealign / RNANodeValue2.java
1 package fr.orsay.lri.varna.models.treealign;
2 import java.util.ArrayList;
3 import java.util.List;
4
5
6
7 /**
8  * In this model, nodes are either:
9  * 1. a couple of paired bases, and in that case they may have children,
10  *    in this case singleNode is true 
11  * 2. a single base that comes from a broken base pair
12  *    (broken during planarization), without children,
13  *    in this case singleNode is true
14  * 3. a list of consecutive non-paired bases, without children.
15  *    in this case singleNode is false
16  * Note that case 2 happens only if original sequences contained
17  * pseudoknots, otherwise this case can be ignored.
18  * 
19  * @author Raphael Champeimont
20  *
21  */
22 public class RNANodeValue2 implements GraphvizDrawableNodeValue {
23         /**
24          * Says whether we have a single node or a list of nodes.
25          */
26         private boolean singleNode = true;
27         
28         /**
29          * Defined if singleNode is true. 
30          */
31         private RNANodeValue node;
32         
33         /**
34          * Defined if singleNode is false; 
35          */
36         private List<RNANodeValue> nodes;
37         
38         public RNANodeValue2(boolean singleNode) {
39                 this.singleNode = singleNode;
40                 if (singleNode) {
41                         node = new RNANodeValue();
42                 } else {
43                         nodes = new ArrayList<RNANodeValue>();
44                 }
45         }
46
47         /**
48          * In case of a single node, return it.
49          * Will throw RNANodeValue2WrongTypeException if singleNode = false.
50          */
51         public RNANodeValue getNode() {
52                 if (singleNode) {
53                         return node;
54                 } else {
55                         throw (new RNANodeValue2WrongTypeException());
56                 }
57         }
58         
59         public void setNode(RNANodeValue node) {
60                 if (singleNode) {
61                         this.node = node;
62                 } else {
63                         throw (new RNANodeValue2WrongTypeException());
64                 }
65         }
66
67         /**
68          * In case of multiple nodes, return them.
69          * Will throw RNANodeValue2WrongTypeException if singleNode = true.
70          */
71         public List<RNANodeValue> getNodes() {
72                 if (!singleNode) {
73                         return nodes;
74                 } else {
75                         throw (new RNANodeValue2WrongTypeException());
76                 }
77         }
78         /**
79          * In case of multiple nodes, return the sequence of nucleotides.
80          */
81         public char[] computeSequence() {
82                 if (!singleNode) {
83                         final int n = nodes.size();
84                         char[] sequence = new char[n];
85                         for (int i=0; i<n; i++) {
86                                 sequence[i] = nodes.get(i).getLeftNucleotide().charAt(0);
87                         }
88                         return sequence;
89                 } else {
90                         throw (new RNANodeValue2WrongTypeException());
91                 }
92         }
93         
94         public void setNodes(List<RNANodeValue> nodes) {
95                 if (!singleNode) {
96                         this.nodes = nodes;
97                 } else {
98                         throw (new RNANodeValue2WrongTypeException());
99                 }
100         }
101
102         public boolean isSingleNode() {
103                 return singleNode;
104         }
105         
106         public String toString() {
107                 if (singleNode) {
108                         return node.toString();
109                 } else {
110                         String s = "";
111                         for (RNANodeValue node: nodes) {
112                                 if (s != "") {
113                                         s += " ";
114                                 }
115                                 s += node.toString();
116                         }
117                         return s;
118                 }
119         }
120         
121         public String toGraphvizNodeName() {
122                 if (singleNode) {
123                         return node.toGraphvizNodeName();
124                 } else {
125                         String s = "";
126                         for (RNANodeValue node: nodes) {
127                                 if (s != "") {
128                                         s += " ";
129                                 }
130                                 s += node.toGraphvizNodeName();
131                         }
132                         return s;
133                 }
134         }
135
136 }