JAL-1140 reinstate the simpler version of parsing code from annotate3d update
[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
26 import jalview.analysis.SecStrConsensus.SimpleBP;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceI;
31
32 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
33 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
34 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
35 import fr.orsay.lri.varna.factories.RNAFactory;
36 import fr.orsay.lri.varna.models.rna.RNA;
37
38 public class RnamlFile extends AlignFile
39 {
40   public String id;
41
42   protected ArrayList<RNA> result;
43
44   public RnamlFile()
45   {
46     super();
47
48   }
49
50   public RnamlFile(String inFile, String type) throws IOException
51   {
52     super(inFile, type);
53
54   }
55
56   public RnamlFile(FileParse source) throws IOException
57   {
58     super(source);
59
60   }
61
62   public BufferedReader CreateReader() throws FileNotFoundException
63   {
64     FileReader fr = null;
65     fr = new FileReader(inFile);
66
67     BufferedReader r = new BufferedReader(fr);
68     return r;
69   }
70
71   /*
72    * (non-Javadoc)
73    * 
74    * @see jalview.io.AlignFile#parse()
75    */
76   public void parse() throws IOException
77   {
78     if (System.getProperty("java.version").indexOf("1.6") > -1
79             || System.getProperty("java.version").indexOf("1.5") > -1)
80     {
81       // patch for 'This parser does not support specification "null" version
82       // "null"' error
83       // this hack ensures we get a properly updated SAXParserFactory on older
84       // JVMs
85       // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
86       System.setProperty("javax.xml.parsers.SAXParserFactory",
87               "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
88     }
89     // rather than lose exception semantics whilst parsing RNAML with VARNA we
90     // wrap the routine and catch all exceptions before passing them up the
91     // chain as an IOException
92     try
93     {
94       _parse();
95     } catch (ExceptionPermissionDenied pdx)
96     {
97       errormessage = "Couldn't access datasource (" + pdx.getMessage()
98               + ")";
99       throw new IOException(pdx);
100     } catch (ExceptionLoadingFailed lf)
101     {
102       errormessage = "Couldn't process data as RNAML file ("
103               + lf.getMessage() + ")";
104       throw new IOException(lf);
105     } catch (ExceptionFileFormatOrSyntax iff)
106     {
107       errormessage = "Invalid RNAML file (" + iff.getMessage() + ")";
108       throw new IOException(iff);
109     } catch (Exception x)
110     {
111       error = true;
112       errormessage = "Problem parsing data as RNAML (" + x.getMessage()
113               + ")";
114       throw new IOException("Couldn't parse the datasource as RNAML", x);
115     }
116   }
117
118   @SuppressWarnings("unchecked")
119   public void _parse() throws FileNotFoundException,
120           ExceptionPermissionDenied, ExceptionLoadingFailed,
121           ExceptionFileFormatOrSyntax
122   {
123
124     result = RNAFactory.loadSecStrRNAML(getReader());
125
126     ArrayList<ArrayList> allarray = new ArrayList();
127     ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
128     ArrayList strucinarray = new ArrayList();
129     SequenceI[] seqs = new SequenceI[result.size()];
130
131     for (int i = 0; i < result.size(); i++)
132     {
133
134       RNA current = result.get(i);
135       String rna = current.getStructDBN(true);
136       String seq = current.getSeq();
137       int begin = 1;
138       int end = seq.length();
139
140       id = current.getName();
141       seqs[i] = new Sequence(id, seq, begin, end);
142
143       seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
144       String[] annot = new String[rna.length()];
145       Annotation[] ann = new Annotation[rna.length()];
146
147       for (int j = 0; j < rna.length(); j++)
148       {
149         annot[j] = "" + rna.charAt(j);
150
151       }
152       for (int k = 0; k < rna.length(); k++)
153       {
154         ann[k] = new Annotation(annot[k], "",
155                 jalview.schemes.ResidueProperties.getRNASecStrucState(
156                         annot[k]).charAt(0), 0f);
157       }
158
159       AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
160               current.getID(), ann);
161
162       seqs[i].addAlignmentAnnotation(align);
163       seqs[i].setRNA(result.get(i));
164
165       allarray.add(strucinarray);
166
167       annotations.addElement(align);
168       BP.add(align.bps);
169
170     }
171
172     setSeqs(seqs);
173   }
174
175   public static String print(SequenceI[] s)
176   {
177     return "not yet implemented";
178   }
179
180   public String print()
181   {
182     System.out.print("print :");
183     return print(getSeqsAsArray());
184   }
185
186   public ArrayList getRNA()
187   {
188     return result;
189   }
190
191   // public static void main(String[] args) {
192   // Pattern p= Pattern.compile("(.+)[.][^.]+");
193   // Matcher m = p.matcher("toto.xml.zip");
194   // System.out.println(m.matches());
195   // System.out.println(m.group(1));
196   // }
197   /**
198    * make a friendly ID string.
199    * 
200    * @param dataName
201    * @return truncated dataName to after last '/'
202    */
203   private String safeName(String dataName)
204   {
205     int b = 0;
206     while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
207     {
208       dataName = dataName.substring(b + 1).trim();
209
210     }
211     int e = (dataName.length() - dataName.indexOf(".")) + 1;
212     dataName = dataName.substring(1, e).trim();
213     return dataName;
214   }
215 }