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