JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / jalview / ext / jmol / PDBFileWithJmolTest.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.ext.jmol;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25 import org.testng.annotations.Test;
26 import org.testng.annotations.BeforeMethod;
27 import java.util.Vector;
28
29 import MCview.PDBfile;
30
31 import jalview.bin.Cache;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.SequenceI;
35 import jalview.gui.AlignFrame;
36 import jalview.io.AppletFormatAdapter;
37 import jalview.io.FileLoader;
38
39 /**
40  * @author jimp
41  * 
42  */
43 public class PDBFileWithJmolTest
44 {
45   String[] testFile = new String[]
46   { "./examples/1GAQ.txt", "./test/jalview/ext/jmol/1QCF.pdb" }; // ,
47
48   // "./examples/DNMT1_MOUSE.pdb"
49   // };
50
51   @BeforeMethod
52   public void setUp()
53   {
54     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
55             Boolean.TRUE.toString());
56     Cache.applicationProperties.setProperty("ADD_SS_ANN",
57             Boolean.TRUE.toString());
58   }
59
60   @Test
61   public void testAlignmentLoader() throws Exception
62   {
63     for (String f : testFile)
64     {
65       FileLoader fl = new jalview.io.FileLoader(false);
66       AlignFrame af = fl
67               .LoadFileWaitTillLoaded(f, AppletFormatAdapter.FILE);
68       validateSecStrRows(af.getViewport().getAlignment());
69     }
70   }
71
72   @Test
73   public void testFileParser() throws Exception
74   {
75     for (String pdbStr : testFile)
76     {
77       PDBfile mctest = new PDBfile(false, false, false, pdbStr,
78               AppletFormatAdapter.FILE);
79       PDBFileWithJmol jtest = new PDBFileWithJmol(pdbStr,
80               jalview.io.AppletFormatAdapter.FILE);
81       Vector<SequenceI> seqs = jtest.getSeqs(), mcseqs = mctest.getSeqs();
82
83       assertTrue(
84               "No sequences extracted from testfile\n"
85                       + (jtest.hasWarningMessage() ? jtest.getWarningMessage()
86                               : "(No warnings raised)"), seqs != null
87                       && seqs.size() > 0);
88       for (SequenceI sq : seqs)
89       {
90         assertEquals("JMol didn't process " + pdbStr
91                 + " to the same sequence as MCView",
92                 sq.getSequenceAsString(), mcseqs.remove(0)
93                         .getSequenceAsString());
94         AlignmentI al = new Alignment(new SequenceI[]
95         { sq });
96         validateSecStrRows(al);
97       }
98     }
99   }
100
101
102   private void validateSecStrRows(AlignmentI al)
103   {
104     if (!al.isNucleotide())
105     {
106       for (SequenceI asq : al.getSequences())
107       {
108         SequenceI sq = asq;
109         boolean hasDs = false;
110         while (sq.getDatasetSequence() != null
111                 && sq.getAnnotation() == null)
112         {
113           sq = sq.getDatasetSequence();
114           hasDs = true;
115         }
116         checkFirstAAIsAssoc(sq);
117         if (hasDs)
118         {
119           // also verify if alignment sequence has annotation on it
120           // that is correctly mapped
121           checkFirstAAIsAssoc(asq);
122         }
123       }
124     }
125   }
126
127   private void checkFirstAAIsAssoc(SequenceI sq)
128   {
129     assertTrue("No secondary structure assigned for protein sequence.",
130             sq.getAnnotation() != null && sq.getAnnotation().length >= 1
131                     && sq.getAnnotation()[0].hasIcons);
132     assertTrue(
133             "Secondary structure not associated for sequence "
134                     + sq.getName(), sq.getAnnotation()[0].sequenceRef == sq);
135   }
136 }