temp push
[jalview.git] / test / jalview / ext / jmol / JmolViewerTest.java
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 package jalview.ext.jmol;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.structures.JalviewStructureDisplayI;
28 import jalview.bin.Cache;
29 import jalview.bin.Jalview;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.SearchResultsI;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.AlignFrame;
34 import jalview.gui.JvOptionPane;
35 import jalview.gui.Preferences;
36 import jalview.gui.StructureViewer;
37 import jalview.gui.StructureViewer.ViewerType;
38 import jalview.io.DataSourceType;
39 import jalview.io.FileFormat;
40 import jalview.io.FileLoader;
41
42 import java.lang.reflect.InvocationTargetException;
43
44 import org.testng.annotations.AfterClass;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.Test;
47
48 @Test(singleThreaded = true)
49 public class JmolViewerTest
50 {
51
52   @BeforeClass(alwaysRun = true)
53   public void setUpJvOptionPane()
54   {
55     JvOptionPane.setInteractiveMode(false);
56     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57   }
58
59   /**
60    * @throws java.lang.Exception
61    */
62   @BeforeClass(alwaysRun = true)
63   public static void setUpBeforeClass() throws Exception
64   {
65     Jalview.main(
66             new String[]
67             { "-noquestionnaire", "-nonews", "-props",
68                 "test/jalview/ext/rbvi/chimera/testProps.jvprops" });
69   }
70
71   /**
72    * @throws java.lang.Exception
73    */
74   @AfterClass(alwaysRun = true)
75   public static void tearDownAfterClass() throws Exception
76   {
77     jalview.gui.Desktop.getInstance().closeAll_actionPerformed(null);
78   }
79
80   @Test(groups = { "Functional" })
81   public void testSingleSeqViewJMol()
82   {
83     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
84             ViewerType.JMOL.name());
85     String inFile = "examples/1gaq.txt";
86     AlignFrame af = new jalview.io.FileLoader()
87             .LoadFileWaitTillLoaded(inFile, DataSourceType.FILE);
88     assertTrue("Didn't read input file " + inFile, af != null);
89     for (SequenceI sq : af.getViewport().getAlignment().getSequences())
90     {
91       SequenceI dsq = sq.getDatasetSequence();
92       while (dsq.getDatasetSequence() != null)
93       {
94         dsq = dsq.getDatasetSequence();
95       }
96       if (dsq.getAllPDBEntries() != null
97               && dsq.getAllPDBEntries().size() > 0)
98       {
99         for (int q = 0; q < dsq.getAllPDBEntries().size(); q++)
100         {
101           final StructureViewer structureViewer = new StructureViewer(
102                   af.getViewport().getStructureSelectionManager());
103           structureViewer.setViewerType(ViewerType.JMOL);
104           JalviewStructureDisplayI jmolViewer = structureViewer
105                   .viewStructures(dsq.getAllPDBEntries().elementAt(q),
106                           new SequenceI[]
107                           { sq }, af.getCurrentView().getAlignPanel());
108           /*
109            * Wait for viewer load thread to complete
110            */
111           try
112           {
113             while (!jmolViewer.getBinding().isFinishedInit())
114             {
115               Thread.sleep(500);
116             }
117           } catch (InterruptedException e)
118           {
119           }
120
121           jmolViewer.closeViewer(true);
122           // todo: break here means only once through this loop?
123           break;
124         }
125         break;
126       }
127     }
128   }
129
130   @Test(groups = { "Functional" })
131   public void testAddStrToSingleSeqViewJMol()
132           throws InvocationTargetException, InterruptedException
133   {
134     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
135             ViewerType.JMOL.name());
136     String inFile = "examples/1gaq.txt";
137     AlignFrame af = new jalview.io.FileLoader(true)
138             .LoadFileWaitTillLoaded(inFile, DataSourceType.FILE);
139     assertTrue("Didn't read input file " + inFile, af != null);
140     // show a structure for 4th Sequence
141     SequenceI sq1 = af.getViewport().getAlignment().getSequences().get(0);
142     final StructureViewer structureViewer = new StructureViewer(
143             af.getViewport().getStructureSelectionManager());
144     structureViewer.setViewerType(ViewerType.JMOL);
145     JalviewStructureDisplayI jmolViewer = structureViewer.viewStructures(
146             sq1.getDatasetSequence().getAllPDBEntries().elementAt(0),
147             new SequenceI[]
148             { sq1 }, af.getCurrentView().getAlignPanel());
149     /*
150      * Wait for viewer load thread to complete
151      */
152     try
153     {
154       while (!jmolViewer.getBinding().isFinishedInit())
155       {
156         Thread.sleep(500);
157       }
158     } catch (InterruptedException e)
159     {
160     }
161
162     assertTrue(jmolViewer.isVisible());
163
164     // add another pdb file and add it to view
165     final String _inFile = "examples/3W5V.pdb";
166     inFile = _inFile;
167     FileLoader fl = new FileLoader();
168     fl.LoadFile(af.getCurrentView(), _inFile, DataSourceType.FILE,
169             FileFormat.PDB);
170     try
171     {
172       int time = 0;
173       do
174       {
175         Thread.sleep(50); // hope we can avoid race condition
176
177       } while (++time < 30
178               && af.getViewport().getAlignment().getHeight() == 3);
179     } catch (Exception q)
180     {
181     }
182     ;
183     assertTrue("Didn't paste additional structure" + inFile,
184             af.getViewport().getAlignment().getHeight() > 3);
185     SequenceI sq2 = af.getViewport().getAlignment().getSequenceAt(3);
186     PDBEntry pdbe = sq2.getDatasetSequence().getAllPDBEntries().get(0);
187     assertTrue(pdbe.getFile().contains(inFile));
188     structureViewer.viewStructures(pdbe, new SequenceI[] { sq2 },
189             af.alignPanel);
190     /*
191      * Wait for viewer load thread to complete
192      */
193     try
194     {
195       while (structureViewer.isBusy())
196       {
197         Thread.sleep(500);
198       }
199     } catch (InterruptedException e)
200     {
201     }
202     assertEquals(jmolViewer.getBinding().getPdbCount(), 2);
203     String mouseOverTest = "[GLY]293:A.CA/2.1 #2164";
204     ((JalviewJmolBinding) jmolViewer.getBinding()).mouseOverStructure(2164,
205             mouseOverTest);
206     SearchResultsI highlight = af.alignPanel.getSeqPanel()
207             .getLastSearchResults();
208     assertNotNull("Didn't find highlight from second structure mouseover",
209             highlight.getResults(sq2, sq2.getStart(), sq2.getEnd()));
210   }
211
212 }