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