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