JAL-1270 ensure before/after clauses have at least alwaysRun=true in them
[jalview.git] / test / jalview / io / AnnotatedPDBFileInputTest.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.io;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.bin.Cache;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.AlignFrame;
34
35 import java.io.File;
36
37 import org.testng.annotations.AfterClass;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 public class AnnotatedPDBFileInputTest
43 {
44
45   AlignmentI al;
46
47   String pdbId;
48
49   /**
50    * Ensure 'process secondary structure from PDB and add annotations' are set
51    * in preferences, and load PDB example file 1gaq
52    * 
53    * @throws Exception
54    */
55   @BeforeMethod(alwaysRun = true)
56   public void setup() throws Exception
57   {
58     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
59             Boolean.TRUE.toString());
60     Cache.applicationProperties.setProperty("ADD_SS_ANN",
61             Boolean.TRUE.toString());
62     FileLoader loader = new FileLoader(false);
63     AlignFrame af = loader.LoadFileWaitTillLoaded("examples/1gaq.txt",
64             FormatAdapter.FILE);
65     al = af.getViewport().getAlignment();
66     pdbId = al.getSequenceAt(0).getDatasetSequence().getAllPDBEntries()
67             .get(0).getId();
68   }
69
70   @Test(groups = { "Functional" })
71   public void checkNoDuplicates()
72   {
73     // not strictly a requirement, but strange things may happen if multiple
74     // instances of the same annotation are placed in the alignment annotation
75     // vector
76     assertNotNull(al.getAlignmentAnnotation());
77     // verify that all sequence annotation is doubly referenced
78     AlignmentAnnotation[] avec = al.getAlignmentAnnotation();
79     for (int p = 0; p < avec.length; p++)
80     {
81       for (int q = p + 1; q < avec.length; q++)
82       {
83         assertTrue("Found a duplicate annotation row "
84                 + avec[p].label, avec[p] != avec[q]);
85       }
86     }
87   }
88
89   @Test(groups = { "Functional" })
90   public void checkPDBannotationSource()
91   {
92
93     for (SequenceI asq : al.getSequences())
94     {
95       for (AlignmentAnnotation aa : asq.getAnnotation())
96       {
97
98         System.out.println("CalcId: " + aa.getCalcId());
99         assertTrue(MCview.PDBfile.isCalcIdForFile(aa, pdbId));
100       }
101     }
102   }
103
104   /**
105    * Check sequence features have been added
106    */
107   @Test(groups = { "Functional" })
108   public void checkPDBSequenceFeatures()
109   {
110     /*
111      * 1GAQ/A
112      */
113     SequenceFeature[] sf = al.getSequenceAt(0).getSequenceFeatures();
114     assertEquals(296, sf.length);
115     assertEquals("RESNUM", sf[0].getType());
116     assertEquals("GLU:  19  1gaqA", sf[0].getDescription());
117     assertEquals("RESNUM", sf[295].getType());
118     assertEquals("TYR: 314  1gaqA", sf[295].getDescription());
119
120     /*
121      * 1GAQ/B
122      */
123     sf = al.getSequenceAt(1).getSequenceFeatures();
124     assertEquals(98, sf.length);
125     assertEquals("RESNUM", sf[0].getType());
126     assertEquals("ALA:   1  1gaqB", sf[0].getDescription());
127     assertEquals("RESNUM", sf[97].getType());
128     assertEquals("ALA:  98  1gaqB", sf[97].getDescription());
129
130     /*
131      * 1GAQ/C
132      */
133     sf = al.getSequenceAt(2).getSequenceFeatures();
134     assertEquals(296, sf.length);
135     assertEquals("RESNUM", sf[0].getType());
136     assertEquals("GLU:  19  1gaqC", sf[0].getDescription());
137     assertEquals("RESNUM", sf[295].getType());
138     assertEquals("TYR: 314  1gaqC", sf[295].getDescription());
139   }
140
141   @Test(groups = { "Functional" })
142   public void checkAnnotationWiring()
143   {
144     assertTrue(al.getAlignmentAnnotation() != null);
145     // verify that all sequence annotation is doubly referenced
146     for (AlignmentAnnotation aa : al.getAlignmentAnnotation())
147     {
148       if (aa.sequenceRef != null)
149       {
150         assertTrue(al.getSequences().contains(aa.sequenceRef));
151         assertNotNull(aa.sequenceRef.getAnnotation());
152         boolean found = false;
153         for (AlignmentAnnotation sqan : aa.sequenceRef.getAnnotation())
154         {
155           if (sqan == aa)
156           {
157             found = true;
158             break;
159           }
160         }
161         assertTrue(
162                 "Couldn't find sequence associated annotation "
163                         + aa.label
164                         + " on the sequence it is associated with.\nSequence associated editing will fail.",
165                 found);
166       }
167     }
168   }
169
170   /**
171    * @throws java.lang.Exception
172    */
173   @BeforeClass(alwaysRun = true)
174   public static void setUpBeforeClass() throws Exception
175   {
176     jalview.bin.Jalview.main(new String[] { "-props",
177         "test/jalview/io/testProps.jvprops" });
178   }
179
180   /**
181    * @throws java.lang.Exception
182    */
183   @AfterClass(alwaysRun = true)
184   public static void tearDownAfterClass() throws Exception
185   {
186     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
187
188   }
189
190   @Test(groups = { "Functional" })
191   public void testJalviewProjectRelocationAnnotation() throws Exception
192   {
193
194     String inFile = "examples/1gaq.txt";
195     String tfile = File.createTempFile("JalviewTest", ".jvp")
196             .getAbsolutePath();
197     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
198             inFile, FormatAdapter.FILE);
199     assertTrue("Didn't read input file " + inFile, af != null);
200     assertTrue("Failed to store as a project.",
201             af.saveAlignment(tfile, "Jalview"));
202     af.closeMenuItem_actionPerformed(true);
203     af = null;
204     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
205             FormatAdapter.FILE);
206     assertTrue("Failed to import new project", af != null);
207     for (SequenceI asq : af.getViewport().getAlignment().getSequences())
208     {
209       SequenceI sq = asq;
210       while (sq.getDatasetSequence() != null)
211       {
212         sq = sq.getDatasetSequence();
213       }
214       assertNotNull(sq.getAllPDBEntries());
215       assertEquals("Expected only one PDB ID",
216               sq.getAllPDBEntries().size(), 1);
217       for (PDBEntry pdbentry : sq.getAllPDBEntries())
218       {
219         System.err.println("PDB Entry " + pdbentry.getId() + " "
220                 + pdbentry.getFile());
221         boolean exists = false, found = false;
222         for (AlignmentAnnotation ana : sq.getAnnotation())
223         {
224           System.err.println("CalcId " + ana.getCalcId());
225           if (ana.getCalcId() != null
226                   && MCview.PDBfile.isCalcIdHandled(ana.getCalcId()))
227           {
228             exists = true;
229             if (MCview.PDBfile.isCalcIdForFile(ana, pdbentry.getId()))
230             {
231               found = true;
232             }
233           }
234         }
235         if (exists)
236         {
237           assertTrue("Couldn't find any annotation for " + pdbentry.getId()
238                   + " (file handle " + pdbentry.getFile() + ")", found);
239         }
240       }
241     }
242   }
243 }