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