JAL-2094 new classes ColorI, Colour added
[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.Enumeration;
11 import java.util.List;
12 import java.util.Vector;
13
14 public class SequenceFetcherTest
15 {
16
17   /**
18    * simple run method to test dbsources.
19    * 
20    * @param argv
21    */
22   public static void main(String[] argv)
23   {
24     // TODO: extracted from SequenceFetcher - convert to proper unit test with
25     // assertions
26
27     AlignmentI ds = null;
28     Vector<Object[]> noProds = new Vector<Object[]>();
29     String usage = "SequenceFetcher.main [-nodas] [<DBNAME> [<ACCNO>]]\n"
30             + "With no arguments, all DbSources will be queried with their test Accession number.\n"
31             + "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"
32             + "If given two arguments, SequenceFetcher will try to find the DbFetcher corresponding to <DBNAME> and retrieve <ACCNO> from it.\n"
33             + "The -nodas option will exclude DAS sources from the database fetchers Jalview will try to use.";
34     boolean withDas = true;
35     if (argv != null && argv.length > 0
36             && argv[0].toLowerCase().startsWith("-nodas"))
37     {
38       withDas = false;
39       String targs[] = new String[argv.length - 1];
40       System.arraycopy(argv, 1, targs, 0, targs.length);
41       argv = targs;
42     }
43     if (argv != null && argv.length > 0)
44     {
45       List<DbSourceProxy> sps = new SequenceFetcher(withDas)
46               .getSourceProxy(argv[0]);
47   
48       if (sps != null)
49       {
50         for (DbSourceProxy sp : sps)
51         {
52           AlignmentI al = null;
53           try
54           {
55             al = sp.getSequenceRecords(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           SequenceI[] prod = al.getSequencesArray();
65           if (al != null)
66           {
67             for (int p = 0; p < prod.length; p++)
68             {
69               System.out.println("Prod " + p + ": "
70                       + prod[p].getDisplayId(true) + " : "
71                       + prod[p].getDescription());
72             }
73           }
74         }
75         return;
76       }
77       else
78       {
79         System.err.println("Can't resolve " + argv[0]
80                 + " as a database name. Allowed values are :\n"
81                 + new SequenceFetcher().getSupportedDb());
82       }
83       System.out.println(usage);
84       return;
85     }
86     ASequenceFetcher sfetcher = new SequenceFetcher(withDas);
87     String[] dbSources = sfetcher.getSupportedDb();
88     for (int dbsource = 0; dbsource < dbSources.length; dbsource++)
89     {
90       String db = dbSources[dbsource];
91       // skip me
92       if (db.equals(DBRefSource.PDB))
93       {
94         continue;
95       }
96       for (DbSourceProxy sp : sfetcher.getSourceProxy(db))
97       {
98         System.out.println("Source: " + sp.getDbName() + " (" + db
99                 + "): retrieving test:" + sp.getTestQuery());
100         AlignmentI al = null;
101         try
102         {
103           al = sp.getSequenceRecords(sp.getTestQuery());
104           if (al != null && al.getHeight() > 0)
105           {
106             boolean dna = sp.isDnaCoding();
107             // try and find products
108             String types[] = jalview.analysis.CrossRef
109                     .findSequenceXrefTypes(dna, al.getSequencesArray());
110             if (types != null)
111             {
112               System.out.println("Xref Types for: "
113                       + (dna ? "dna" : "prot"));
114               for (int t = 0; t < types.length; t++)
115               {
116                 System.out.println("Type: " + types[t]);
117                 SequenceI[] prod = jalview.analysis.CrossRef
118                         .findXrefSequences(al.getSequencesArray(), dna,
119                                 types[t], null)
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 = jalview.analysis.CrossRef.findXrefSequences(
203                   seqs, dna, null, ds);
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 }