JAL-3332 test to check for duplicate db sources and patch to code. crept in after...
[jalview.git] / test / jalview / ws / SequenceFetcherTest.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 jalview.analysis.CrossRef;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefSource;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.JvOptionPane;
29 import jalview.ws.seqfetcher.ASequenceFetcher;
30 import jalview.ws.seqfetcher.DbSourceProxy;
31
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Vector;
37
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 public class SequenceFetcherTest
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   @Test(groups = "Functional")
53   public void testNoDuplicatesInFetchDbRefs()
54   {
55     Map<String, List<DbSourceProxy>> seen = new HashMap<>();
56     jalview.ws.SequenceFetcher sfetcher = new jalview.ws.SequenceFetcher();
57     String dupes = "";
58     for (String src : sfetcher.getOrderedSupportedSources())
59     {
60       List<DbSourceProxy> seenitem = seen.get(src);
61       if (seenitem != null)
62       {
63         dupes += (dupes.length() > 0 ? "," : "") + src;
64       }
65       else
66       {
67         seen.put(src, sfetcher.getSourceProxy(src));
68       }
69     }
70     if (dupes.length() > 0)
71     {
72       Assert.fail("Duplicate sources : " + dupes);
73     }
74   }
75
76   /**
77    * simple run method to test dbsources.
78    * 
79    * @param argv
80    */
81   public static void main(String[] argv)
82   {
83     // TODO: extracted from SequenceFetcher - convert to network dependent
84     // functional integration test with
85     // assertions
86
87     String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
88             + "With no arguments, all DbSources will be queried with their test Accession number.\n"
89             + "With one argument, the argument will be resolved to one or more db sources and each will be queried with their test accession only.\n"
90             + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";
91
92     if (argv != null && argv.length > 0)
93     {
94       String targs[] = new String[argv.length - 1];
95       System.arraycopy(argv, 1, targs, 0, targs.length);
96       argv = targs;
97     }
98     if (argv != null && argv.length > 0)
99     {
100       List<DbSourceProxy> sps = new SequenceFetcher()
101               .getSourceProxy(argv[0]);
102
103       if (sps != null)
104       {
105         for (DbSourceProxy sp : sps)
106         {
107           AlignmentI al = null;
108           try
109           {
110             testRetrieval(argv[0], sp,
111                     argv.length > 1 ? argv[1] : sp.getTestQuery());
112           } catch (Exception e)
113           {
114             e.printStackTrace();
115             System.err.println("Error when retrieving "
116                     + (argv.length > 1 ? argv[1] : sp.getTestQuery())
117                     + " from " + argv[0] + "\nUsage: " + usage);
118           }
119         }
120         return;
121       }
122       else
123       {
124         System.err.println("Can't resolve " + argv[0]
125                 + " as a database name. Allowed values are :\n"
126                 + new SequenceFetcher().getSupportedDb());
127       }
128       System.out.println(usage);
129       return;
130     }
131     ASequenceFetcher sfetcher = new SequenceFetcher();
132     String[] dbSources = sfetcher.getSupportedDb();
133     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
134     {
135       String db = dbSources[dbsource];
136       // skip me
137       if (db.equals(DBRefSource.PDB))
138       {
139         continue;
140       }
141       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
142       {
143         testRetrieval(db, sp, sp.getTestQuery());
144       }
145     }
146
147   }
148
149   private static void testRetrieval(String db, DbSourceProxy sp,
150           String testQuery)
151   {
152     AlignmentI ds = null;
153     Vector<Object[]> noProds = new Vector<>();
154     System.out.println("Source: " + sp.getDbName() + " (" + db
155             + "): retrieving test:" + sp.getTestQuery());
156     {
157       AlignmentI al = null;
158       try
159       {
160         al = sp.getSequenceRecords(testQuery);
161         if (al != null && al.getHeight() > 0)
162         {
163           boolean dna = sp.isDnaCoding();
164           al.setDataset(null);
165           AlignmentI alds = al.getDataset();
166           // try and find products
167           CrossRef crossRef = new CrossRef(al.getSequencesArray(), alds);
168           List<String> types = crossRef.findXrefSourcesForSequences(dna);
169           if (types != null)
170           {
171             System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));
172             for (String source : types)
173             {
174               System.out.println("Type: " + source);
175               SequenceI[] prod = crossRef.findXrefSequences(source, dna)
176                       .getSequencesArray();
177               System.out.println("Found "
178                       + ((prod == null) ? "no" : "" + prod.length)
179                       + " products");
180               if (prod != null)
181               {
182                 for (int p = 0; p < prod.length; p++)
183                 {
184                   System.out.println("Prod " + p + ": "
185                           + prod[p].getDisplayId(true));
186                 }
187               }
188             }
189           }
190           else
191           {
192             noProds.addElement((dna ? new Object[] { al, al }
193                     : new Object[] { al }));
194           }
195
196         }
197       } catch (Exception ex)
198       {
199         System.out.println("ERROR:Failed to retrieve test query.");
200         ex.printStackTrace(System.out);
201       }
202
203       if (al == null)
204       {
205         System.out.println("ERROR:No alignment retrieved.");
206         StringBuffer raw = sp.getRawRecords();
207         if (raw != null)
208         {
209           System.out.println(raw.toString());
210         }
211         else
212         {
213           System.out.println("ERROR:No Raw results.");
214         }
215       }
216       else
217       {
218         System.out.println("Retrieved " + al.getHeight() + " sequences.");
219         if (ds == null)
220         {
221           ds = al.getDataset();
222         }
223         else
224         {
225           ds.append(al.getDataset());
226           al.setDataset(ds);
227         }
228       }
229       System.out.flush();
230       System.err.flush();
231     }
232     if (noProds.size() > 0)
233     {
234       Enumeration<Object[]> ts = noProds.elements();
235       while (ts.hasMoreElements())
236
237       {
238         Object[] typeSq = ts.nextElement();
239         boolean dna = (typeSq.length > 1);
240         AlignmentI al = (AlignmentI) typeSq[0];
241         System.out.println("Trying getProducts for "
242                 + al.getSequenceAt(0).getDisplayId(true));
243         System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));
244         // have a bash at finding the products amongst all the retrieved
245         // sequences.
246         SequenceI[] seqs = al.getSequencesArray();
247         Alignment prodal = new CrossRef(seqs, ds).findXrefSequences(null,
248                 dna);
249         System.out.println("Found "
250                 + ((prodal == null) ? "no" : "" + prodal.getHeight())
251                 + " products");
252         if (prodal != null)
253         {
254           SequenceI[] prod = prodal.getSequencesArray(); // note
255           // should
256           // test
257           // rather
258           // than
259           // throw
260           // away
261           // codon
262           // mapping
263           // (if
264           // present)
265           for (int p = 0; p < prod.length; p++)
266           {
267             System.out.println("Prod " + p + ": "
268                     + prod[p].getDisplayId(true));
269           }
270         }
271       }
272     }
273   }
274 }