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