OSDN Git Service

add LogWriter interface to enable switch log format
[xerial/xerial-core.git] / src / test / java / org / xerial / lens / LensTest.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2009 Taro L. Saito
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *--------------------------------------------------------------------------*/
16 //--------------------------------------
17 // XerialJ
18 //
19 // LensTest.java
20 // Since: Feb 23, 2009 6:02:27 PM
21 //
22 // $URL$
23 // $Author$
24 //--------------------------------------
25 package org.xerial.lens;
26
27 import static org.junit.Assert.*;
28
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.xerial.core.XerialException;
38 import org.xerial.lens.relation.NodeBase;
39 import org.xerial.lens.relation.Tuple;
40 import org.xerial.silk.SilkParser;
41 import org.xerial.silk.SilkUtilTest;
42 import org.xerial.util.FileResource;
43 import org.xerial.util.HashedArrayList;
44 import org.xerial.util.StringUtil;
45 import org.xerial.util.log.Logger;
46
47 public class LensTest {
48     private static Logger _logger = Logger.getLogger(LensTest.class);
49
50     @Before
51     public void setUp() throws Exception {}
52
53     @After
54     public void tearDown() throws Exception {}
55
56     public static class GeneTable {
57         private HashedArrayList<Coordinate, Gene> sequenceTable = new HashedArrayList<Coordinate, Gene>();
58
59         public void add(Coordinate coordinate, Gene gene) {
60             sequenceTable.put(coordinate, gene);
61         }
62     }
63
64     static class Gene {
65         private String name;
66         private long start;
67         private String strand;
68         private StringBuilder sequence = new StringBuilder();
69
70         public void appendSequence(String sequence) {}
71     }
72
73     @Test
74     public void testTranslateSilk() throws IOException, XerialException {
75         GeneTable g = Lens.loadSilk(GeneTable.class, FileResource.find(LensTest.class,
76                 "sequence.silk"));
77
78         assertNotNull(g);
79         assertEquals(2, g.sequenceTable.size());
80         for (Coordinate each : g.sequenceTable.keySet()) {
81             List<Gene> gl = g.sequenceTable.get(each);
82             assertEquals(2, gl.size());
83         }
84
85     }
86
87     @Ignore
88     @Test
89     public void testMapping() throws Exception {
90         // TODO: How to resolve nested scope when the information of gene class is not available  
91         // -coordinate(name:chr1)
92         //   -gene(name:gene1)
93
94         Coordinate c = Lens.loadSilk(Coordinate.class, FileResource.find(SilkUtilTest.class,
95                 "sequence.silk"));
96
97         _logger.info(c);
98         assertNotNull(c);
99         assertEquals("utgb", c.group);
100         assertEquals("human", c.species);
101         assertEquals("hg18", c.revision);
102         assertEquals("chr1", c.name);
103
104     }
105
106     @Test
107     public void testBED() throws Exception {
108         BEDQuery result = new BEDQuery("chr7", 1L, 1000000000L);
109         Lens.loadSilk(result, FileResource.find(LensTest.class, "sample.bed.silk"));
110
111         _logger.debug(StringUtil.join(result.geneList, "\n"));
112
113         assertEquals("Item,RGB,Demo2", result.track.name);
114         assertEquals("Item RGBdemonstration2", result.track.description);
115         assertEquals(2, result.track.visibility);
116         assertEquals("On", result.track.itemRgb);
117         assertEquals(1, result.track.useScore);
118         assertEquals("0,128,0", result.track.color);
119         assertEquals("http://genome.ucsc.edu/goldenPath/help/clones.html#$$", result.track.url);
120     }
121
122     public static class Locus {
123         public long start;
124         public long end;
125
126         public Locus() {}
127
128         public Locus(long start, long end) {
129             this.start = start;
130             this.end = end;
131         }
132
133         public boolean hasOverlap(Locus other) {
134             return (this.start <= other.end) && (this.end >= other.start);
135         }
136
137         @Override
138         public String toString() {
139             return String.format("(%s, %s)", start, end);
140         }
141     }
142
143     public static class CDS extends Locus {
144
145     }
146
147     public static class Exon extends Locus {
148
149     }
150
151     public static class BEDGene extends Locus {
152         public String coordinate;
153         public String name;
154         public String color;
155         public String strand;
156         public List<CDS> cds;
157         public List<Exon> exon;
158
159         @Override
160         public String toString() {
161             return ObjectLens.toJSON(this);
162         }
163     }
164
165     public static class BEDTrack {
166         public String name;
167         public String description;
168         public String itemRgb;
169         public int visibility;
170         public String color;
171         public int useScore;
172         public String url;
173     }
174
175     public static class BEDQuery {
176         private String coordinate;
177         private Locus queryRange;
178         private List<BEDGene> geneList = new ArrayList<BEDGene>();
179
180         public BEDTrack track;
181
182         public BEDQuery(String coordinate, long startOnGenome, long endOnGenome) {
183             this.coordinate = coordinate;
184             this.queryRange = new Locus(startOnGenome, endOnGenome);
185         }
186
187         public void addGene(BEDGene gene) {
188             // where conditions
189             if (coordinate.equals(gene.coordinate) && queryRange.hasOverlap(gene)) {
190                 // draw the gene on the canvas
191                 geneList.add(gene);
192             }
193         }
194     }
195
196     public static class DASFeature {
197         public DASGFF gff;
198         public Segment segment;
199
200         @Override
201         public String toString() {
202             return ObjectLens.toJSON(this);
203         }
204
205     }
206
207     public static class DASGFF {
208         public String version;
209         public String href;
210
211     }
212
213     public static class Segment {
214         public String id;
215         public long start;
216         public long stop;
217         public List<Feature> feature;
218     }
219
220     public static class Feature {
221         public String id;
222         public long start;
223         public long end;
224
225         public String score;
226         public String orientation;
227
228         public Method method;
229         public FeatureType type;
230         public Group group;
231         public Target target;
232
233     }
234
235     public static class Target {
236         public String id;
237         public long start;
238         public long stop;
239     }
240
241     public static class FeatureType {
242         public String id;
243         public String category;
244         public String value;
245
246     }
247
248     public static class Group {
249         public String id;
250         public String type;
251         public String label;
252         public Link link;
253
254     }
255
256     public static class Link {
257         public String href;
258         public String value;
259     }
260
261     public static class Method {
262         public String id;
263         public String value;
264     }
265
266     /*
267      * <pre>
268     <SEGMENT id="13" start="1800000" stop="18100000"> 
269     <FEATURE id="ENSE00001471274"> 
270     <START>17957458</START> 
271     <END>17957578</END> 
272     <TYPE id="exon:coding:ensembl" category="transcription">exon:coding:ensembl</TYPE> 
273     <METHOD id="ensembl">ensembl</METHOD> 
274     <SCORE>-</SCORE> 
275     <ORIENTATION>-</ORIENTATION> 
276     <GROUP id="ENST00000342944" type="transcript:ensembl" label="ENST00000342944 (AL138715.11-201)"> 
277       <LINK href="http://www.ensembl.org/Homo_sapiens/Transcript/Summary?t=ENST00000342944;db=core">TransView ENST00000342944</LINK> 
278     </GROUP> 
279     <TARGET id="ENST00000342944" start="1" stop="121" /> 
280     </FEATURE> 
281     </SEGMENT> 
282     </pre>
283      */
284     @Test
285     public void dasTest() throws Exception {
286         DASFeature das = Lens.loadXML(DASFeature.class, FileResource
287                 .find(LensTest.class, "das.xml"));
288         assertEquals(1, das.segment.feature.size());
289         Feature f = das.segment.feature.get(0);
290         _logger.debug(das);
291         assertEquals("ENSE00001471274", f.id);
292         assertEquals(17957458L, f.start);
293         assertEquals(17957578L, f.end);
294         assertEquals("-", f.score);
295         assertEquals("-", f.orientation);
296         Target t = f.target;
297         assertEquals("ENST00000342944", t.id);
298         assertEquals(1L, t.start);
299         assertEquals(121L, t.stop);
300
301     }
302
303     public static class Text {
304         public String value;
305     }
306
307     public static class TextQuery {
308         public Text text;
309     }
310
311     @Test
312     public void textData() throws Exception {
313         TextQuery q = Lens
314                 .loadSilk(TextQuery.class, FileResource.find(LensTest.class, "text.silk"));
315         assertNotNull(q.text);
316         assertEquals("hello world", q.text.value);
317     }
318
319     public static class ArrayData {
320         public List<String> list = new ArrayList<String>();
321     }
322
323     @Test
324     public void toJSONTest() throws Exception {
325         ArrayData d = new ArrayData();
326         assertEquals("{\"list\":[]}", Lens.toJSON(d));
327     }
328
329     public static class MyGene {
330         public String name;
331         public long start;
332         public String link;
333     }
334
335     public static class MyGeneQuery {
336         private List<MyGene> geneList = new ArrayList<MyGene>();
337
338         public void addGene_Coordinate(MyGene gene, Chr c) {
339             if (c.name.equals("chr1"))
340                 geneList.add(gene);
341         }
342
343         public List<MyGene> getGeneList() {
344             return geneList;
345         }
346     }
347
348     public static class Chr {
349         public String name;
350     }
351
352     @Test
353     public void testLens() throws Exception {
354
355         MyGeneQuery result = Lens.loadSilk(MyGeneQuery.class, FileResource.open(LensTest.class,
356                 "sequence.silk"));
357
358         _logger.debug(Lens.toJSON(result));
359     }
360
361     @Ignore
362     @Test
363     public void testFind() throws Exception {
364         Lens.find(MyGene.class, "gene", new SilkParser(FileResource.find(LensTest.class,
365                 "sequence.silk")), new ObjectHandler<MyGene>() {
366
367             public void handle(MyGene input) throws Exception {
368                 _logger.info(Lens.toJSON(input));
369             }
370         });
371     }
372
373     public static class TupleNode extends NodeBase<TupleNode> {
374         public final int id;
375         public final String name;
376
377         public TupleNode(int id, String name) {
378             this.id = id;
379             this.name = name;
380         }
381     }
382
383     @Test
384     public void testToSilkTuple() throws Exception {
385         Tuple<TupleNode> t = new Tuple<TupleNode>();
386         t.add(new TupleNode(1, "A"));
387         t.add(new TupleNode(2, "B"));
388
389         String s = Lens.toSilk(t);
390         _logger.debug(s);
391     }
392
393 }