JAL-1673 new test to verify transferRESNUMFeatures()
[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(
65                 "Found a duplicate annotation row " + avec[p].label,
66                 avec[p], avec[q]);
67       }
68     }
69   }
70
71   @Test(groups ={ "Functional" })
72   public void checkPDBannotationSource()
73   {
74
75     for (SequenceI asq : al.getSequences())
76     {
77       for (AlignmentAnnotation aa : asq.getAnnotation())
78       {
79
80         System.out.println("CalcId: " + aa.getCalcId());
81         assertTrue(MCview.PDBfile.isCalcIdForFile(aa, pdbId));
82       }
83     }
84   }
85
86   /**
87    * Check sequence features have been added
88    */
89   @Test(groups ={ "Functional" })
90   public void checkPDBSequenceFeatures()
91   {
92     /*
93      * 1GAQ/A
94      */
95     SequenceFeature[] sf = al.getSequenceAt(0).getSequenceFeatures();
96     assertEquals(296, sf.length);
97     assertEquals("RESNUM", sf[0].getType());
98     assertEquals("GLU:  19  1gaqA", sf[0].getDescription());
99     assertEquals("RESNUM", sf[295].getType());
100     assertEquals("TYR: 314  1gaqA", sf[295].getDescription());
101
102     /*
103      * 1GAQ/B
104      */
105     sf = al.getSequenceAt(1).getSequenceFeatures();
106     assertEquals(98, sf.length);
107     assertEquals("RESNUM", sf[0].getType());
108     assertEquals("ALA:   1  1gaqB", sf[0].getDescription());
109     assertEquals("RESNUM", sf[97].getType());
110     assertEquals("ALA:  98  1gaqB", sf[97].getDescription());
111
112     /*
113      * 1GAQ/C
114      */
115     sf = al.getSequenceAt(2).getSequenceFeatures();
116     assertEquals(296, sf.length);
117     assertEquals("RESNUM", sf[0].getType());
118     assertEquals("GLU:  19  1gaqC", sf[0].getDescription());
119     assertEquals("RESNUM", sf[295].getType());
120     assertEquals("TYR: 314  1gaqC", sf[295].getDescription());
121   }
122
123   @Test(groups ={ "Functional" })
124   public void checkAnnotationWiring()
125   {
126     assertTrue(al.getAlignmentAnnotation() != null);
127     // verify that all sequence annotation is doubly referenced
128     for (AlignmentAnnotation aa : al.getAlignmentAnnotation())
129     {
130       if (aa.sequenceRef != null)
131       {
132         assertTrue(al.getSequences().contains(aa.sequenceRef));
133         assertNotNull(aa.sequenceRef.getAnnotation());
134         boolean found = false;
135         for (AlignmentAnnotation sqan : aa.sequenceRef.getAnnotation())
136         {
137           if (sqan == aa)
138           {
139             found = true;
140             break;
141           }
142         }
143         assertTrue(
144                 "Couldn't find sequence associated annotation "
145                         + aa.label
146                         + " on the sequence it is associated with.\nSequence associated editing will fail.",
147                 found);
148       }
149     }
150   }
151
152   /**
153    * @throws java.lang.Exception
154    */
155   @BeforeClass(alwaysRun = true)
156   public static void setUpBeforeClass() throws Exception
157   {
158     jalview.bin.Jalview.main(new String[] { "-props",
159         "test/jalview/io/testProps.jvprops" });
160   }
161
162   /**
163    * @throws java.lang.Exception
164    */
165   @AfterClass
166   public static void tearDownAfterClass() throws Exception
167   {
168     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
169
170   }
171
172   @Test(groups ={ "Functional" })
173   public void testJalviewProjectRelocationAnnotation() throws Exception
174   {
175
176     String inFile = "examples/1gaq.txt";
177     String tfile = File.createTempFile("JalviewTest", ".jvp")
178             .getAbsolutePath();
179     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
180             inFile, FormatAdapter.FILE);
181     assertTrue("Didn't read input file " + inFile, af != null);
182     assertTrue("Failed to store as a project.",
183             af.saveAlignment(tfile, "Jalview"));
184     af.closeMenuItem_actionPerformed(true);
185     af = null;
186     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
187             FormatAdapter.FILE);
188     assertTrue("Failed to import new project", af != null);
189     for (SequenceI asq : af.getViewport().getAlignment().getSequences())
190     {
191       SequenceI sq = asq;
192       while (sq.getDatasetSequence() != null)
193       {
194         sq = sq.getDatasetSequence();
195       }
196       assertNotNull(sq.getAllPDBEntries());
197       assertEquals("Expected only one PDB ID", 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,
211                     pdbentry.getId()))
212             {
213               found = true;
214             }
215           }
216         }
217         if (exists)
218         {
219           assertTrue("Couldn't find any annotation for " + pdbentry.getId()
220                   + " (file handle " + pdbentry.getFile() + ")", found);
221         }
222       }
223     }
224   }
225 }