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