JAL-3746 apply copyright to tests
[jalview.git] / test / jalview / ws / dbsources / RemoteFormatTest.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.dbsources;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotNull;
26 import static org.testng.Assert.assertTrue;
27
28 import jalview.analysis.AlignSeq;
29 import jalview.bin.Cache;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.DBRefSource;
32 import jalview.datamodel.SequenceI;
33 import jalview.ext.ensembl.EnsemblGenomes;
34 import jalview.fts.api.FTSData;
35 import jalview.fts.api.FTSDataColumnI;
36 import jalview.fts.api.FTSRestClientI;
37 import jalview.fts.core.FTSRestRequest;
38 import jalview.fts.core.FTSRestResponse;
39 import jalview.fts.service.uniprot.UniProtFTSRestClient;
40 import jalview.ws.SequenceFetcher;
41 import jalview.ws.seqfetcher.DbSourceProxy;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import org.testng.annotations.BeforeTest;
47 import org.testng.annotations.DataProvider;
48 import org.testng.annotations.Test;
49
50 /**
51  * A class to verify that remotely fetched data has an expected format and can
52  * be successfully processed by Jalview. This is intended as a first line of
53  * defence and early warning of service affecting changes to data fetched
54  * externally.
55  * <p>
56  * This is class is not intended to cover remote services e.g. alignment. Nor
57  * should it duplicate tests already provided by other classes (such as
58  * PDBFTSRestClientTest). Or maybe we will relocate those tests here...
59  */
60 public class RemoteFormatTest
61 {
62   SequenceFetcher sf;
63
64   @BeforeTest(alwaysRun = true)
65   public void setUp() throws Exception
66   {
67     Cache.loadProperties("test/jalview/io/testProps.jvprops");
68     // ensure 'add annotation from structure' is selected
69     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
70             Boolean.TRUE.toString());
71     Cache.applicationProperties.setProperty("ADD_SS_ANN",
72             Boolean.TRUE.toString());
73
74     sf = new SequenceFetcher();
75   }
76
77   @DataProvider(name = "AccessionData")
78   protected Object[][] getAccessions()
79   {
80     return new Object[][] { { DBRefSource.UNIPROT, "P30419" },
81         { DBRefSource.PDB, "1QIP" },
82         { DBRefSource.EMBL, "X53828" },
83         { DBRefSource.EMBLCDS, "CAA37824" },
84         { DBRefSource.ENSEMBL, "ENSG00000157764" },
85         { new EnsemblGenomes().getDbSource(), "DDB_G0283883" },
86         { new PfamFull().getDbSource(), "PF03760" },
87         { new PfamSeed().getDbSource(), "PF03760" },
88         { new RfamSeed().getDbSource(), "RF00014" } };
89   }
90
91   @Test(groups = "Network", dataProvider = "AccessionData")
92   public void testFetchAccession(String dbSource, String accessionId)
93           throws Exception
94   {
95     System.out.println("Fetching " + accessionId + " from " + dbSource);
96     List<DbSourceProxy> sps = sf.getSourceProxy(dbSource);
97     assertFalse(sps.isEmpty());
98     AlignmentI al = sps.get(0).getSequenceRecords(accessionId);
99     assertNotNull(al);
100     assertTrue(al.getHeight() > 0);
101     SequenceI sq = al.getSequenceAt(0);
102     // suppress this check as only Uniprot and PDB acquire PDB refs
103     // assertTrue(sq.getAllPDBEntries().size() > 0, "No PDBEntry on sequence.");
104     assertTrue(sq.getDBRefs().size() > 0, "No DBRef on sequence.");
105     // suppress this test as only certain databases provide 'primary' dbrefs
106     // assertFalse(sq.getPrimaryDBRefs().isEmpty());
107     int length = AlignSeq.extractGaps("-. ", sq.getSequenceAsString())
108             .length();
109     assertEquals(sq.getEnd() - sq.getStart() + 1, length,
110             "Sequence start/end doesn't match number of residues in sequence");
111   }
112
113   @Test(groups = { "Network" })
114   public void testUniprotFreeTextSearch() throws Exception
115   {
116     List<FTSDataColumnI> wantedFields = new ArrayList<>();
117     FTSRestClientI client = UniProtFTSRestClient.getInstance();
118     wantedFields.add(client.getDataColumnByNameOrCode("id"));
119     wantedFields.add(client.getDataColumnByNameOrCode("entry name"));
120     wantedFields.add(client.getDataColumnByNameOrCode("organism"));
121     wantedFields.add(client.getDataColumnByNameOrCode("reviewed")); // Status
122     wantedFields.add(client.getDataColumnByNameOrCode("length"));
123
124     FTSRestRequest request = new FTSRestRequest();
125     request.setAllowEmptySeq(false);
126     request.setResponseSize(100);
127     request.setFieldToSearchBy("Search All");
128     request.setSearchTerm("metanephrops"); // lobster!
129     request.setWantedFields(wantedFields);
130
131     FTSRestResponse response;
132     response = client.executeRequest(request);
133     assertTrue(response.getNumberOfItemsFound() > 20);
134     assertTrue(response.getSearchSummary() != null);
135     assertTrue(response.getSearchSummary().size() > 20);
136     // verify we successfully filtered out the header row (JAL-2485)
137     FTSData header = response.getSearchSummary().iterator().next();
138     assertFalse(
139             header.getSummaryData()[0].toString().equalsIgnoreCase("Entry"),
140             "Failed to filter out summary header row");
141   }
142 }