JAL-3210 Merge branch 'develop' into trialMerge
[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    * @j2sIgnore
81    */
82   public static void main(String[] argv)
83   {
84     // TODO: extracted from SequenceFetcher - convert to network dependent
85     // functional integration test with
86     // assertions
87
88     String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
89             + "With no arguments, all DbSources will be queried with their test Accession number.\n"
90             + "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"
91             + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.";
92
93     if (argv != null && argv.length > 0)
94     {
95       String targs[] = new String[argv.length - 1];
96       System.arraycopy(argv, 1, targs, 0, targs.length);
97       argv = targs;
98     }
99     if (argv != null && argv.length > 0)
100     {
101       List<DbSourceProxy> sps = new SequenceFetcher()
102               .getSourceProxy(argv[0]);
103
104       if (sps != null)
105       {
106         for (DbSourceProxy sp : sps)
107         {
108           AlignmentI al = null;
109           try
110           {
111             testRetrieval(argv[0], sp,
112                     argv.length > 1 ? argv[1] : sp.getTestQuery());
113           } catch (Exception e)
114           {
115             e.printStackTrace();
116             System.err.println("Error when retrieving "
117                     + (argv.length > 1 ? argv[1] : sp.getTestQuery())
118                     + " from " + argv[0] + "\nUsage: " + usage);
119           }
120         }
121         return;
122       }
123       else
124       {
125         System.err.println("Can't resolve " + argv[0]
126                 + " as a database name. Allowed values are :\n"
127                 + new SequenceFetcher().getSupportedDb());
128       }
129       System.out.println(usage);
130       return;
131     }
132     ASequenceFetcher sfetcher = new SequenceFetcher();
133     String[] dbSources = sfetcher.getSupportedDb();
134     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
135     {
136       String db = dbSources[dbsource];
137       // skip me
138       if (db.equals(DBRefSource.PDB))
139       {
140         continue;
141       }
142       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
143       {
144         testRetrieval(db, sp, sp.getTestQuery());
145       }
146     }
147
148   }
149
150   private static void testRetrieval(String db, DbSourceProxy sp,
151           String testQuery)
152   {
153     AlignmentI ds = null;
154     Vector<Object[]> noProds = new Vector<>();
155     System.out.println("Source: " + sp.getDbName() + " (" + db
156             + "): retrieving test:" + sp.getTestQuery());
157     {
158       AlignmentI al = null;
159       try
160       {
161         al = sp.getSequenceRecords(testQuery);
162         if (al != null && al.getHeight() > 0)
163         {
164           boolean dna = sp.isDnaCoding();
165           al.setDataset(null);
166           AlignmentI alds = al.getDataset();
167           // try and find products
168           CrossRef crossRef = new CrossRef(al.getSequencesArray(), alds);
169           List<String> types = crossRef.findXrefSourcesForSequences(dna);
170           if (types != null)
171           {
172             System.out.println("Xref Types for: " + (dna ? "dna" : "prot"));
173             for (String source : types)
174             {
175               System.out.println("Type: " + source);
176               SequenceI[] prod = crossRef.findXrefSequences(source, dna)
177                       .getSequencesArray();
178               System.out.println("Found "
179                       + ((prod == null) ? "no" : "" + prod.length)
180                       + " products");
181               if (prod != null)
182               {
183                 for (int p = 0; p < prod.length; p++)
184                 {
185                   System.out.println("Prod " + p + ": "
186                           + prod[p].getDisplayId(true));
187                 }
188               }
189             }
190           }
191           else
192           {
193             noProds.addElement((dna ? new Object[] { al, al }
194                     : new Object[] { al }));
195           }
196
197         }
198       } catch (Exception ex)
199       {
200         System.out.println("ERROR:Failed to retrieve test query.");
201         ex.printStackTrace(System.out);
202       }
203
204       if (al == null)
205       {
206         System.out.println("ERROR:No alignment retrieved.");
207         StringBuffer raw = sp.getRawRecords();
208         if (raw != null)
209         {
210           System.out.println(raw.toString());
211         }
212         else
213         {
214           System.out.println("ERROR:No Raw results.");
215         }
216       }
217       else
218       {
219         System.out.println("Retrieved " + al.getHeight() + " sequences.");
220         if (ds == null)
221         {
222           ds = al.getDataset();
223         }
224         else
225         {
226           ds.append(al.getDataset());
227           al.setDataset(ds);
228         }
229       }
230       System.out.flush();
231       System.err.flush();
232     }
233     if (noProds.size() > 0)
234     {
235       Enumeration<Object[]> ts = noProds.elements();
236       while (ts.hasMoreElements())
237
238       {
239         Object[] typeSq = ts.nextElement();
240         boolean dna = (typeSq.length > 1);
241         AlignmentI al = (AlignmentI) typeSq[0];
242         System.out.println("Trying getProducts for "
243                 + al.getSequenceAt(0).getDisplayId(true));
244         System.out.println("Search DS Xref for: " + (dna ? "dna" : "prot"));
245         // have a bash at finding the products amongst all the retrieved
246         // sequences.
247         SequenceI[] seqs = al.getSequencesArray();
248         Alignment prodal = new CrossRef(seqs, ds).findXrefSequences(null,
249                 dna);
250         System.out.println("Found "
251                 + ((prodal == null) ? "no" : "" + prodal.getHeight())
252                 + " products");
253         if (prodal != null)
254         {
255           SequenceI[] prod = prodal.getSequencesArray(); // note
256           // should
257           // test
258           // rather
259           // than
260           // throw
261           // away
262           // codon
263           // mapping
264           // (if
265           // present)
266           for (int p = 0; p < prod.length; p++)
267           {
268             System.out.println("Prod " + p + ": "
269                     + prod[p].getDisplayId(true));
270           }
271         }
272       }
273     }
274   }
275 }