Anne's refined RNAML parser. Seems to have some bugs still (merge with jprocter's...
[jalview.git] / src / jalview / io / RnamlFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * 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.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import java.io.BufferedReader;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.util.ArrayList;
24 import java.util.Vector;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import jalview.analysis.SecStrConsensus;
29 import jalview.analysis.SecStrConsensus.SimpleBP;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.SecondaryStructureAnnotation;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36
37 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
38 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
39 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
40 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
41 import fr.orsay.lri.varna.factories.RNAFactory;
42 import fr.orsay.lri.varna.factories.RNAFactory.RNAFileType;
43 import fr.orsay.lri.varna.models.rna.RNA;
44 import fr.orsay.lri.varna.utils.RNAMLParser;
45
46 public class RnamlFile extends AlignFile {
47         public int id;
48         public String namefile;
49         public String name;
50         protected ArrayList<RNA> result;
51
52         public RnamlFile() {
53                 super();
54
55         }
56
57         public RnamlFile(String inFile, String type) throws Exception {
58                 super(inFile, type);
59
60         }
61
62         public RnamlFile(FileParse source) throws Exception {
63                 super(source);
64
65         }
66
67         public BufferedReader CreateReader() throws FileNotFoundException {
68                 FileReader fr = null;
69                 fr = new FileReader(inFile);
70
71                 BufferedReader r = new BufferedReader(fr);
72                 return r;
73         }
74
75         @SuppressWarnings("unchecked")
76         public void parse() throws FileNotFoundException,
77                         ExceptionPermissionDenied, ExceptionLoadingFailed,
78                         ExceptionFileFormatOrSyntax {
79
80                 result = RNAFactory.loadSecStrRNAML(getReader());
81
82                 ArrayList<ArrayList> allarray = new ArrayList();
83                 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
84                 ArrayList strucinarray = new ArrayList();
85                 SequenceI[] seqs = new SequenceI[result.size()];
86                 namefile = inFile.getName();
87
88                 for (int i = 0; i < result.size(); i++) {
89
90                         RNA current = result.get(i);
91                         String rna = current.getStructDBN(true);
92                         String seq = current.getSeq();
93                         int begin = 1;
94                         int end = 1000;
95                         id = i;
96                         System.out.println("id=" + i);
97                         name = this.safeName(namefile, i);
98                         seqs[i] = new Sequence(name, seq, begin, end);
99
100                         seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
101                         String[] annot = new String[rna.length()];
102                         Annotation[] ann = new Annotation[rna.length()];
103
104                         for (int j = 0; j < rna.length(); j++) {
105                                 annot[j] = "" + rna.charAt(j);
106
107                         }
108                         for (int k = 0; k < rna.length(); k++) {
109                                 ann[k] = new Annotation(annot[k], "",
110                                                 jalview.schemes.ResidueProperties.getRNASecStrucState(
111                                                                 annot[k]).charAt(0), 0f);
112                         }
113
114                         AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
115                                         current.getID(), ann);
116
117                         seqs[i].addAlignmentAnnotation(align);
118                         seqs[i].setRNA(result.get(i));
119
120                         char[] struc = align.getRNAStruc().toCharArray();
121
122                         allarray.add(strucinarray);
123
124                         this.annotations.addElement(align);
125                         BP.add(align.bps);
126                         
127                 }
128                 this.setSeqs(seqs);
129                 int[] tab = SecStrConsensus.extractConsensus(BP);
130
131
132         }
133
134         public void parse(BufferedReader r) throws ExceptionPermissionDenied,
135                         ExceptionLoadingFailed, ExceptionFileFormatOrSyntax {
136
137                 result = RNAFactory.loadSecStrRNAML(r);
138
139                 ArrayList<ArrayList> allarray = new ArrayList();
140                 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
141                 ArrayList strucinarray = new ArrayList();
142                 SequenceI[] seqs = new SequenceI[result.size()];
143                 namefile = inFile.getName();
144
145                 for (int i = 0; i < result.size(); i++) {
146
147                         RNA current = result.get(i);
148                         String rna = current.getStructDBN(true);
149
150                         String seq = current.getSeq();
151                         int begin = 1;
152                         int end = 1000;
153                         id = i;
154                         name = this.safeName(namefile, i);
155                         seqs[i] = new Sequence(name, seq, begin, end);
156
157                         seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
158                         String[] annot = new String[rna.length()];
159                         Annotation[] ann = new Annotation[rna.length()];
160
161                         for (int j = 0; j < rna.length(); j++) {
162                                 annot[j] = "" + rna.charAt(j);
163
164                         }
165                         for (int k = 0; k < rna.length(); k++) {
166                                 ann[k] = new Annotation(annot[k], "",
167                                                 jalview.schemes.ResidueProperties.getRNASecStrucState(
168                                                                 annot[k]).charAt(0), 0f);
169                         }
170
171                         AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
172                                         current.getID(), ann);
173
174                         seqs[i].addAlignmentAnnotation(align);
175                         seqs[i].setRNA(result.get(i));
176
177                         char[] struc = align.getRNAStruc().toCharArray();
178                         for (int y = 0; y < struc.length; y++) {
179                                 strucinarray.add(struc[y]);
180                                 // System.out.println("structy"+struc[y]);
181                         }
182                         // System.out.println("Sequence"+i+"\t : "+strucinarray.toString());
183                         allarray.add(strucinarray);
184
185                         this.annotations.addElement(align);
186                         System.out.println(rna);
187                         BP.add(align.bps);
188                         System.out.println(align.bps.size());
189                 }
190                 this.setSeqs(seqs);
191                 int[] tab = SecStrConsensus.extractConsensus(BP);
192
193         
194         }
195
196         
197
198         public static String print(SequenceI[] s) {
199                 return "not yet implemented";
200         }
201
202         public String print() {
203                 System.out.print("print :");
204                 return print(getSeqsAsArray());
205         }
206
207         public ArrayList getRNA() {
208                 return result;
209         }
210
211         public Vector getAnnot() {
212                 return annotations;
213         }
214
215         // public static void main(String[] args) {
216         // Pattern p= Pattern.compile("(.+)[.][^.]+");
217         // Matcher m = p.matcher("toto.xml.zip");
218         // System.out.println(m.matches());
219         // System.out.println(m.group(1));
220         // }
221         public String safeName(String namefile, int id) {
222
223                 Pattern p = Pattern.compile("(.*)[.][^.]+");
224                 Matcher m = p.matcher(namefile);
225                 name = m.group(1) + "_" + id;
226
227                 return name;
228
229         }
230
231 }