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