Merge branch 'develop' into features/JAL-653_JAL-1766_htslib_refseqsupport
[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 noProds = new Vector();
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]).getSequencesArray();
120                 System.out.println("Found "
121                         + ((prod == null) ? "no" : "" + prod.length)
122                         + " products");
123                 if (prod != null)
124                 {
125                   for (int p = 0; p < prod.length; p++)
126                   {
127                     System.out.println("Prod " + p + ": "
128                             + prod[p].getDisplayId(true));
129                   }
130                 }
131               }
132             }
133             else
134             {
135               noProds.addElement((dna ? new Object[] { al, al }
136                       : new Object[] { al }));
137             }
138   
139           }
140         } catch (Exception ex)
141         {
142           System.out.println("ERROR:Failed to retrieve test query.");
143           ex.printStackTrace(System.out);
144         }
145   
146         if (al == null)
147         {
148           System.out.println("ERROR:No alignment retrieved.");
149           StringBuffer raw = sp.getRawRecords();
150           if (raw != null)
151           {
152             System.out.println(raw.toString());
153           }
154           else
155           {
156             System.out.println("ERROR:No Raw results.");
157           }
158         }
159         else
160         {
161           System.out.println("Retrieved " + al.getHeight() + " sequences.");
162           for (int s = 0; s < al.getHeight(); s++)
163           {
164             SequenceI sq = al.getSequenceAt(s);
165             while (sq.getDatasetSequence() != null)
166             {
167               sq = sq.getDatasetSequence();
168   
169             }
170             if (ds == null)
171             {
172               ds = new Alignment(new SequenceI[] { sq });
173   
174             }
175             else
176             {
177               ds.addSequence(sq);
178             }
179           }
180         }
181         System.out.flush();
182         System.err.flush();
183   
184       }
185       if (noProds.size() > 0)
186       {
187         Enumeration ts = noProds.elements();
188         while (ts.hasMoreElements())
189   
190         {
191           Object[] typeSq = (Object[]) ts.nextElement();
192           boolean dna = (typeSq.length > 1);
193           AlignmentI al = (AlignmentI) typeSq[0];
194           System.out.println("Trying getProducts for "
195                   + al.getSequenceAt(0).getDisplayId(true));
196           System.out.println("Search DS Xref for: "
197                   + (dna ? "dna" : "prot"));
198           // have a bash at finding the products amongst all the retrieved
199           // sequences.
200           SequenceI[] seqs = al.getSequencesArray();
201           Alignment prodal = jalview.analysis.CrossRef.findXrefSequences(
202                   seqs, dna, null, ds);
203           System.out.println("Found "
204                   + ((prodal == null) ? "no" : "" + prodal.getHeight())
205                   + " products");
206           if (prodal != null)
207           {
208             SequenceI[] prod = prodal.getSequencesArray(); // note
209             // should
210             // test
211             // rather
212             // than
213             // throw
214             // away
215             // codon
216             // mapping
217             // (if
218             // present)
219             for (int p = 0; p < prod.length; p++)
220             {
221               System.out.println("Prod " + p + ": "
222                       + prod[p].getDisplayId(true));
223             }
224           }
225         }
226   
227       }
228   
229     }
230   }
231
232 }