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