revised RNA stem/loop processing code that copes with pseudoknots
[jalview.git] / src / jalview / io / RnamlFile.java
index 69786fb..bc6b385 100644 (file)
@@ -1,9 +1,22 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
 package jalview.io;
 
-
-
-
-
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.SecondaryStructureAnnotation;
@@ -36,6 +49,7 @@ import com.stevesoft.pat.Regex;
 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
+import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
 import fr.orsay.lri.varna.factories.RNAFactory;
 import fr.orsay.lri.varna.factories.RNAFactory.RNAFileType;
 import fr.orsay.lri.varna.models.rna.RNA;
@@ -55,13 +69,13 @@ public class RnamlFile extends AlignFile
                
          }
         
-        public RnamlFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
+        public RnamlFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
          {
            super(inFile, type);
            
          }
 
-         public RnamlFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
+         public RnamlFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
          {
            super(source);
            
@@ -89,68 +103,64 @@ public class RnamlFile extends AlignFile
        }
        
        
+       @SuppressWarnings("unchecked")
        public void parse() throws FileNotFoundException, ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax
        {
-                FileReader fr = null;
-                 fr = new FileReader(inFile); 
-                 
-                 BufferedReader r = new BufferedReader (fr);
-                Hashtable seqhash = new Hashtable();
-                 Vector headers = new Vector();
-                 //ArrayList<String> seq =new ArrayList();
-                 //System.out.println(r);
-                  
+                result = RNAFactory.loadSecStrRNAML(getReader());
                  
-                result = RNAFactory.loadSecStrRNAML(r);
-                 
-                System.out.println("this is the secondary scructure:" +result.toArray());
+                System.out.println("this is the secondary scructure:" +result.size());
                  //System.out.println("this is the secondary scructure:" +result.toString());
                  //System.out.println("this is the secondary scructure:" +result.size());
-                       for(int i=0;i<result.size();i++)
-                       {
-                 //System.out.println(result.get(0).getSeq());
-                 //System.out.println(result.get(0).getStructBPSEQ());
-                 //System.out.println(result.get(0).getStructDBN(true));
-                 String rna1 =result.get(0).getStructDBN(true);
-                 //System.out.println(result.get(0).getAnnotations());
-                 String seq = result.get(0).getSeq();
+                SequenceI[] seqs = new SequenceI[result.size()];
                  System.out.println(type); //the type is "File"
                  System.out.println(inFile );//inFile is the path
-                 int begin=0;
-                 int end = seq.length()-1;
-                 SequenceI[] seqs = new SequenceI[1];
-                 id = safeName(getDataName());
-                 seqs[0] = new Sequence(id, seq, begin, end);
-                 SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
-                 seqs[0].addSequenceFeature(feat);
-                 this.setSeqs(seqs);
                  
-                 String[] annot=new String[10000] ;
-                 Annotation[] ann = new Annotation[1000];
-                 for(int j=0;j<rna1.length();j++)
+                for(int i=0;i<result.size();i++)
                        {
-                 annot[j] =rna1.substring(j,j+1);
-                 System.out.println(annot[j]);
+                        RNA current = result.get(i);
+                 System.out.println(current.getSeq());
+                 //System.out.println(result.get(i).getStructBPSEQ());
+                 System.out.println(result.get(i).getStructDBN(true));
+                 System.out.println(i);
+                 String rna =current.getStructDBN(true);
+                 //System.out.println(result.get(0).getAnnotations());
+                 String seq = current.getSeq();
+       
+                 int begin=1;
+                 int end = seq.length();
+                 
+                 id = current.getName();
+                 seqs[i] = new Sequence(id, seq, begin, end);
+                 //System.out.println(seq);
+                 System.out.println("Rna is still "+rna);
+                 //SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
+                 //seqs[i].addSequenceFeature(feat);
+                 
+                 String[] annot=new String[rna.length()] ;
+                 Annotation[] ann = new Annotation[rna.length()];
+               
+                 for(int j=0;j<rna.length();j++)
+                       {
+                         annot[j] =rna.substring(j,j+1);
+                 
                        }
                  
-                 for(int k=0;k<rna1.length();k++)
+                 for(int k=0;k<rna.length();k++)
                {
-                         ann[k] = new Annotation(annot[k], "", ' ', 0f);
-                         
-                         ann[k].secondaryStructure = jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0);
-                         
+                         ann[k] = new Annotation(annot[k], "", jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0), 0f);
+                                                 
                }
-                 AlignmentAnnotation align = new AlignmentAnnotation("Secondary structure","",ann);
-                 seqs[0].addAlignmentAnnotation(align);
-                 seqs[0].setRNA(result.get(begin));
-                 this.annotations.addElement(align);
-                 
+                 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",current.getID(),ann);
                  
+                 seqs[i].addAlignmentAnnotation(align);
+                 seqs[i].setRNA(result.get(i));
+                 this.annotations.addElement(align);
                }
+         this.setSeqs(seqs);
+                
+                
                
-               //Annotate3D tintin = new Annotate3D();
-                 
-               //System.out.println(tintin);  
+
        }
          
        
@@ -360,4 +370,3 @@ public class RnamlFile extends AlignFile
                  return annotations;
          }
 }
-