Add support RNAML format
[jalview.git] / src / jalview / io / RnamlFile.java
1 package jalview.io;
2
3
4
5
6
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.SecondaryStructureAnnotation;
10 import jalview.datamodel.Sequence;
11 import jalview.datamodel.SequenceFeature;
12 import jalview.datamodel.SequenceI;
13
14 import java.io.BufferedReader;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.Reader;
18 import java.io.FileReader;
19 import java.lang.reflect.Array;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Hashtable;
23 import java.util.List;
24 import java.util.Vector;
25
26 import javax.xml.parsers.ParserConfigurationException;
27 import javax.xml.parsers.SAXParser;
28 import javax.xml.parsers.SAXParserFactory;
29
30 import org.xml.sax.InputSource;
31 import org.xml.sax.SAXException;
32
33 import com.stevesoft.pat.Regex;
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 import fr.orsay.lri.varna.utils.RNAMLParser;
41
42
43
44
45 public class RnamlFile extends AlignFile
46 {
47          public String id;
48          
49          public RnamlFile()
50           {
51                  super();
52                 
53           }
54          
55          public RnamlFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
56           {
57             super(inFile, type);
58             
59           }
60
61           public RnamlFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
62           {
63             super(source);
64             
65           }
66           
67           
68         
69         
70           public void parse() throws ExceptionPermissionDenied, ExceptionLoadingFailed, ExceptionFileFormatOrSyntax, FileNotFoundException 
71           {
72                   System.out.println("this is a rnaml file");
73                   
74                   
75                   FileReader fr = null;
76                   fr = new FileReader(inFile); 
77                   
78                   BufferedReader r = new BufferedReader (fr);
79                  
80                   Hashtable seqhash = new Hashtable();
81                   Vector headers = new Vector();
82                   //ArrayList<String> seq =new ArrayList();
83                   System.out.println(r);
84                   ArrayList<RNA> result = RNAFactory.loadSecStrRNAML(r);
85                   
86                   System.out.println("this is the secondary scructure:" +result.toArray());
87                   System.out.println("this is the secondary scructure:" +result.toString());
88                   System.out.println("this is the secondary scructure:" +result.size());
89                         for(int i=0;i<result.size();i++)
90                         {
91                   System.out.println(result.get(0).getSeq());
92                   System.out.println(result.get(0).getStructBPSEQ());
93                   System.out.println(result.get(0).getStructDBN(true));
94                   String rna1 =result.get(0).getStructDBN(true);
95                   System.out.println(result.get(0).getAnnotations());
96                   String seq = result.get(0).getSeq();
97                   System.out.println(type); //the type is "File"
98                   System.out.println(inFile );//inFile is the path
99                   int begin=0;
100                   int end = seq.length()-1;
101                   SequenceI[] seqs = new SequenceI[1];
102                   id = safeName(getDataName());
103                   seqs[0] = new Sequence(id, seq, begin, end);
104                   SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
105                   seqs[0].addSequenceFeature(feat);
106                   this.setSeqs(seqs);
107                   
108                   String[] annot=new String[10000] ;
109                   Annotation[] ann = new Annotation[1000];
110                   for(int j=0;j<rna1.length();j++)
111                         {
112                   annot[j] =rna1.substring(j,j+1);
113                   System.out.println(annot[j]);
114                         }
115                   
116                   for(int k=0;k<rna1.length();k++)
117                 {
118                           ann[k] = new Annotation(annot[k], "", ' ', 0f);
119                           
120                           ann[k].secondaryStructure = jalview.schemes.ResidueProperties.getRNASecStrucState(annot[k]).charAt(0);
121                           
122                 }
123                   AlignmentAnnotation align = new AlignmentAnnotation("Secondary structure","",ann);
124                   seqs[0].addAlignmentAnnotation(align);
125                   this.annotations.addElement(align);
126                 }
127                   
128                   
129                   
130                   
131                   
132                   
133                   
134                   
135                  
136         
137 //                                      for(RNAMLParser.RNATmp rnaTmp : RNAMLData.getMolecules())
138 //                                      {
139 //                                              // Retrieving parsed data
140 //                                              seq = rnaTmp.getSequence(); //contient la séquence sous forme de liste
141 //                                              System.out.println(seq);
142 //                                              String seqAsStr = "";
143 //                                              for(String s:seq)
144 //                                              {
145 //                                                      // Only consider last character, e.g. in case of modified nucleotide
146 //                                                      seqAsStr += s.charAt(s.length()-1);
147 //                                              }
148 //                                              int begin=0;
149 //                                              int end = seqAsStr.length()-1;
150 //                                              SequenceI[] seqs = new SequenceI[1];
151 //                                              id = safeName(getDataName());
152 //                                              seqs[0] = new Sequence(id, seqAsStr, begin, end);
153 //                                              SequenceFeature feat = new SequenceFeature(type, "",5, 13, 0f, null);
154 //
155 //                          seqs[0].addSequenceFeature(feat);
156 //                                              this.setSeqs(seqs);
157                                                 
158                                                 //String pos = annot.substring(1, 2);
159                                                 //System.out.println(annot);
160                                                 //System.out.println(pos);
161                                                 
162                                            // ann[1] = new Annotation(pos, "", ' ', 0f);
163                                            // ann[1].secondaryStructure = 'A';
164                                             //System.out.println(ann[1]);
165                                             
166                                            // toto.annotations=ann;
167                                             
168
169                                             
170 ////                                             Annotation[] els = new Annotation[seq.size()];
171 //                                              
172 //                                              //System.err.println(""+seq.size());
173 //                                          Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
174 //                                          String[] annot = new String[10000];
175 //                                        
176 //                                              //Annotation[] ann = new Annotation[allbpsTmp.size()];
177 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
178 //                                              RNAMLParser.BPTemp bp = allbpsTmp.get(i);
179 //                                              //System.err.println("toto ="+bp);
180 //                                              int bp5 = bp.pos5;
181 //                                              annot[bp5]= "(";
182 //                                              //annot[bp5]= '(';
183 //                                              System.out.println(annot[bp5]);
184 //                                              //System.out.println("bp5 ="+bp5);
185 //                                              int bp3 = bp.pos3;
186 //                                      
187 //                                              annot[bp3]= ")";
188 //                                              System.out.println(annot[bp3]);
189 //                                              //System.out.println(annot.get(bp3));
190 //                                              
191 //                                              //String pos1 = annot.substring(bp5, bp5+1);
192 //                                              //String pos2 = annot.substring(bp3, bp3+1);
193 //                                              //ann[i] = new Annotation(pos1, "", ' ', 0f);
194 //                                              //ann[i+1] = new Annotation(pos2, "", ' ', 0f);
195 //                                              //System.out.println(pos1);
196 //                                              
197 //                                              //System.out.println("bp3 ="+bp3);
198 //                                              //ModeleBP newStyle = bp.createBPStyle(mb, part);
199 //                                              }       
200 //                                              Annotation[] ann = new Annotation[annot.length+1];
201 //                                              for(int i=0;i<annot.length;i++)
202 //                                              {               
203 //                                                      if(annot[i]==null)
204 //                                                      {
205 //                                                              annot[i]=".";
206 //                                                      }
207 //                                                      
208 //                                                      ann[i] = new Annotation(annot[i], "", ' ', 0f);
209 //                                              //System.out.print(annot[i]);
210 //                                              }
211 //                                              
212 //                                              AlignmentAnnotation toto = new AlignmentAnnotation("Secondary structure","",ann);
213 //                                              this.annotations.addElement(toto);
214                                         
215                                                 // Creating empty structure of suitable size
216 //                                              int[] str = new int[seq.size()];
217 //                                              for (int i=0;i<str.length;i++)
218 //                                              { str[i] = -1; }
219 //                                              Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
220 //                                              for (int i = 0; i < allbpsTmp.size(); i++) {
221 //                                                      RNAMLParser.BPTemp bp = allbpsTmp.get(i);
222 //                                                      //System.err.println("toto ="+bp);
223 //                                                      int bp5 = bp.pos5;
224 //                                                      //System.out.println("bp5 ="+bp5);
225 //                                                      int bp3 = bp.pos3;
226                                                         //System.out.println("bp3 ="+bp3);
227                                                         //ModeleBP newStyle = bp.createBPStyle(mb, part);
228 //                                              }
229                                                 //Annotation ann;
230                                             //ann = new Annotation(bp5, "", ' ', 0f);
231                                 
232                                         //}
233
234                         
235
236 //                              return result;
237           
238 }
239
240           
241           public static String print(SequenceI[] s)
242           {
243             return "not yet implemented";
244           }
245           
246           public String print()
247           {
248                 System.out.print("affiche :");
249             return print(getSeqsAsArray());
250           }
251           
252           /**
253            * make a friendly ID string.
254            * 
255            * @param dataName
256            * @return truncated dataName to after last '/'
257            */
258           private String safeName(String dataName)
259           {
260             int b = 0;
261             while ((b = dataName.indexOf("/")) > -1 && b < dataName.length() )
262             {              
263                 dataName = dataName.substring(b + 1).trim();
264                 
265             }
266             int e = (dataName.length() - dataName.indexOf("."))+1;
267             dataName = dataName.substring(1,e).trim();
268             return dataName;
269           }
270           
271 }
272