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