allows for null Jmol viewer
[jalview.git] / src2 / fr / orsay / lri / varna / factories / RNAAlignment.java
1 package fr.orsay.lri.varna.factories;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.Hashtable;
8 import java.util.Stack;
9
10 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
11 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
12 import fr.orsay.lri.varna.models.rna.RNA;
13
14 /**
15  * BH SwingJS -- must explicitly check for array out of bounds
16  */
17 public class RNAAlignment {
18    private ArrayList<String> _lst = new ArrayList<String> (); 
19    private Hashtable<String, Integer> _index = new Hashtable<String, Integer> ();
20    private Hashtable<String, String> _accession = new Hashtable<String, String> ();
21    private String _secStr = "";
22    
23    public void addSequence(String id, String s)
24    {
25            if (!_index.containsKey(id))
26            {
27                    _index.put(id,_lst.size());
28                    _lst.add(s);
29            }
30            _lst.set(_index.get(id),s);
31    }
32    
33    public void setSecStr(String s)
34    {
35            _secStr = s;
36    }
37    
38   public void setAccession(String id, String AC)
39   {
40           _accession.put(id,AC);
41   }
42    
43    public ArrayList<RNA> getRNAs() throws ExceptionUnmatchedClosingParentheses
44    {
45            ArrayList<RNA> result = new ArrayList<RNA>(); 
46            int[] str = RNAFactory.parseSecStr(_secStr);
47            ArrayList<String> ids = new ArrayList<String>(_index.keySet());
48            Collections.sort(ids,new Comparator<String>(){
49                 public int compare(String o1, String o2) {
50                         return o1.compareToIgnoreCase(o2);
51                 }});
52            for (String id: ids )
53            {
54                    int n = _index.get(id);
55                    String seq = _lst.get(n);
56                    if (seq.length() != str.length)
57                            throw new ArrayIndexOutOfBoundsException(); // BH SwingJS -- must explicitly check for array out of bounds
58                    String nseq ="";
59                    String nstr ="";
60                    for(int i=0;i<seq.length();i++)
61                    {
62                            char c = seq.charAt(i);
63                            int j = str[i];
64                            
65                            if (!(c=='.' || c==':' || c=='-'))
66                            {
67                                    nseq += c;
68                                    if (j==-1)
69                                    {
70                                            nstr += '.';
71                                    }
72                                    else
73                                    {
74                                            int cp = seq.charAt(j);
75                                            if (cp=='.' || cp==':' || cp=='-')
76                                            {
77                                                    nstr += '.';                                            
78                                            }
79                                            else
80                                            {
81                                                    nstr += _secStr.charAt(i);
82                                            }
83                                    }
84                            }
85                    }
86                    RNA r = new RNA();
87                    try {
88                         r.setRNA(nseq, nstr);
89                         r.setName(id);
90                         if (_accession.containsKey(id))
91                         {
92                                 r.setID(_accession.get(id));
93                         }
94                         else
95                         {
96                                 r.setID(id);
97                         }
98                         result.add(r);
99                 } catch (ExceptionFileFormatOrSyntax e) {
100                         // TODO Auto-generated catch block
101                         e.printStackTrace();
102                 }
103            }
104            return result;
105    }
106 }