JAL-1355
[jalview.git] / src / jalview / io / RnamlFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import java.io.BufferedReader;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.util.ArrayList;
28
29 import jalview.analysis.SecStrConsensus.SimpleBP;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.util.MessageManager;
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 public class RnamlFile extends AlignFile
43 {
44   public String id;
45
46   protected ArrayList<RNA> result;
47
48   public RnamlFile()
49   {
50     super();
51
52   }
53
54   public RnamlFile(String inFile, String type) throws IOException
55   {
56     super(inFile, type);
57
58   }
59
60   public RnamlFile(FileParse source) throws IOException
61   {
62     super(source);
63
64   }
65
66   public BufferedReader CreateReader() throws FileNotFoundException
67   {
68     FileReader fr = null;
69     fr = new FileReader(inFile);
70
71     BufferedReader r = new BufferedReader(fr);
72     return r;
73   }
74
75   /*
76    * (non-Javadoc)
77    * 
78    * @see jalview.io.AlignFile#parse()
79    */
80   public void parse() throws IOException
81   {
82     if (System.getProperty("java.version").indexOf("1.6") > -1
83             || System.getProperty("java.version").indexOf("1.5") > -1)
84     {
85       // patch for 'This parser does not support specification "null" version
86       // "null"' error
87       // this hack ensures we get a properly updated SAXParserFactory on older
88       // JVMs
89       // thanks to Stefan Birkner over at https://coderwall.com/p/kqsrrw
90       System.setProperty("javax.xml.parsers.SAXParserFactory",
91               "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
92     }
93     // rather than lose exception semantics whilst parsing RNAML with VARNA we
94     // wrap the routine and catch all exceptions before passing them up the
95     // chain as an IOException
96     try
97     {
98       _parse();
99     } catch (ExceptionPermissionDenied pdx)
100     {
101       errormessage = MessageManager.formatMessage("exception.rnaml_couldnt_access_datasource", new String[]{pdx.getMessage()});
102       throw new IOException(pdx);
103     } catch (ExceptionLoadingFailed lf)
104     {
105       errormessage = MessageManager.formatMessage("exception.ranml_couldnt_process_data", new String[]{lf.getMessage()});
106       throw new IOException(lf);
107     } catch (ExceptionFileFormatOrSyntax iff)
108     {
109         errormessage = MessageManager.formatMessage("exception.ranml_invalid_file", new String[]{iff.getMessage()});
110       throw new IOException(iff);
111     } catch (Exception x)
112     {
113       error = true;
114       errormessage = MessageManager.formatMessage("exception.ranml_problem_parsing_data", new String[]{x.getMessage()});
115       throw new IOException(errormessage , x);
116     }
117   }
118
119   @SuppressWarnings("unchecked")
120   public void _parse() throws FileNotFoundException,
121           ExceptionPermissionDenied, ExceptionLoadingFailed,
122           ExceptionFileFormatOrSyntax
123   {
124
125     result = RNAFactory.loadSecStrRNAML(getReader());
126
127     ArrayList<ArrayList> allarray = new ArrayList();
128     ArrayList<ArrayList<SimpleBP>> BP = new ArrayList();
129     ArrayList strucinarray = new ArrayList();
130     SequenceI[] seqs = new SequenceI[result.size()];
131
132     for (int i = 0; i < result.size(); i++)
133     {
134
135       RNA current = result.get(i);
136       String rna = current.getStructDBN(true);
137       String seq = current.getSeq();
138       int begin = 1;
139       int end = seq.length();
140
141       id = current.getName();
142       seqs[i] = new Sequence(id, seq, begin, end);
143
144       seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength()));
145       String[] annot = new String[rna.length()];
146       Annotation[] ann = new Annotation[rna.length()];
147
148       for (int j = 0; j < rna.length(); j++)
149       {
150         annot[j] = "" + rna.charAt(j);
151
152       }
153       for (int k = 0; k < rna.length(); k++)
154       {
155         ann[k] = new Annotation(annot[k], "",
156                 jalview.schemes.ResidueProperties.getRNASecStrucState(
157                         annot[k]).charAt(0), 0f);
158       }
159
160       AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
161               current.getID(), ann);
162
163       seqs[i].addAlignmentAnnotation(align);
164       seqs[i].setRNA(result.get(i));
165
166       allarray.add(strucinarray);
167
168       annotations.addElement(align);
169       BP.add(align.bps);
170
171     }
172
173     setSeqs(seqs);
174   }
175
176   public static String print(SequenceI[] s)
177   {
178     return "not yet implemented";
179   }
180
181   public String print()
182   {
183     System.out.print("print :");
184     return print(getSeqsAsArray());
185   }
186
187   public ArrayList getRNA()
188   {
189     return result;
190   }
191
192   // public static void main(String[] args) {
193   // Pattern p= Pattern.compile("(.+)[.][^.]+");
194   // Matcher m = p.matcher("toto.xml.zip");
195   // System.out.println(m.matches());
196   // System.out.println(m.group(1));
197   // }
198   /**
199    * make a friendly ID string.
200    * 
201    * @param dataName
202    * @return truncated dataName to after last '/'
203    */
204   private String safeName(String dataName)
205   {
206     int b = 0;
207     while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
208     {
209       dataName = dataName.substring(b + 1).trim();
210
211     }
212     int e = (dataName.length() - dataName.indexOf(".")) + 1;
213     dataName = dataName.substring(1, e).trim();
214     return dataName;
215   }
216 }