JAL-2110 remove hardwired detection of search context from the dataset ‘isNucleotide...
[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     AlignmentI ds = null;
29     Vector<Object[]> noProds = new Vector<Object[]>();
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             CrossRef crossRef = new CrossRef(al.getSequencesArray(),
110                     al);
111             List<String> types = crossRef.findXrefSourcesForSequences(dna);
112             if (types != null)
113             {
114               System.out.println("Xref Types for: "
115                       + (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           for (int s = 0; s < al.getHeight(); s++)
164           {
165             SequenceI sq = al.getSequenceAt(s);
166             while (sq.getDatasetSequence() != null)
167             {
168               sq = sq.getDatasetSequence();
169   
170             }
171             if (ds == null)
172             {
173               ds = new Alignment(new SequenceI[] { sq });
174   
175             }
176             else
177             {
178               ds.addSequence(sq);
179             }
180           }
181         }
182         System.out.flush();
183         System.err.flush();
184   
185       }
186       if (noProds.size() > 0)
187       {
188         Enumeration<Object[]> ts = noProds.elements();
189         while (ts.hasMoreElements())
190   
191         {
192           Object[] typeSq = ts.nextElement();
193           boolean dna = (typeSq.length > 1);
194           AlignmentI al = (AlignmentI) typeSq[0];
195           System.out.println("Trying getProducts for "
196                   + al.getSequenceAt(0).getDisplayId(true));
197           System.out.println("Search DS Xref for: "
198                   + (dna ? "dna" : "prot"));
199           // have a bash at finding the products amongst all the retrieved
200           // sequences.
201           SequenceI[] seqs = al.getSequencesArray();
202           Alignment prodal = new CrossRef(seqs, ds).findXrefSequences(null,
203                   dna);
204           System.out.println("Found "
205                   + ((prodal == null) ? "no" : "" + prodal.getHeight())
206                   + " products");
207           if (prodal != null)
208           {
209             SequenceI[] prod = prodal.getSequencesArray(); // note
210             // should
211             // test
212             // rather
213             // than
214             // throw
215             // away
216             // codon
217             // mapping
218             // (if
219             // present)
220             for (int p = 0; p < prod.length; p++)
221             {
222               System.out.println("Prod " + p + ": "
223                       + prod[p].getDisplayId(true));
224             }
225           }
226         }
227   
228       }
229   
230     }
231   }
232
233 }