JAL-2904 replaced assert for (getEnd()-getStart()+1)==getLength() in testPdb/mmCIFSeq...
[jalview.git] / test / jalview / ws / PDBSequenceFetcherTest.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.ws;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.JvOptionPane;
31 import jalview.structure.StructureImportSettings;
32 import jalview.structure.StructureImportSettings.StructureParser;
33 import jalview.ws.seqfetcher.DbSourceProxy;
34
35 import java.util.Arrays;
36 import java.util.List;
37
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 public class PDBSequenceFetcherTest
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   SequenceFetcher sf;
53
54   @BeforeMethod(alwaysRun = true)
55   public void setUp() throws Exception
56   {
57     Cache.loadProperties("test/jalview/io/testProps.jvprops");
58     // ensure 'add annotation from structure' is selected
59     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
60             Boolean.TRUE.toString());
61     Cache.applicationProperties.setProperty("ADD_SS_ANN",
62             Boolean.TRUE.toString());
63
64     sf = new SequenceFetcher(false);
65   }
66
67   /**
68    * Test that RNA structure can be added by a call to the RNAML service.
69    * 
70    * Note this test depends on http://arn-ibmc.in2p3.fr/api/compute/2d which is
71    * not always reliable.
72    * 
73    * @throws Exception
74    */
75   @Test(groups = { "Network" }, enabled = true)
76   public void testRnaSeqRetrieve() throws Exception
77   {
78     Cache.applicationProperties.setProperty("PDB_DOWNLOAD_FORMAT", "PDB");
79     List<DbSourceProxy> sps = sf.getSourceProxy("PDB");
80     AlignmentI response = sps.get(0).getSequenceRecords("2GIS");
81     assertTrue(response != null);
82     assertTrue(response.getHeight() == 1);
83     for (SequenceI sq : response.getSequences())
84     {
85       assertTrue("No annotation transfered to sequence.",
86               sq.getAnnotation().length > 0);
87       assertTrue("No PDBEntry on sequence.",
88               sq.getAllPDBEntries().size() > 0);
89       assertTrue(
90               "No RNA annotation on sequence, possibly http://arn-ibmc.in2p3.fr/api/compute/2d not available?",
91               sq.getRNA() != null);
92     }
93   }
94
95   @Test(groups = { "Network" }, enabled = true)
96   public void testPdbSeqRetrieve() throws Exception
97   {
98     StructureImportSettings.setDefaultStructureFileFormat("PDB");
99     StructureImportSettings
100             .setDefaultPDBFileParser(StructureParser.JALVIEW_PARSER);
101
102     testRetrieveProteinSeqFromPDB();
103   }
104
105   @Test(groups = { "Network" }, enabled = true)
106   public void testmmCifSeqRetrieve() throws Exception
107   {
108     StructureImportSettings.setDefaultStructureFileFormat("mmCIF");
109     testRetrieveProteinSeqFromPDB();
110   }
111
112   private class TestRetrieveObject
113   {
114     String id;
115
116     int expectedHeight;
117
118     public TestRetrieveObject(String id, int expectedHeight)
119     {
120       super();
121       this.id = id;
122       this.expectedHeight = expectedHeight;
123     }
124
125   }
126
127   private List<TestRetrieveObject> toRetrieve = Arrays.asList(
128           new TestRetrieveObject("1QIP", 4),
129           new TestRetrieveObject("4IM2", 1));
130
131   private void testRetrieveProteinSeqFromPDB() throws Exception
132   {
133     List<DbSourceProxy> sps = sf.getSourceProxy("PDB");
134     for (TestRetrieveObject str : toRetrieve)
135     {
136       AlignmentI response = sps.get(0).getSequenceRecords(str.id);
137       assertTrue("No aligment for " + str.id, response != null);
138       assertEquals(response.getHeight(), str.expectedHeight,
139               "Number of chains for " + str.id);
140       for (SequenceI sq : response.getSequences())
141       {
142         assertTrue("No annotation transfered to sequence " + sq.getName(),
143                 sq.getAnnotation().length > 0);
144         assertTrue("No PDBEntry on sequence " + sq.getName(),
145                 sq.getAllPDBEntries().size() > 0);
146         // FIXME: should test that all residues extracted as sequences from
147         // chains in structure have a mapping to data in the structure
148
149         for (int rs = sq.getStart(); rs < sq.getStart()
150                 + sq.getLength(); rs++)
151         {
152           List<SequenceFeature> sf = sq.findFeatures(rs, rs, "RESNUM");
153           assertEquals(sf.size(), 1);
154         }
155       }
156     }
157   }
158 }