fix merge conflicts for finess on JAL-1140 (introduced by 1a8f953dd71a9e319e910261927...
[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.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Vector;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import jalview.analysis.SecStrConsensus;
30 import jalview.analysis.SecStrConsensus.SimpleBP;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.Annotation;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceI;
35
36 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
37 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
38 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
39 import fr.orsay.lri.varna.factories.RNAFactory;
40 import fr.orsay.lri.varna.models.rna.RNA;
41
42 /**
43  * @author jimp
44  *
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         /* (non-Javadoc)
76          * @see jalview.io.AlignFile#parse()
77          */
78         public void parse() throws IOException
79         {
80         // rather than lose exception semantics whilst parsing RNAML with VARNA we
81         // wrap the routine and catch all exceptions before passing them up the
82         // chain as an IOException
83           try {
84             _parse();
85           } catch (Exception x)
86           {
87             error=true;
88             errormessage="Problem parsing data as RNAML ("+x.getMessage()+")";
89             throw new IOException("Couldn't parse the datasource as RNAML",x);
90           }
91         }
92         @SuppressWarnings("unchecked")
93         public void _parse() throws FileNotFoundException,
94                         ExceptionPermissionDenied, ExceptionLoadingFailed,
95                         ExceptionFileFormatOrSyntax {
96
97                 result = RNAFactory.loadSecStrRNAML(getReader());
98
99                 ArrayList<ArrayList> allarray = new ArrayList();
100                 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
101                 ArrayList strucinarray = new ArrayList();
102                 SequenceI[] seqs = new SequenceI[result.size()];
103                 namefile = inFile.getName();
104
105                 for (int i = 0; i < result.size(); i++) {
106
107                         RNA current = result.get(i);
108                         String rna = current.getStructDBN(true);
109                         String seq = current.getSeq();
110                         int begin = 1;
111                         int end = 1000;
112                         id = i;
113                         System.out.println("id=" + i);
114                         name = this.safeName(namefile, i);
115                         seqs[i] = new Sequence(name, seq, begin, end);
116
117                         seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
118                         String[] annot = new String[rna.length()];
119                         Annotation[] ann = new Annotation[rna.length()];
120
121                         for (int j = 0; j < rna.length(); j++) {
122                                 annot[j] = "" + rna.charAt(j);
123
124                         }
125                         for (int k = 0; k < rna.length(); k++) {
126                                 ann[k] = new Annotation(annot[k], "",
127                                                 jalview.schemes.ResidueProperties.getRNASecStrucState(
128                                                                 annot[k]).charAt(0), 0f);
129                         }
130
131                         AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
132                                         current.getID(), ann);
133
134                         seqs[i].addAlignmentAnnotation(align);
135                         seqs[i].setRNA(result.get(i));
136
137                         char[] struc = align.getRNAStruc().toCharArray();
138
139                         allarray.add(strucinarray);
140
141                         this.annotations.addElement(align);
142                         BP.add(align.bps);
143                         
144                 }
145                 this.setSeqs(seqs);
146                 int[] tab = SecStrConsensus.extractConsensus(BP);
147
148
149         }
150
151         public void parse(BufferedReader r) throws ExceptionPermissionDenied,
152                         ExceptionLoadingFailed, ExceptionFileFormatOrSyntax {
153
154                 result = RNAFactory.loadSecStrRNAML(r);
155
156                 ArrayList<ArrayList> allarray = new ArrayList();
157                 ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
158                 ArrayList strucinarray = new ArrayList();
159                 SequenceI[] seqs = new SequenceI[result.size()];
160                 namefile = inFile.getName();
161
162                 for (int i = 0; i < result.size(); i++) {
163
164                         RNA current = result.get(i);
165                         String rna = current.getStructDBN(true);
166
167                         String seq = current.getSeq();
168                         int begin = 1;
169                         int end = 1000;
170                         id = i;
171                         name = this.safeName(namefile, i);
172                         seqs[i] = new Sequence(name, seq, begin, end);
173
174                         seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
175                         String[] annot = new String[rna.length()];
176                         Annotation[] ann = new Annotation[rna.length()];
177
178                         for (int j = 0; j < rna.length(); j++) {
179                                 annot[j] = "" + rna.charAt(j);
180
181                         }
182                         for (int k = 0; k < rna.length(); k++) {
183                                 ann[k] = new Annotation(annot[k], "",
184                                                 jalview.schemes.ResidueProperties.getRNASecStrucState(
185                                                                 annot[k]).charAt(0), 0f);
186                         }
187
188                         AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
189                                         current.getID(), ann);
190
191                         seqs[i].addAlignmentAnnotation(align);
192                         seqs[i].setRNA(result.get(i));
193
194                         char[] struc = align.getRNAStruc().toCharArray();
195                         for (int y = 0; y < struc.length; y++) {
196                                 strucinarray.add(struc[y]);
197                                 // System.out.println("structy"+struc[y]);
198                         }
199                         // System.out.println("Sequence"+i+"\t : "+strucinarray.toString());
200                         allarray.add(strucinarray);
201
202                         this.annotations.addElement(align);
203                         System.out.println(rna);
204                         BP.add(align.bps);
205                         System.out.println(align.bps.size());
206                 }
207                 this.setSeqs(seqs);
208                 int[] tab = SecStrConsensus.extractConsensus(BP);
209
210         
211         }
212
213         
214
215         public static String print(SequenceI[] s) {
216                 return "not yet implemented";
217         }
218
219         public String print() {
220                 System.out.print("print :");
221                 return print(getSeqsAsArray());
222         }
223
224         public ArrayList getRNA() {
225                 return result;
226         }
227
228         public Vector getAnnot() {
229                 return annotations;
230         }
231
232         // public static void main(String[] args) {
233         // Pattern p= Pattern.compile("(.+)[.][^.]+");
234         // Matcher m = p.matcher("toto.xml.zip");
235         // System.out.println(m.matches());
236         // System.out.println(m.group(1));
237         // }
238         public String safeName(String namefile, int id) {
239
240                 Pattern p = Pattern.compile("(.*)[.][^.]+");
241                 Matcher m = p.matcher(namefile);
242                 name = m.group(1) + "_" + id;
243
244                 return name;
245
246         }
247
248 }