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