JAL-1270 set Cache properties in setUp to ensure test success
[jalview.git] / test / jalview / io / AnnotatedPDBFileInputTest.java
1 package jalview.io;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
7
8 import java.io.File;
9
10 import org.junit.AfterClass;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14
15 import jalview.bin.Cache;
16 import jalview.datamodel.AlignmentAnnotation;
17 import jalview.datamodel.AlignmentI;
18 import jalview.datamodel.PDBEntry;
19 import jalview.datamodel.SequenceFeature;
20 import jalview.datamodel.SequenceI;
21 import jalview.gui.AlignFrame;
22 import jalview.gui.Desktop;
23 import jalview.structure.StructureMapping;
24 import jalview.structure.StructureSelectionManager;
25
26 public class AnnotatedPDBFileInputTest
27 {
28
29   AlignmentI al;
30
31   String pdbStr = "examples/1gaq.txt";
32
33   String pdbId;
34
35   @Before
36   public void setup() throws Exception
37   {
38     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
39             Boolean.TRUE.toString());
40     Cache.applicationProperties.setProperty("ADD_SS_ANN",
41             Boolean.TRUE.toString());
42     FileLoader loader = new FileLoader(false);
43     AlignFrame af = loader.LoadFileWaitTillLoaded(pdbStr,
44             FormatAdapter.FILE);
45     al = af.getViewport().getAlignment();
46     pdbId = al.getSequenceAt(0).getDatasetSequence().getPDBId()
47             .get(0).getId();
48   }
49
50   @Test
51   public void checkNoDuplicates()
52   {
53     // not strictly a requirement, but strange things may happen if multiple
54     // instances of the same annotation are placed in the alignment annotation
55     // vector
56     assertNotNull(al.getAlignmentAnnotation());
57     // verify that all sequence annotation is doubly referenced
58     AlignmentAnnotation[] avec = al.getAlignmentAnnotation();
59     for (int p = 0; p < avec.length; p++)
60     {
61       for (int q = p + 1; q < avec.length; q++)
62       {
63         assertNotEquals(
64                 "Found a duplicate annotation row " + avec[p].label,
65                 avec[p], avec[q]);
66       }
67     }
68   }
69
70   @Test
71   public void checkPDBannotationSource()
72   {
73
74     for (SequenceI asq : al.getSequences())
75     {
76       for (AlignmentAnnotation aa : asq.getAnnotation())
77       {
78
79         System.out.println("CalcId: " + aa.getCalcId());
80         assertTrue(MCview.PDBfile.isCalcIdForFile(aa, pdbId));
81       }
82     }
83   }
84
85   /**
86    * Check sequence features have been added
87    */
88   @Test
89   public void checkPDBSequenceFeatures()
90   {
91     StructureSelectionManager ssm = StructureSelectionManager
92             .getStructureSelectionManager(Desktop.instance);
93     StructureMapping[] mappings = ssm.getMapping("1gaq");
94     // suspect we really want to make assertions on sequence features
95     // in these mappings' sequencess
96     /*
97      * 1GAQ/A
98      */
99     SequenceFeature[] sf = al.getSequenceAt(0).getSequenceFeatures();
100     assertEquals(296, sf.length);
101     assertEquals("RESNUM", sf[0].getType());
102     assertEquals("GLU:19 1gaqA", sf[0].getDescription());
103     assertEquals("RESNUM", sf[295].getType());
104     assertEquals("TYR:314 1gaqA", sf[295].getDescription());
105
106     /*
107      * 1GAQ/B
108      */
109     sf = al.getSequenceAt(1).getSequenceFeatures();
110     assertEquals(98, sf.length);
111     assertEquals("RESNUM", sf[0].getType());
112     assertEquals("ALA:1 1gaqB", sf[0].getDescription());
113     assertEquals("RESNUM", sf[97].getType());
114     assertEquals("ALA:98 1gaqB", sf[97].getDescription());
115
116     /*
117      * 1GAQ/C
118      */
119     sf = al.getSequenceAt(2).getSequenceFeatures();
120     assertEquals(296, sf.length);
121     assertEquals("RESNUM", sf[0].getType());
122     assertEquals("GLU:19 1gaqC", sf[0].getDescription());
123     assertEquals("RESNUM", sf[295].getType());
124     assertEquals("TYR:314 1gaqC", sf[295].getDescription());
125   }
126
127   @Test
128   public void checkAnnotationWiring()
129   {
130     assertTrue(al.getAlignmentAnnotation() != null);
131     // verify that all sequence annotation is doubly referenced
132     for (AlignmentAnnotation aa : al.getAlignmentAnnotation())
133     {
134       if (aa.sequenceRef != null)
135       {
136         assertTrue(al.getSequences().contains(aa.sequenceRef));
137         assertNotNull(aa.sequenceRef.getAnnotation());
138         boolean found = false;
139         for (AlignmentAnnotation sqan : aa.sequenceRef.getAnnotation())
140         {
141           if (sqan == aa)
142           {
143             found = true;
144             break;
145           }
146         }
147         assertTrue(
148                 "Couldn't find sequence associated annotation "
149                         + aa.label
150                         + " on the sequence it is associated with.\nSequence associated editing will fail.",
151                 found);
152       }
153     }
154   }
155
156   /**
157    * @throws java.lang.Exception
158    */
159   @BeforeClass
160   public static void setUpBeforeClass() throws Exception
161   {
162     jalview.bin.Jalview.main(new String[]
163     { "-props", "test/src/jalview/io/testProps.jvprops" });
164   }
165
166   /**
167    * @throws java.lang.Exception
168    */
169   @AfterClass
170   public static void tearDownAfterClass() throws Exception
171   {
172     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
173
174   }
175
176   @Test
177   public void testJalviewProjectRelocationAnnotation() throws Exception
178   {
179
180     String inFile = "examples/1gaq.txt";
181     String tfile = File.createTempFile("JalviewTest", ".jvp")
182             .getAbsolutePath();
183     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
184             inFile, FormatAdapter.FILE);
185     assertTrue("Didn't read input file " + inFile, af != null);
186     assertTrue("Failed to store as a project.",
187             af.saveAlignment(tfile, "Jalview"));
188     af.closeMenuItem_actionPerformed(true);
189     af = null;
190     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
191             FormatAdapter.FILE);
192     assertTrue("Failed to import new project", af != null);
193     for (SequenceI asq : af.getViewport().getAlignment().getSequences())
194     {
195       SequenceI sq = asq;
196       while (sq.getDatasetSequence() != null)
197       {
198         sq = sq.getDatasetSequence();
199       }
200       assertNotNull(sq.getPDBId());
201       assertEquals("Expected only one PDB ID", sq.getPDBId().size(), 1);
202       for (PDBEntry pdbentry : sq.getPDBId())
203       {
204         System.err.println("PDB Entry " + pdbentry.getId() + " "
205                 + pdbentry.getFile());
206         boolean exists = false, found = false;
207         for (AlignmentAnnotation ana : sq.getAnnotation())
208         {
209           System.err.println("CalcId " + ana.getCalcId());
210           if (ana.getCalcId() != null
211                   && MCview.PDBfile.isCalcIdHandled(ana.getCalcId()))
212           {
213             exists = true;
214             if (MCview.PDBfile.isCalcIdForFile(ana,
215                     pdbentry.getId()))
216             {
217               found = true;
218             }
219           }
220         }
221         if (exists)
222         {
223           assertTrue("Couldn't find any annotation for " + pdbentry.getId()
224                   + " (file handle " + pdbentry.getFile() + ")", found);
225         }
226       }
227     }
228   }
229 }