JAL-3438 spotless for 2.11.2.0
[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.Assert;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.Test;
42
43 public class PDBSequenceFetcherTest
44 {
45
46   @BeforeClass(alwaysRun = true)
47   public void setUpJvOptionPane()
48   {
49     JvOptionPane.setInteractiveMode(false);
50     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51   }
52
53   SequenceFetcher sf;
54
55   @BeforeMethod(alwaysRun = true)
56   public void setUp() throws Exception
57   {
58     Cache.loadProperties("test/jalview/io/testProps.jvprops");
59     // ensure 'add annotation from structure' is selected
60     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
61             Boolean.TRUE.toString());
62     Cache.applicationProperties.setProperty("ADD_SS_ANN",
63             Boolean.TRUE.toString());
64
65     sf = new SequenceFetcher();
66   }
67
68   /**
69    * Test that RNA structure can be added by a call to the RNAML service.
70    * 
71    * Note this test depends on http://arn-ibmc.in2p3.fr/api/compute/2d which is
72    * not always reliable.
73    * 
74    * @throws Exception
75    */
76   @Test(groups = { "Network" }, enabled = true)
77   public void testRnaSeqRetrieve() throws Exception
78   {
79     Cache.applicationProperties.setProperty("PDB_DOWNLOAD_FORMAT", "PDB");
80     List<DbSourceProxy> sps = sf.getSourceProxy("PDB");
81     AlignmentI response = sps.get(0).getSequenceRecords("2GIS");
82     assertTrue(response != null);
83     assertTrue(response.getHeight() == 1);
84     for (SequenceI sq : response.getSequences())
85     {
86       assertTrue("No annotation transfered to sequence.",
87               sq.getAnnotation().length > 0);
88       assertTrue("No PDBEntry on sequence.",
89               sq.getAllPDBEntries().size() > 0);
90       assertTrue(
91               "No RNA annotation on sequence, possibly http://arn-ibmc.in2p3.fr/api/compute/2d not available?",
92               sq.getRNA() != null);
93     }
94   }
95
96   @Test(groups = { "Network" }, enabled = true)
97   public void testPdbSeqRetrieve() throws Exception
98   {
99     StructureImportSettings.setDefaultStructureFileFormat("PDB");
100     StructureImportSettings
101             .setDefaultPDBFileParser(StructureParser.JALVIEW_PARSER);
102
103     testRetrieveProteinSeqFromPDB();
104   }
105
106   @Test(groups = { "Network" }, enabled = true)
107   public void testmmCifSeqRetrieve() throws Exception
108   {
109     StructureImportSettings.setDefaultStructureFileFormat("mmCIF");
110     testRetrieveProteinSeqFromPDB();
111   }
112
113   private class TestRetrieveObject
114   {
115     String id;
116
117     int expectedHeight;
118
119     public TestRetrieveObject(String id, int expectedHeight)
120     {
121       super();
122       this.id = id;
123       this.expectedHeight = expectedHeight;
124     }
125
126   }
127
128   private List<TestRetrieveObject> toRetrieve = Arrays.asList(
129           new TestRetrieveObject("1QIP", 4),
130           new TestRetrieveObject("4IM2", 1));
131
132   private void testRetrieveProteinSeqFromPDB() throws Exception
133   {
134     List<DbSourceProxy> sps = sf.getSourceProxy("PDB");
135     StringBuilder errors = new StringBuilder();
136     for (TestRetrieveObject str : toRetrieve)
137     {
138       AlignmentI response = sps.get(0).getSequenceRecords(str.id);
139       assertTrue("No aligment for " + str.id, response != null);
140       assertEquals(response.getHeight(), str.expectedHeight,
141               "Number of chains for " + str.id);
142       for (SequenceI sq : response.getSequences())
143       {
144         assertTrue("No annotation transfered to sequence " + sq.getName(),
145                 sq.getAnnotation().length > 0);
146         assertTrue("No PDBEntry on sequence " + sq.getName(),
147                 sq.getAllPDBEntries().size() > 0);
148         // FIXME: should test that all residues extracted as sequences from
149         // chains in structure have a mapping to data in the structure
150         List<SequenceFeature> prev = null;
151         int lastp = -1;
152         for (int col = 1; col <= sq.getLength(); col++)
153         {
154           List<SequenceFeature> sf = sq.findFeatures(col, col, "RESNUM");
155           if (sf.size() != 1)
156           {
157             errors.append(str.id + ": "
158                     + "Expected one feature at column (position): "
159                     + (col - 1) + " (" + sq.findPosition(col - 1) + ")"
160                     + ": saw " + sf.size());
161             errors.append("\n");
162             if (prev != null)
163             {
164               errors.append("Last Feature was at position " + lastp + ": "
165                       + prev.get(0).toString());
166               errors.append("\n");
167             }
168           }
169           else
170           {
171             prev = sf;
172             lastp = sq.findPosition(col - 1);
173           }
174         }
175       }
176     }
177     if (errors.length() > 0)
178     {
179       Assert.fail(errors.toString());
180     }
181   }
182 }