JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / factories / RNAFactory.java
1 package fr.orsay.lri.varna.factories;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.io.Reader;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.EmptyStackException;
12 import java.util.Hashtable;
13 import java.util.List;
14 import java.util.Stack;
15 import java.util.Vector;
16
17 import javax.xml.parsers.SAXParser;
18 import javax.xml.parsers.SAXParserFactory;
19
20 import org.xml.sax.InputSource;
21
22 import fr.orsay.lri.varna.exceptions.ExceptionExportFailed;
23 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
24 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
25 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
26 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
27 import fr.orsay.lri.varna.models.rna.ModeleBP;
28 import fr.orsay.lri.varna.models.rna.ModeleBackboneElement;
29 import fr.orsay.lri.varna.models.rna.ModeleBackboneElement.BackboneType;
30 import fr.orsay.lri.varna.models.rna.ModeleBase;
31 import fr.orsay.lri.varna.models.rna.RNA;
32 import fr.orsay.lri.varna.utils.RNAMLParser;
33
34 /**
35  * BH JAVA FIX: mostly here we are just removing a lot of unnecessary stack traces when doing a drag-drop of a file
36  * BH JAVA FIX: making sure the file reader is closed properly
37  *  
38  */
39 public class RNAFactory
40 {
41
42   public enum RNAFileType
43   {
44     FILE_TYPE_STOCKHOLM, FILE_TYPE_TCOFFEE, FILE_TYPE_BPSEQ, FILE_TYPE_CT, FILE_TYPE_DBN, FILE_TYPE_RNAML, FILE_TYPE_UNKNOWN
45   }
46
47 private static boolean isQuiet;
48
49   public static ArrayList<RNA> loadSecStrRNAML(Reader r)
50       throws ExceptionPermissionDenied, ExceptionLoadingFailed,
51       ExceptionFileFormatOrSyntax
52   {
53
54     ArrayList<RNA> result = new ArrayList<RNA>();
55     try
56     {
57       // System.setProperty("javax.xml.parsers.SAXParserFactory",
58       // "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
59       SAXParserFactory saxFact = javax.xml.parsers.SAXParserFactory
60           .newInstance();
61       saxFact.setValidating(false);
62       saxFact.setXIncludeAware(false);
63       saxFact.setNamespaceAware(false);
64       SAXParser sp = saxFact.newSAXParser();
65       RNAMLParser RNAMLData = new RNAMLParser();
66       sp.parse(new InputSource(r), RNAMLData);
67
68       /*
69        * XMLReader xr = XMLReaderFactory.createXMLReader(); RNAMLParser
70        * RNAMLData = new RNAMLParser(); xr.setContentHandler(RNAMLData);
71        * xr.setErrorHandler(RNAMLData); xr.setEntityResolver(RNAMLData);
72        * xr.parse(new InputSource(r));
73        */
74       for (RNAMLParser.RNATmp rnaTmp : RNAMLData.getMolecules())
75       {
76         RNA current = new RNA();
77         // Retrieving parsed data
78         List<String> seq = rnaTmp.getSequence();
79         // Creating empty structure of suitable size
80         int[] str = new int[seq.size()];
81         for (int i = 0; i < str.length; i++)
82         {
83           str[i] = -1;
84         }
85         current.setRNA(seq, str);
86         Vector<RNAMLParser.BPTemp> allbpsTmp = rnaTmp.getStructure();
87         ArrayList<ModeleBP> allbps = new ArrayList<ModeleBP>();
88         for (int i = 0; i < allbpsTmp.size(); i++)
89         {
90           RNAMLParser.BPTemp bp = allbpsTmp.get(i);
91           // System.err.println(bp);
92           int bp5 = bp.pos5;
93           int bp3 = bp.pos3;
94           ModeleBase mb = current.getBaseAt(bp5);
95           ModeleBase part = current.getBaseAt(bp3);
96           ModeleBP newStyle = bp.createBPStyle(mb, part);
97           allbps.add(newStyle);
98         }
99
100         current.applyBPs(allbps);
101         result.add(current);
102       }
103
104     }
105     catch (IOException ioe)
106     {
107       throw new ExceptionLoadingFailed(
108           "Couldn't load file due to I/O or security policy issues.", "");
109     }
110     catch (Exception ge)
111     {
112         if (!isQuiet) // BH
113       ge.printStackTrace();
114     }
115     return result;
116   }
117
118   public static int[] parseSecStr(String _secStr)
119       throws ExceptionUnmatchedClosingParentheses
120   {
121     Hashtable<Character, Stack<Integer>> stacks = new Hashtable<Character, Stack<Integer>>();
122     int[] result = new int[_secStr.length()];
123     int i = 0;
124     try
125     {
126       for (i = 0; i < _secStr.length(); i++)
127       {
128         result[i] = -1;
129         char c = _secStr.charAt(i);
130         char c2 = Character.toUpperCase(c);
131         if (!stacks.containsKey(c2))
132         {
133           stacks.put(c2, new Stack<Integer>());
134         }
135         switch (c)
136         {
137         case '<':
138         case '{':
139         case '(':
140         case '[':
141           stacks.get(c).push(i);
142           break;
143         case '>':
144         {
145           int j = stacks.get('<').pop();
146           result[i] = j;
147           result[j] = i;
148           break;
149         }
150         case '}':
151         {
152           int j = stacks.get('{').pop();
153           result[i] = j;
154           result[j] = i;
155           break;
156         }
157         case ')':
158         {
159           int j = stacks.get('(').pop();
160           result[i] = j;
161           result[j] = i;
162           break;
163         }
164         case ']':
165         {
166           int j = stacks.get('[').pop();
167           result[i] = j;
168           result[j] = i;
169           break;
170         }
171         case '.':
172           break;
173         default:
174         {
175           if (Character.isLetter(c) && Character.isUpperCase(c))
176           {
177             stacks.get(c).push(i);
178           }
179           else if (Character.isLetter(c) && Character.isLowerCase(c))
180           {
181             int j = stacks.get(Character.toUpperCase(c)).pop();
182             result[i] = j;
183             result[j] = i;
184           }
185         }
186         }
187       }
188     }
189     catch (EmptyStackException e)
190     {
191       throw new ExceptionUnmatchedClosingParentheses(i);
192     }
193     return result;
194   }
195
196   public static ArrayList<RNA> loadSecStrDBN(Reader r)
197       throws ExceptionLoadingFailed, ExceptionPermissionDenied,
198       ExceptionUnmatchedClosingParentheses, ExceptionFileFormatOrSyntax
199   {
200     boolean loadOk = false;
201     ArrayList<RNA> result = new ArrayList<RNA>();
202     RNA current = new RNA();
203     try
204     {
205       BufferedReader fr = new BufferedReader(r);
206       String line = fr.readLine();
207       String title = "";
208       String seqTmp = "";
209       String strTmp = "";
210       while ((line != null) && (strTmp.equals("")))
211       {
212         line = line.trim();
213         if (!line.startsWith(">"))
214         {
215           if (seqTmp.equals(""))
216           {
217             seqTmp = line;
218           }
219           else
220           {
221             strTmp = line;
222           }
223         }
224         else
225         {
226           title = line.substring(1).trim();
227         }
228         line = fr.readLine();
229       }
230       if (strTmp.length() != 0)
231       {
232         current.setRNA(seqTmp, strTmp);
233         current.setName(title);
234         loadOk = true;
235       }
236     }
237     catch (IOException e)
238     {
239       throw new ExceptionLoadingFailed(e.getMessage(), "");
240     }
241     if (loadOk)
242     {
243       result.add(current);
244     }
245     return result;
246   }
247
248   public static ArrayList<RNA> loadSecStr(File f)
249               throws ExceptionFileFormatOrSyntax
250           {
251             try {
252                         return loadSecStr(new BufferedReader(new FileReader(f)), RNAFileType.FILE_TYPE_UNKNOWN);
253                 } catch (FileNotFoundException e) {
254                         throw new ExceptionFileFormatOrSyntax(f.toString());
255                 }
256           }
257
258   public static ArrayList<RNA> loadSecStr(Reader r)
259       throws ExceptionFileFormatOrSyntax
260   {
261     return loadSecStr(new BufferedReader(r), RNAFileType.FILE_TYPE_UNKNOWN);
262   }
263
264         public static ArrayList<RNA> loadSecStr(BufferedReader r, RNAFileType fileType) throws ExceptionFileFormatOrSyntax {
265                 try {
266                         switch (fileType) {
267                         case FILE_TYPE_DBN: {
268                                 try {
269                                         ArrayList<RNA> result = loadSecStrDBN(r);
270                                         if (result.size() != 0)
271                                                 return result;
272                                 } catch (Exception e) {
273                                 }
274                         }
275                                 break;
276                         case FILE_TYPE_CT: {
277                                 try {
278                                         ArrayList<RNA> result = loadSecStrCT(r);
279                                         if (result.size() != 0)
280                                                 return result;
281                                 } catch (Exception e) {
282                                         if (!isQuiet) // BH
283                                                 e.printStackTrace();
284                                 }
285                         }
286                                 break;
287                         case FILE_TYPE_BPSEQ: {
288                                 try {
289                                         ArrayList<RNA> result = loadSecStrBPSEQ(r);
290                                         if (result.size() != 0)
291                                                 return result;
292                                 } catch (Exception e) {
293                                         if (!isQuiet) // BH
294                                                 e.printStackTrace();
295                                 }
296                         }
297                                 break;
298                         case FILE_TYPE_TCOFFEE: {
299                                 try {
300                                         ArrayList<RNA> result = loadSecStrTCoffee(r);
301                                         if (result.size() != 0)
302                                                 return result;
303                                 } catch (Exception e) {
304                                         if (!isQuiet) // BH
305                                                 e.printStackTrace();
306                                 }
307                         }
308                                 break;
309                         case FILE_TYPE_STOCKHOLM: {
310                                 try {
311                                         ArrayList<RNA> result = loadSecStrStockholm(r);
312                                         if (result.size() != 0)
313                                                 return result;
314                                 } catch (Exception e) {
315                                         if (!isQuiet) // BH
316                                                 e.printStackTrace();
317                                 }
318                         }
319                                 break;
320                         case FILE_TYPE_RNAML: {
321                                 try {
322                                         ArrayList<RNA> result = loadSecStrRNAML(r);
323                                         if (result.size() != 0)
324                                                 return result;
325                                 } catch (Exception e) {
326                                         if (!isQuiet) // BH
327                                                 e.printStackTrace();
328                                 }
329                         }
330                                 break;
331
332                         case FILE_TYPE_UNKNOWN: {
333                                 try {
334                                         r.mark(1000000);
335                                         RNAFactory.RNAFileType[] types = RNAFactory.RNAFileType.values();
336                                         isQuiet = true; // BH to not report errors when
337                                                                         // drag-dropping
338                                         ArrayList<RNA> result = null;
339                                         RNAFactory.RNAFileType t = null;
340                                         for (int i = 0; i < types.length; i++) {
341                                                 r.reset();
342                                                 t = types[i];
343                                                 if (t != RNAFactory.RNAFileType.FILE_TYPE_UNKNOWN) {
344                                                         try {
345                                                                 result = loadSecStr(r, t);
346                                                                 if (result.size() != 0) {
347                                                                         break;
348                                                                 }
349                                                         } catch (Exception e) {
350                                                                 if (!isQuiet) // BH
351                                                                         System.err.println(e.toString());
352                                                         }
353                                                 }
354                                         }
355                                         System.out.println(t); // BH
356                                         isQuiet = false; // BH
357                                         return result;
358                                 } catch (IOException e2) {
359                                         e2.printStackTrace();
360                                 }
361                         }
362                         }
363                         throw new ExceptionFileFormatOrSyntax("Couldn't parse this file as " + fileType + ".");
364                 } finally { // BH !!
365                         try {
366                                 if (!isQuiet)
367                                         r.close();
368                         } catch (IOException e) {
369                                 // ignore
370                         }
371                 }
372         }
373
374   public static RNAFileType guessFileTypeFromExtension(String path)
375   {
376     if (path.toLowerCase().endsWith("ml"))
377     {
378       return RNAFileType.FILE_TYPE_RNAML;
379     }
380     else if (path.toLowerCase().endsWith("dbn")
381         || path.toLowerCase().endsWith("faa"))
382     {
383       return RNAFileType.FILE_TYPE_DBN;
384     }
385     else if (path.toLowerCase().endsWith("ct"))
386     {
387       return RNAFileType.FILE_TYPE_CT;
388     }
389     else if (path.toLowerCase().endsWith("bpseq"))
390     {
391       return RNAFileType.FILE_TYPE_BPSEQ;
392     }
393     else if (path.toLowerCase().endsWith("rfold"))
394     {
395       return RNAFileType.FILE_TYPE_TCOFFEE;
396     }
397     else if (path.toLowerCase().endsWith("stockholm")
398         || path.toLowerCase().endsWith("stk"))
399     {
400       return RNAFileType.FILE_TYPE_STOCKHOLM;
401     }
402
403     return RNAFileType.FILE_TYPE_UNKNOWN;
404
405   }
406
407   public static ArrayList<RNA> loadSecStr(String path)
408       throws ExceptionExportFailed, ExceptionPermissionDenied,
409       ExceptionLoadingFailed, ExceptionFileFormatOrSyntax,
410       ExceptionUnmatchedClosingParentheses, FileNotFoundException
411   {
412     FileReader fr = null;
413     try
414     {
415       fr = new FileReader(path);
416       RNAFileType type = guessFileTypeFromExtension(path);
417       return loadSecStr(new BufferedReader(fr), type);
418     }
419     catch (ExceptionFileFormatOrSyntax e)
420     {
421       if (fr != null)
422         try
423         {
424           fr.close();
425         }
426         catch (IOException e2)
427         {
428         }
429       e.setPath(path);
430       throw e;
431     }
432   }
433
434   public static ArrayList<RNA> loadSecStrStockholm(BufferedReader r)
435       throws IOException, ExceptionUnmatchedClosingParentheses
436   {
437     RNAAlignment a = StockholmIO.readAlignement(r);
438     return a.getRNAs();
439   }
440
441   public static ArrayList<RNA> loadSecStrBPSEQ(Reader r)
442       throws ExceptionPermissionDenied, ExceptionLoadingFailed,
443       ExceptionFileFormatOrSyntax
444   {
445     boolean loadOk = false;
446     ArrayList<RNA> result = new ArrayList<RNA>();
447     RNA current = new RNA();
448     try
449     {
450       BufferedReader fr = new BufferedReader(r);
451       String line = fr.readLine();
452       ArrayList<String> seqTmp = new ArrayList<String>();
453       Hashtable<Integer, Vector<Integer>> strTmp = new Hashtable<Integer, Vector<Integer>>();
454
455       int bpFrom;
456       String base;
457       int bpTo;
458       int minIndex = -1;
459       boolean noWarningYet = true;
460       String title = "";
461       String id = "";
462       String filenameStr = "Filename:";
463       String organismStr = "Organism:";
464       String ANStr = "Accession Number:";
465       while (line != null)
466       {
467         line = line.trim();
468         String[] tokens = line.split("\\s+");
469         ArrayList<Integer> numbers  = new ArrayList<Integer>(); 
470         Hashtable<Integer,Integer> numberToIndex  = new Hashtable<Integer,Integer>(); 
471         if ((tokens.length >= 3) && !tokens[0].contains("#")
472             && !line.startsWith("Organism:") && !line.startsWith("Citation")
473             && !line.startsWith("Filename:")
474             && !line.startsWith("Accession Number:"))
475         {
476           base = tokens[1];
477           seqTmp.add(base);
478           bpFrom = (Integer.parseInt(tokens[0]));
479           numbers.add(bpFrom);
480           if (minIndex < 0)
481             minIndex = bpFrom;
482
483           if (seqTmp.size() < (bpFrom - minIndex + 1))
484           {
485             if (noWarningYet)
486             {
487               noWarningYet = false;
488               /*
489                * warningEmition( "Discontinuity detected between nucleotides " +
490                * (seqTmp.size()) + " and " + (bpFrom + 1) +
491                * "!\nFilling in missing portions with unpaired unknown 'X' nucleotides ..."
492                * );
493                */
494             }
495             while (seqTmp.size() < (bpFrom - minIndex + 1))
496             {
497               // System.err.println(".");
498               seqTmp.add("X");
499             }
500           }
501           for (int i = 2; i < tokens.length; i++)
502           {
503             bpTo = (Integer.parseInt(tokens[i]));
504             if ((bpTo != 0) || (i != tokens.length - 1))
505             {
506               if (!strTmp.containsKey(bpFrom))
507                 strTmp.put(bpFrom, new Vector<Integer>());
508               strTmp.get(bpFrom).add(bpTo);
509             }
510           }
511         }
512         else if (tokens[0].startsWith("#"))
513         {
514           int occur = line.indexOf("#");
515           String tmp = line.substring(occur + 1);
516           title += tmp.trim() + " ";
517         }
518         else if (tokens[0].startsWith(filenameStr))
519         {
520           int occur = line.indexOf(filenameStr);
521           String tmp = line.substring(occur + filenameStr.length());
522           title += tmp.trim();
523         }
524         else if (tokens[0].startsWith(organismStr))
525         {
526           int occur = line.indexOf(organismStr);
527           String tmp = line.substring(occur + organismStr.length());
528           if (title.length() != 0)
529           {
530             title = "/" + title;
531           }
532           title = tmp.trim() + title;
533         }
534         else if (line.contains(ANStr))
535         {
536           int occur = line.indexOf(ANStr);
537           String tmp = line.substring(occur + ANStr.length());
538           id = tmp.trim();
539         }
540         line = fr.readLine();
541       }
542       if (strTmp.size() != 0)
543       {
544         ArrayList<String> seq = seqTmp;
545         int[] str = new int[seq.size()];
546         for (int i = 0; i < seq.size(); i++)
547         {
548           str[i] = -1;
549         }
550         current.setRNA(seq, str, minIndex);
551         ArrayList<ModeleBP> allbps = new ArrayList<ModeleBP>();
552         for (int i : strTmp.keySet())
553         {
554           for (int j : strTmp.get(i))
555           {
556                   if (i<=j)
557                   {
558                     ModeleBase mb = current.getBaseAt(i - minIndex);
559                     ModeleBase part = current.getBaseAt(j - minIndex);
560                     ModeleBP newStyle = new ModeleBP(mb, part);
561                     allbps.add(newStyle);
562                   }
563           }
564         }
565         current.applyBPs(allbps);
566         current.setName(title);
567         current.setID(id);
568         loadOk = true;
569       }
570     }
571     catch (NumberFormatException e)
572     {
573         if (!isQuiet) // BH SwingJS
574       e.printStackTrace();
575     }
576     catch (IOException e)
577     {
578       // TODO Auto-generated catch block
579       e.printStackTrace();
580     }
581     catch (Exception e)
582     {
583       throw new ExceptionLoadingFailed(e.getMessage(), "");
584     }
585     if (loadOk)
586       result.add(current);
587     return result;
588   }
589
590   public static ArrayList<RNA> loadSecStrTCoffee(Reader r)
591       throws ExceptionPermissionDenied, ExceptionLoadingFailed,
592       ExceptionFileFormatOrSyntax
593   {
594     boolean loadOk = false;
595     ArrayList<RNA> result = new ArrayList<RNA>();
596     try
597     {
598       BufferedReader fr = new BufferedReader(r);
599       String line = fr.readLine();
600       ArrayList<String> seqs = new ArrayList<String>();
601       ArrayList<String> ids = new ArrayList<String>();
602       int numSeqs = -1;
603       int currSeq = -1;
604       RNA current = null;
605       while (line != null)
606       {
607         if (!line.startsWith("!"))
608         {
609           String[] tokens = line.split("\\s+");
610           // This may indicate new secondary structure
611           if (line.startsWith("#"))
612           {
613             currSeq = Integer.parseInt(tokens[0].substring(1));
614             int currSeq2 = Integer.parseInt(tokens[1]);
615             // For TCoffee, a sec str is a matching between a seq and itself
616             // => Disregard any alignment by filtering on the equality of sequence indices.
617             if (currSeq == currSeq2)
618             {
619               current = new RNA();
620               current.setName(ids.get(currSeq - 1));
621               current.setSequence(seqs.get(currSeq - 1));
622               result.add(current);
623             }
624             else
625             {
626               current = null;
627             }
628           }
629           // Beginning of the file... 
630           else if (current == null)
631           {
632             //... either this is the number of sequences...
633             if (numSeqs < 0)
634             {
635               numSeqs = Integer.parseInt(tokens[0]);
636             }
637             //... or this is a sequence definition...
638             else
639             {
640               String id = tokens[0];
641               String seq = tokens[2];
642               seqs.add(seq);
643               ids.add(id);
644             }
645           }
646           //Otherwise, this is a base-pair definition, related to the currently selected sequence
647           else if (tokens.length == 3)
648           {
649             int from = Integer.parseInt(tokens[0]) - 1;
650             int to = Integer.parseInt(tokens[1]) - 1;
651             current.addBP(from, to);
652           }
653         }
654         line = fr.readLine();
655       }
656       loadOk = true;
657     }
658     catch (NumberFormatException e)
659     {
660         if (!isQuiet) // BH SwingJS
661       e.printStackTrace();
662     }
663     catch (IOException e)
664     {
665         if (!isQuiet) // BH SwingJS
666                 e.printStackTrace();
667     }
668     if (!loadOk)
669     {
670       throw new ExceptionLoadingFailed("Parse Error", "");
671     }
672     return result;
673   }
674
675   public static ArrayList<RNA> loadSecStrCT(Reader r)
676       throws ExceptionPermissionDenied, ExceptionLoadingFailed,
677       ExceptionFileFormatOrSyntax
678   {
679     boolean loadOk = false;
680     ArrayList<RNA> result = new ArrayList<RNA>();
681     RNA current = new RNA();
682     try
683     {
684       BufferedReader fr = new BufferedReader(r);
685       String line = fr.readLine();
686       ArrayList<String> seq = new ArrayList<String>();
687       ArrayList<String> lbls = new ArrayList<String>();
688       Vector<Integer> strTmp = new Vector<Integer>();
689       Vector<Integer> newStrands = new Vector<Integer>();
690       int bpFrom;
691       String base;
692       String lbl;
693       int bpTo;
694       boolean noWarningYet = true;
695       int minIndex = -1;
696       String title = "";
697       while (line != null)
698       {
699         line = line.trim();
700         String[] tokens = line.split("\\s+");
701         if (tokens.length >= 6)
702         {
703           try
704           {
705             bpFrom = (Integer.parseInt(tokens[0]));
706             bpTo = (Integer.parseInt(tokens[4]));
707             if (minIndex == -1)
708               minIndex = bpFrom;
709             bpFrom -= minIndex;
710             if (bpTo != 0)
711               bpTo -= minIndex;
712             else
713               bpTo = -1;
714             base = tokens[1];
715             lbl = tokens[5];
716             int before = Integer.parseInt(tokens[2]);
717             int after = Integer.parseInt(tokens[3]);
718             
719             if (before==0 && !seq.isEmpty())
720             {
721                 newStrands.add(strTmp.size()-1);
722             }
723             if (bpFrom != seq.size())
724             {
725               if (noWarningYet)
726               {
727                 noWarningYet = false;
728                 /*
729                  * warningEmition( "Discontinuity detected between nucleotides "
730                  * + (seq.size()) + " and " + (bpFrom + 1) +
731                  * "!\nFilling in missing portions with unpaired unknown 'X' nucleotides ..."
732                  * );
733                  */
734               }
735               while (bpFrom > seq.size())
736               {
737                 seq.add("X");
738                 strTmp.add(-1);
739                 lbls.add("");
740               }
741             }
742             seq.add(base);
743             strTmp.add(bpTo);
744             lbls.add(lbl);
745           }
746           catch (NumberFormatException e)
747           {
748                   if (strTmp.size()!=0)
749                           e.printStackTrace();
750           }
751         }
752         if ((line.contains("ENERGY = ")) || line.contains("dG = "))
753         {
754           String[] ntokens = line.split("\\s+");
755           if (ntokens.length >= 4)
756           {
757             String energy = ntokens[3];
758             for (int i = 4; i < ntokens.length; i++)
759             {
760               title += ntokens[i] + " ";
761             }
762             title += "(E=" + energy + " kcal/mol)";
763           }
764         }
765         line = fr.readLine();
766       }
767       if (strTmp.size() != 0)
768       {
769         int[] str = new int[strTmp.size()];
770         for (int i = 0; i < strTmp.size(); i++)
771         {
772           str[i] = strTmp.elementAt(i).intValue();
773         }
774         current.setRNA(seq, str, minIndex);
775         current.setName(title);
776         for (int i = 0; i < current.getSize(); i++)
777         {
778           current.getBaseAt(i).setLabel(lbls.get(i));
779         }
780         for (int i : newStrands)
781         {
782                 current.getBackbone().addElement(new ModeleBackboneElement(i,BackboneType.DISCONTINUOUS_TYPE));
783         }
784
785         
786         loadOk = true;
787       }
788     }
789     catch (IOException e)
790     {
791         e.printStackTrace();
792       throw new ExceptionLoadingFailed(e.getMessage(), "");
793     }
794     catch (NumberFormatException e)
795     {
796         if (!isQuiet) // BH SwingJS
797         e.printStackTrace();
798       throw new ExceptionFileFormatOrSyntax(e.getMessage(), "");
799     }
800     if (loadOk)
801       result.add(current);
802     return result;
803   }
804
805 }