sequence numbering from 1
[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.SecondaryStructureAnnotation;
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.ws.jws1.Annotate3D;
27
28 import java.io.BufferedReader;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.io.FileReader;
33 import java.lang.reflect.Array;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Hashtable;
37 import java.util.List;
38 import java.util.Vector;
39
40 import javax.xml.parsers.ParserConfigurationException;
41 import javax.xml.parsers.SAXParser;
42 import javax.xml.parsers.SAXParserFactory;
43
44 import org.xml.sax.InputSource;
45 import org.xml.sax.SAXException;
46
47 import com.stevesoft.pat.Regex;
48
49 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
50 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
51 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
52 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
53 import fr.orsay.lri.varna.factories.RNAFactory;
54 import fr.orsay.lri.varna.factories.RNAFactory.RNAFileType;
55 import fr.orsay.lri.varna.models.rna.RNA;
56 import fr.orsay.lri.varna.utils.RNAMLParser;
57
58
59
60
61 public class RnamlFile extends AlignFile
62 {
63          public String id;
64          protected ArrayList<RNA> result;
65          
66          public RnamlFile()
67           {
68                  super();
69                 
70           }
71          
72          public RnamlFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
73           {
74             super(inFile, type);
75             
76           }
77
78           public RnamlFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
79           {
80             super(source);
81             
82           }
83           
84 //        public RnamlFile(BufferedReader r) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
85 //        {
86 //                      super();            
87 //                  parse(r);
88 //                  // sets the index of each sequence in the alignment
89 //                  for( int i=0,c=seqs.size(); i<c; i++ ) {  
90 //                      seqs.get(i).setIndex(i);  
91 //                  }
92 //                
93 //          
94 //        }
95           
96         public BufferedReader CreateReader() throws FileNotFoundException
97         {
98                  FileReader fr = null;
99                   fr = new FileReader(inFile); 
100                   
101                   BufferedReader r = new BufferedReader (fr);
102                   return r;
103         }
104         
105         
106         @SuppressWarnings("unchecked")
107         public void parse() throws FileNotFoundException, ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax
108         {
109                  result = RNAFactory.loadSecStrRNAML(getReader());
110                   
111                  System.out.println("this is the secondary scructure:" +result.size());
112                   //System.out.println("this is the secondary scructure:" +result.toString());
113                   //System.out.println("this is the secondary scructure:" +result.size());
114                  SequenceI[] seqs = new SequenceI[result.size()];
115                   System.out.println(type); //the type is "File"
116                   System.out.println(inFile );//inFile is the path
117                   
118                  for(int i=0;i<result.size();i++)
119                         {
120                          RNA current = result.get(i);
121                   System.out.println(current.getSeq());
122                   //System.out.println(result.get(i).getStructBPSEQ());
123                   System.out.println(result.get(i).getStructDBN(true));
124                   System.out.println(i);
125                   String rna =current.getStructDBN(true);
126                   //System.out.println(result.get(0).getAnnotations());
127                   String seq = current.getSeq();
128         
129                   int begin=1;
130                   int end = seq.length();
131                   
132                   id = current.getName();
133                   seqs[i] = new Sequence(id, seq, begin, end);
134                   //System.out.println(seq);
135                   System.out.println("Rna is still "+rna);
136                   //SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
137                   //seqs[i].addSequenceFeature(feat);
138                   
139                   String[] annot=new String[rna.length()] ;
140                   Annotation[] ann = new Annotation[rna.length()];
141                 
142                   for(int j=0;j<rna.length();j++)
143                         {
144                           annot[j] =rna.substring(j,j+1);
145                   
146                         }
147                   
148                   for(int k=0;k<rna.length();k++)
149                 {
150                           ann[k] = new Annotation(annot[k], "", jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0), 0f);
151                                                   
152                 }
153                   AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",current.getID(),ann);
154                   
155                   seqs[i].addAlignmentAnnotation(align);
156                   seqs[i].setRNA(result.get(i));
157                   this.annotations.addElement(align);
158                 }
159           this.setSeqs(seqs);
160                  
161                  
162                 
163
164         }
165           
166         
167           public void parse(BufferedReader r) throws ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax 
168           {
169                    System.out.println("parse with Buffer (RnamlFile");
170                   Hashtable seqhash = new Hashtable();
171                   Vector headers = new Vector();
172                   //ArrayList<String> seq =new ArrayList();
173                   //System.out.println(r);
174                   
175                   result = RNAFactory.loadSecStr(r,RNAFileType.FILE_TYPE_RNAML);
176                   
177                   System.out.println("this is the secondary scructure:" +result.toArray());
178                   System.out.println("this is the secondary scructure:" +result.toString());
179                   System.out.println("this is the secondary scructure:" +result.size());
180                         for(int i=0;i<result.size();i++)
181                         {
182                   System.out.println(result.get(0).getSeq());
183                   System.out.println(result.get(0).getStructBPSEQ());
184                   System.out.println(result.get(0).getStructDBN(true));
185                   String rna1 =result.get(0).getStructDBN(true);
186                   System.out.println(result.get(0).getAnnotations());
187                   String seq = result.get(0).getSeq();
188                   System.out.println(type); //the type is "File"
189                   System.out.println(inFile );//inFile is the path
190                   int begin=0;
191                   int end = seq.length()-1;
192                   SequenceI[] seqs = new SequenceI[1];
193                   id = safeName(getDataName());
194                   seqs[0] = new Sequence(id, seq, begin, end);
195                   SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
196                   seqs[0].addSequenceFeature(feat);
197                   this.setSeqs(seqs);
198                   
199                   String[] annot=new String[10000] ;
200                   Annotation[] ann = new Annotation[1000];
201                   for(int j=0;j<rna1.length();j++)
202                         {
203                   annot[j] =rna1.substring(j,j+1);
204                   //System.out.println(annot[j]);
205                         }
206                   
207                   for(int k=0;k<rna1.length();k++)
208                 {
209                           ann[k] = new Annotation(annot[k], "", ' ', 0f);
210                           
211                           ann[k].secondaryStructure = jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0);
212                           
213                 }
214                   AlignmentAnnotation align = new AlignmentAnnotation("Secondary structure","",ann);
215                   seqs[0].addAlignmentAnnotation(align);
216                   this.annotations.addElement(align);
217                 }
218                 //Annotate3D tintin = new Annotate3D();
219                   
220                 //System.out.println(tintin);  
221                   
222                   
223                   
224                   
225                   
226                   
227                  
228         
229 //                                      for(RNAMLParser.RNATmp rnaTmp : RNAMLData.getMolecules())
230 //                                      {
231 //                                              // Retrieving parsed data
232 //                                              seq = rnaTmp.getSequence(); //contient la séquence sous forme de liste
233 //                                              System.out.println(seq);
234 //                                              String seqAsStr = "";
235 //                                              for(String s:seq)
236 //                                              {
237 //                                                      // Only consider last character, e.g. in case of modified nucleotide
238 //                                                      seqAsStr += s.charAt(s.length()-1);
239 //                                              }
240 //                                              int begin=0;
241 //                                              int end = seqAsStr.length()-1;
242 //                                              SequenceI[] seqs = new SequenceI[1];
243 //                                              id = safeName(getDataName());
244 //                                              seqs[0] = new Sequence(id, seqAsStr, begin, end);
245 //                                              SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
246 //
247 //                          seqs[0].addSequenceFeature(feat);
248 //                                              this.setSeqs(seqs);
249                                                 
250                                                 //String pos = annot.substring(1, 2);
251                                                 //System.out.println(annot);
252                                                 //System.out.println(pos);
253                                                 
254                                            // ann[1] = new Annotation(pos, "", ' ', 0f);
255                                            // ann[1].secondaryStructure = 'A';
256                                             //System.out.println(ann[1]);
257                                             
258                                            // toto.annotations=ann;
259                                             
260
261                                             
262 ////                                             Annotation[] els = new Annotation[seq.size()];
263 //                                              
264 //                                              //System.err.println(""+seq.size());
265 //                                          Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
266 //                                          String[] annot = new String[10000];
267 //                                        
268 //                                              //Annotation[] ann = new Annotation[allbpsTmp.size()];
269 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
270 //                                              RNAMLParser.BPTemp bp = allbpsTmp.get(i);
271 //                                              //System.err.println("toto ="+bp);
272 //                                              int bp5 = bp.pos5;
273 //                                              annot[bp5]= "(";
274 //                                              //annot[bp5]= '(';
275 //                                              System.out.println(annot[bp5]);
276 //                                              //System.out.println("bp5 ="+bp5);
277 //                                              int bp3 = bp.pos3;
278 //                                      
279 //                                              annot[bp3]= ")";
280 //                                              System.out.println(annot[bp3]);
281 //                                              //System.out.println(annot.get(bp3));
282 //                                              
283 //                                              //String pos1 = annot.substring(bp5, bp5+1);
284 //                                              //String pos2 = annot.substring(bp3, bp3+1);
285 //                                              //ann[i] = new Annotation(pos1, "", ' ', 0f);
286 //                                              //ann[i+1] = new Annotation(pos2, "", ' ', 0f);
287 //                                              //System.out.println(pos1);
288 //                                              
289 //                                              //System.out.println("bp3 ="+bp3);
290 //                                              //ModeleBP newStyle = bp.createBPStyle(mb, part);
291 //                                              }       
292 //                                              Annotation[] ann = new Annotation[annot.length+1];
293 //                                              for(int i=0;i<annot.length;i++)
294 //                                              {               
295 //                                                      if(annot[i]==null)
296 //                                                      {
297 //                                                              annot[i]=".";
298 //                                                      }
299 //                                                      
300 //                                                      ann[i] = new Annotation(annot[i], "", ' ', 0f);
301 //                                              //System.out.print(annot[i]);
302 //                                              }
303 //                                              
304 //                                              AlignmentAnnotation toto = new AlignmentAnnotation("Secondary structure","",ann);
305 //                                              this.annotations.addElement(toto);
306                                         
307                                                 // Creating empty structure of suitable size
308 //                                              int[] str = new int[seq.size()];
309 //                                              for (int i=0;i<str.length;i++)
310 //                                              { str[i] = -1; }
311 //                                              Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
312 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
313 //                                                      RNAMLParser.BPTemp bp = allbpsTmp.get(i);
314 //                                                      //System.err.println("toto ="+bp);
315 //                                                      int bp5 = bp.pos5;
316 //                                                      //System.out.println("bp5 ="+bp5);
317 //                                                      int bp3 = bp.pos3;
318                                                         //System.out.println("bp3 ="+bp3);
319                                                         //ModeleBP newStyle = bp.createBPStyle(mb, part);
320 //                                              }
321                                                 //Annotation ann;
322                                             //ann = new Annotation(bp5, "", ' ', 0f);
323                                 
324                                         //}
325
326                         
327
328 //                              return result;
329           
330 }
331
332           
333           public static String print(SequenceI[] s)
334           {
335             return "not yet implemented";
336           }
337           
338           public String print()
339           {
340                 System.out.print("affiche :");
341             return print(getSeqsAsArray());
342           }
343           
344           /**
345            * make a friendly ID string.
346            * 
347            * @param dataName
348            * @return truncated dataName to after last '/'
349            */
350           private String safeName(String dataName)
351           {
352             int b = 0;
353             while ((b = dataName.indexOf("/")) > -1 && b < dataName.length() )
354             {              
355                 dataName = dataName.substring(b + 1).trim();
356                 
357             }
358             int e = (dataName.length() - dataName.indexOf("."))+1;
359             dataName = dataName.substring(1,e).trim();
360             return dataName;
361           }
362           
363           public ArrayList getRNA()
364           {
365                   return result;
366           }
367           
368           public Vector getAnnot()
369           {
370                   return annotations;
371           }
372 }