JAL-1705 updated for change to CrossRef signature
[jalview.git] / test / jalview / ws / SequenceFetcherTest.java
1 package jalview.ws;
2
3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.DBRefSource;
6 import jalview.datamodel.SequenceI;
7 import jalview.ws.seqfetcher.ASequenceFetcher;
8 import jalview.ws.seqfetcher.DbSourceProxy;
9
10 import java.util.ArrayList;
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     AlignmentI ds = null;
29     Vector noProds = new Vector();
30     String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
31             + "With no arguments, all DbSources will be queried with their test Accession number.\n"
32             + "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"
33             + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
34             + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
35     boolean withDas = true;
36     if (argv != null && argv.length > 0
37             && argv[0].toLowerCase().startsWith("-nodas"))
38     {
39       withDas = false;
40       String targs[] = new String[argv.length - 1];
41       System.arraycopy(argv, 1, targs, 0, targs.length);
42       argv = targs;
43     }
44     if (argv != null && argv.length > 0)
45     {
46       List<DbSourceProxy> sps = new SequenceFetcher(withDas)
47               .getSourceProxy(argv[0]);
48   
49       if (sps != null)
50       {
51         for (DbSourceProxy sp : sps)
52         {
53           AlignmentI al = null;
54           try
55           {
56             al = sp.getSequenceRecords(argv.length > 1 ? argv[1] : sp
57                     .getTestQuery());
58           } catch (Exception e)
59           {
60             e.printStackTrace();
61             System.err.println("Error when retrieving "
62                     + (argv.length > 1 ? argv[1] : sp.getTestQuery())
63                     + " from " + argv[0] + "\nUsage: " + usage);
64           }
65           SequenceI[] prod = al.getSequencesArray();
66           if (al != null)
67           {
68             for (int p = 0; p < prod.length; p++)
69             {
70               System.out.println("Prod " + p + ": "
71                       + prod[p].getDisplayId(true) + " : "
72                       + prod[p].getDescription());
73             }
74           }
75         }
76         return;
77       }
78       else
79       {
80         System.err.println("Can't resolve " + argv[0]
81                 + " as a database name. Allowed values are :\n"
82                 + new SequenceFetcher().getSupportedDb());
83       }
84       System.out.println(usage);
85       return;
86     }
87     ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
88     String[] dbSources = sfetcher.getSupportedDb();
89     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
90     {
91       String db = dbSources[dbsource];
92       // skip me
93       if (db.equals(DBRefSource.PDB))
94       {
95         continue;
96       }
97       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
98       {
99         System.out.println("Source: " + sp.getDbName() + " (" + db
100                 + "): retrieving test:" + sp.getTestQuery());
101         AlignmentI al = null;
102         try
103         {
104           al = sp.getSequenceRecords(sp.getTestQuery());
105           if (al != null && al.getHeight() > 0)
106           {
107             boolean dna = sp.isDnaCoding();
108             // try and find products
109             String types[] = jalview.analysis.CrossRef
110                     .findSequenceXrefTypes(dna, al.getSequencesArray());
111             if (types != null)
112             {
113               System.out.println("Xref Types for: "
114                       + (dna ? "dna" : "prot"));
115               for (int t = 0; t < types.length; t++)
116               {
117                 System.out.println("Type: " + types[t]);
118                 SequenceI[] prod = jalview.analysis.CrossRef
119                         .findXrefSequences(al.getSequencesArray(), dna,
120                                 types[t], null, new ArrayList<SequenceI>())
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           for (int s = 0; s < al.getHeight(); s++)
165           {
166             SequenceI sq = al.getSequenceAt(s);
167             while (sq.getDatasetSequence() != null)
168             {
169               sq = sq.getDatasetSequence();
170   
171             }
172             if (ds == null)
173             {
174               ds = new Alignment(new SequenceI[] { sq });
175   
176             }
177             else
178             {
179               ds.addSequence(sq);
180             }
181           }
182         }
183         System.out.flush();
184         System.err.flush();
185   
186       }
187       if (noProds.size() > 0)
188       {
189         Enumeration ts = noProds.elements();
190         while (ts.hasMoreElements())
191   
192         {
193           Object[] typeSq = (Object[]) ts.nextElement();
194           boolean dna = (typeSq.length > 1);
195           AlignmentI al = (AlignmentI) typeSq[0];
196           System.out.println("Trying getProducts for "
197                   + al.getSequenceAt(0).getDisplayId(true));
198           System.out.println("Search DS Xref for: "
199                   + (dna ? "dna" : "prot"));
200           // have a bash at finding the products amongst all the retrieved
201           // sequences.
202           SequenceI[] seqs = al.getSequencesArray();
203           Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
204                   seqs, dna, null, ds, new ArrayList<SequenceI>());
205           System.out.println("Found "
206                   + ((prodal == null) ? "no" : "" + prodal.getHeight())
207                   + " products");
208           if (prodal != null)
209           {
210             SequenceI[] prod = prodal.getSequencesArray(); // note
211             // should
212             // test
213             // rather
214             // than
215             // throw
216             // away
217             // codon
218             // mapping
219             // (if
220             // present)
221             for (int p = 0; p < prod.length; p++)
222             {
223               System.out.println("Prod " + p + ": "
224                       + prod[p].getDisplayId(true));
225             }
226           }
227         }
228   
229       }
230   
231     }
232   }
233
234 }