d844d626b30484149d04cff10e51d492880b7bbb
[jalview.git] / examples / groovy / JvLoadTester.groovy
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 import jalview.gui.*;
22 import jalview.io.*;
23
24 def class JvLoadTest {
25     FileLoader fl = null;
26     def String safename = null;
27     JvLoadTest(String sname) { 
28         if (!new File(sname).exists() || new File(sname).canWrite())
29             {
30                 safename = sname;
31             } else {
32                 System.err.println("Warning : "+sname+" isn't being used to store temporary files.");
33             }   
34     }
35     def public boolean doTest (file) {
36         fl = new FileLoader(false);
37         System.gc();
38         AlignFrame af = fl.LoadFileWaitTillLoaded(file
39                                                   ,FormatAdapter.FILE);
40         return doTest(af);
41     }
42     def public boolean doSequentialReadTest (file) {
43         return doSequentialReadTest(file, 0);
44     }
45     // Return true if there is more data to read.
46     def public boolean peekFp(FileParse fp) {
47         try { fp.mark(); }  catch (Exception ex) { System.err.println("FAILED mark."+ex); return false; };
48         try {
49           def String nl;
50           for (i in 1..3) { 
51            nl = fp.nextLine();
52            if (nl==null) { return false; }
53            System.out.println(nl +"\\n");
54           }
55         } catch (Exception e) { // end of file.
56                 return false; };
57         try { fp.reset(); } catch (Exception ex) { System.err.println("FAILED rewind."+ex); return false; };
58         return true;
59     }
60     /*
61       Halt after loading the mx'th entry in the filestream
62     */
63     def public boolean doSequentialReadTest (file, int mx) {
64         // first properly open the file
65         //      if (!doTest(file)) { return };
66         def FileParse fp = null;
67         try {
68                 fp = new FileParse(file, AppletFormatAdapter.FILE);
69         } catch (Exception e) { System.err.println("Couldn't open "+file+"\\n"); e.printStackTrace(); return false;};
70         Desktop.instance.closeAll_actionPerformed(null)
71             System.gc();
72         while (fp!=null && fp.isValid() && (mx==0 || mx!=fp.index)) {
73             if (!peekFp(fp)) return false;
74             fl = new FileLoader(false);
75             AlignFrame af = fl.LoadFileWaitTillLoaded(fp, null);
76             System.out.println("FileParse index: "+fp.index);   
77             if (af!=null && (mx==0 || mx!=fp.index))
78                 {       def boolean res = doTest(af);
79                 if (!res)
80                     {
81                         // return false;
82                     }
83                 } else {
84                     // return false;
85                 }
86         }
87         return true;
88     }
89     def public void waitTillSettled(AlignFrame af)
90     {
91         if (af==null) { return; }
92         Thread.sleep(10);
93         while (af.getViewport().updatingConsensus || af.getViewport().updatingConservation) {
94             Thread.sleep(150); // wait until things settle down
95         }
96     }
97     def public boolean doTest(AlignFrame af) {
98         Object pr = af.getViewport().getAlignment().getProperty("AC");
99         if (pr!=null) { System.out.println("Accession = "+(String) pr); }
100         af.selectAllSequenceMenuItem_actionPerformed(null)
101             def boolean done = false;
102         // Just try to save - don\'t mess around with clipboard
103         /*while (!done) {
104           try {
105           af.copy_actionPerformed(null)
106           done = true;
107           } catch (Exception e) {
108           Thread.sleep(100); // wait until clipboard might be available again
109           }
110           }*/
111         if (af==null) { return false; }
112         waitTillSettled(af);
113         // Try and save as a jalview project and reload
114         try {
115             //      af.saveAlignment(safename, "Jalview")
116             new Jalview2XML().SaveState(new java.io.File(safename));
117             Thread.sleep(100);
118                 } catch (Exception ex) { 
119                     System.out.println("Couldn\'t save.");
120                     ex.printStackTrace(System.err);
121                     return false;
122                 }
123         waitTillSettled(af);
124         try {
125             Desktop.instance.closeAll_actionPerformed(null);
126         } catch (Exception ex) {}
127         System.gc();
128         try {
129             af = new FileLoader(false).LoadFileWaitTillLoaded(safename, FormatAdapter.FILE);    
130         } 
131         catch (Exception ex) {
132             System.out.println("Couldn't reload saved file.");
133             System.gc();
134             return false;
135         }
136         waitTillSettled(af);
137
138         Desktop.instance.closeAll_actionPerformed(null);
139
140         // af.paste(true)
141         // af.newView_actionPerformed(null)
142         // af.newView_actionPerformed(null)
143
144         return true;
145     }
146     def public boolean TestForAll(String dir) {
147         println "For directory or file : "+dir;
148         File fd = new File(dir);
149         if (!fd.isDirectory()) { return doSequentialReadTest(dir); }
150         fd.eachFile() { file -> TestForAll(file.getAbsolutePath()) };
151     }
152 }
153 def JvLoadTest newJvLoadTest(String tempFile) {
154         jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
155         System.gc();
156         jalview.gui.Desktop.instance.desktop.showMemoryUsage(true);
157         return new JvLoadTest(tempFile)
158 }