patch for 'This parser does not support specification "null" version "null"' error
[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 jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.Annotation;
22 import jalview.datamodel.Sequence;
23 import jalview.datamodel.SequenceI;
24
25 import java.io.BufferedReader;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.util.ArrayList;
30
31 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
32 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
33 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
34 import fr.orsay.lri.varna.factories.RNAFactory;
35 import fr.orsay.lri.varna.models.rna.RNA;
36
37 public class RnamlFile extends AlignFile
38 {
39   public String id;
40
41   protected ArrayList<RNA> result;
42
43   public RnamlFile()
44   {
45     super();
46
47   }
48
49   public RnamlFile(String inFile, String type) throws IOException
50   {
51     super(inFile, type);
52
53   }
54
55   public RnamlFile(FileParse source) throws IOException
56   {
57     super(source);
58
59   }
60
61   // public RnamlFile(BufferedReader r) throws IOException,
62   // ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException,
63   // ExceptionPermissionDenied, ExceptionLoadingFailed
64   // {
65   // super();
66   // parse(r);
67   // // sets the index of each sequence in the alignment
68   // for( int i=0,c=seqs.size(); i<c; i++ ) {
69   // seqs.get(i).setIndex(i);
70   // }
71   //
72   //
73   // }
74
75   public BufferedReader CreateReader() throws FileNotFoundException
76   {
77     FileReader fr = null;
78     fr = new FileReader(inFile);
79
80     BufferedReader r = new BufferedReader(fr);
81     return r;
82   }
83
84   public void parse() throws IOException
85   {
86     if (System.getProperty("java.version").indexOf("1.6")>-1 || System.getProperty("java.version").indexOf("1.5")>-1)
87     {
88       // patch for 'This parser does not support specification "null" version "null"' error
89       // this hack ensures we get a properly updated SAXParserFactory on older JVMs
90       // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
91       System.setProperty("javax.xml.parsers.SAXParserFactory", "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
92     }
93     result = null;
94     try
95     {
96       result = RNAFactory.loadSecStrRNAML(getReader());
97
98     } catch (ExceptionPermissionDenied pdx)
99     {
100       errormessage = "Couldn't access datasource (" + pdx.getMessage()
101               + ")";
102       throw new IOException(pdx);
103     } catch (ExceptionLoadingFailed lf)
104     {
105       errormessage = "Couldn't process data as RNAML file ("
106               + lf.getMessage() + ")";
107       throw new IOException(lf);
108     } catch (ExceptionFileFormatOrSyntax iff)
109     {
110       errormessage = "Invalid RNAML file (" + iff.getMessage() + ")";
111       throw new IOException(iff);
112     }
113
114     SequenceI[] seqs = new SequenceI[result.size()];
115
116     for (int i = 0; i < result.size(); i++)
117     {
118       // DEBUG System.err.println("RNAML entry "+i);
119       RNA current = result.get(i);
120       String seq = current.getSeq();
121       // DEBUG System.err.println(seq);
122       String rna = current.getStructDBN(true);
123       // DEBUG System.err.println(rna);
124
125       int begin = 1;
126       int end = seq.length(); // TODO: compute non-gapped length for sequence
127
128       id = current.getName();
129       seqs[i] = new Sequence(id, seq, begin, end);
130
131       String[] annot = new String[rna.length()];
132       Annotation[] ann = new Annotation[rna.length()];
133
134       for (int j = 0; j < rna.length(); j++)
135       {
136         annot[j] = rna.substring(j, j + 1);
137
138       }
139
140       for (int k = 0; k < rna.length(); k++)
141       {
142         ann[k] = new Annotation(annot[k], "",
143                 jalview.schemes.ResidueProperties.getRNASecStrucState(
144                         annot[k]).charAt(0), 0f);
145
146       }
147       AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
148               current.getID(), ann);
149
150       seqs[i].addAlignmentAnnotation(align);
151       seqs[i].setRNA(result.get(i));
152       annotations.addElement(align);
153     }
154     setSeqs(seqs);
155   }
156
157   public static String print(SequenceI[] s)
158   {
159     // TODO: implement RNAML export option
160     return "not yet implemented";
161   }
162
163   public String print()
164   {
165     return print(getSeqsAsArray());
166   }
167
168   /**
169    * make a friendly ID string.
170    * 
171    * @param dataName
172    * @return truncated dataName to after last '/'
173    */
174   private String safeName(String dataName)
175   {
176     int b = 0;
177     while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
178     {
179       dataName = dataName.substring(b + 1).trim();
180
181     }
182     int e = (dataName.length() - dataName.indexOf(".")) + 1;
183     dataName = dataName.substring(1, e).trim();
184     return dataName;
185   }
186 }