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