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