JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / ext / jmol / PDBFileWithJmolTest.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.ext.jmol;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.bin.Cache;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileLoader;
33
34 import java.util.Vector;
35
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
38
39 import MCview.PDBfile;
40
41 /**
42  * @author jimp
43  * 
44  */
45 public class PDBFileWithJmolTest
46 {
47   String[] testFile = new String[] { "./examples/1GAQ.txt",
48       "./test/jalview/ext/jmol/1QCF.pdb" }; // ,
49
50   // "./examples/DNMT1_MOUSE.pdb"
51   // };
52
53   @BeforeMethod(alwaysRun = true)
54   public void setUp()
55   {
56     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
57             Boolean.TRUE.toString());
58     Cache.applicationProperties.setProperty("ADD_SS_ANN",
59             Boolean.TRUE.toString());
60   }
61
62   @Test(groups = { "Functional" })
63   public void testAlignmentLoader() throws Exception
64   {
65     for (String f : testFile)
66     {
67       FileLoader fl = new jalview.io.FileLoader(false);
68       AlignFrame af = fl
69               .LoadFileWaitTillLoaded(f, AppletFormatAdapter.FILE);
70       validateSecStrRows(af.getViewport().getAlignment());
71     }
72   }
73
74   @Test(groups = { "Functional" })
75   public void testFileParser() throws Exception
76   {
77     for (String pdbStr : testFile)
78     {
79       PDBfile mctest = new PDBfile(false, false, false, pdbStr,
80               AppletFormatAdapter.FILE);
81       PDBFileWithJmol jtest = new PDBFileWithJmol(pdbStr,
82               jalview.io.AppletFormatAdapter.FILE);
83       Vector<SequenceI> seqs = jtest.getSeqs(), mcseqs = mctest.getSeqs();
84
85       assertTrue(
86               "No sequences extracted from testfile\n"
87                       + (jtest.hasWarningMessage() ? jtest.getWarningMessage()
88                               : "(No warnings raised)"), seqs != null
89                       && seqs.size() > 0);
90       for (SequenceI sq : seqs)
91       {
92         assertEquals("JMol didn't process " + pdbStr
93                 + " to the same sequence as MCView",
94                 sq.getSequenceAsString(), mcseqs.remove(0)
95                         .getSequenceAsString());
96         AlignmentI al = new Alignment(new SequenceI[] { sq });
97         validateSecStrRows(al);
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 }