1 /*--------------------------------------------------------------------------
2 * Copyright 2009 Taro L. Saito
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 //--------------------------------------
20 // Since: Feb 23, 2009 6:02:27 PM
24 //--------------------------------------
25 package org.xerial.lens;
27 import static org.junit.Assert.*;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
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;
47 public class LensTest {
48 private static Logger _logger = Logger.getLogger(LensTest.class);
51 public void setUp() throws Exception {}
54 public void tearDown() throws Exception {}
56 public static class GeneTable {
57 private HashedArrayList<Coordinate, Gene> sequenceTable = new HashedArrayList<Coordinate, Gene>();
59 public void add(Coordinate coordinate, Gene gene) {
60 sequenceTable.put(coordinate, gene);
67 private String strand;
68 private StringBuilder sequence = new StringBuilder();
70 public void appendSequence(String sequence) {}
74 public void testTranslateSilk() throws IOException, XerialException {
75 GeneTable g = Lens.loadSilk(GeneTable.class, FileResource.find(LensTest.class,
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());
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)
94 Coordinate c = Lens.loadSilk(Coordinate.class, FileResource.find(SilkUtilTest.class,
99 assertEquals("utgb", c.group);
100 assertEquals("human", c.species);
101 assertEquals("hg18", c.revision);
102 assertEquals("chr1", c.name);
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"));
111 _logger.debug(StringUtil.join(result.geneList, "\n"));
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);
122 public static class Locus {
128 public Locus(long start, long end) {
133 public boolean hasOverlap(Locus other) {
134 return (this.start <= other.end) && (this.end >= other.start);
138 public String toString() {
139 return String.format("(%s, %s)", start, end);
143 public static class CDS extends Locus {
147 public static class Exon extends Locus {
151 public static class BEDGene extends Locus {
152 public String coordinate;
155 public String strand;
156 public List<CDS> cds;
157 public List<Exon> exon;
160 public String toString() {
161 return ObjectLens.toJSON(this);
165 public static class BEDTrack {
167 public String description;
168 public String itemRgb;
169 public int visibility;
175 public static class BEDQuery {
176 private String coordinate;
177 private Locus queryRange;
178 private List<BEDGene> geneList = new ArrayList<BEDGene>();
180 public BEDTrack track;
182 public BEDQuery(String coordinate, long startOnGenome, long endOnGenome) {
183 this.coordinate = coordinate;
184 this.queryRange = new Locus(startOnGenome, endOnGenome);
187 public void addGene(BEDGene gene) {
189 if (coordinate.equals(gene.coordinate) && queryRange.hasOverlap(gene)) {
190 // draw the gene on the canvas
196 public static class DASFeature {
198 public Segment segment;
201 public String toString() {
202 return ObjectLens.toJSON(this);
207 public static class DASGFF {
208 public String version;
213 public static class Segment {
217 public List<Feature> feature;
220 public static class Feature {
226 public String orientation;
228 public Method method;
229 public FeatureType type;
231 public Target target;
235 public static class Target {
241 public static class FeatureType {
243 public String category;
248 public static class Group {
256 public static class Link {
261 public static class Method {
268 <SEGMENT id="13" start="1800000" stop="18100000">
269 <FEATURE id="ENSE00001471274">
270 <START>17957458</START>
272 <TYPE id="exon:coding:ensembl" category="transcription">exon:coding:ensembl</TYPE>
273 <METHOD id="ensembl">ensembl</METHOD>
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>
279 <TARGET id="ENST00000342944" start="1" stop="121" />
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);
291 assertEquals("ENSE00001471274", f.id);
292 assertEquals(17957458L, f.start);
293 assertEquals(17957578L, f.end);
294 assertEquals("-", f.score);
295 assertEquals("-", f.orientation);
297 assertEquals("ENST00000342944", t.id);
298 assertEquals(1L, t.start);
299 assertEquals(121L, t.stop);
303 public static class Text {
307 public static class TextQuery {
312 public void textData() throws Exception {
314 .loadSilk(TextQuery.class, FileResource.find(LensTest.class, "text.silk"));
315 assertNotNull(q.text);
316 assertEquals("hello world", q.text.value);
319 public static class ArrayData {
320 public List<String> list = new ArrayList<String>();
324 public void toJSONTest() throws Exception {
325 ArrayData d = new ArrayData();
326 assertEquals("{\"list\":[]}", Lens.toJSON(d));
329 public static class MyGene {
335 public static class MyGeneQuery {
336 private List<MyGene> geneList = new ArrayList<MyGene>();
338 public void addGene_Coordinate(MyGene gene, Chr c) {
339 if (c.name.equals("chr1"))
343 public List<MyGene> getGeneList() {
348 public static class Chr {
353 public void testLens() throws Exception {
355 MyGeneQuery result = Lens.loadSilk(MyGeneQuery.class, FileResource.open(LensTest.class,
358 _logger.debug(Lens.toJSON(result));
363 public void testFind() throws Exception {
364 Lens.find(MyGene.class, "gene", new SilkParser(FileResource.find(LensTest.class,
365 "sequence.silk")), new ObjectHandler<MyGene>() {
367 public void handle(MyGene input) throws Exception {
368 _logger.info(Lens.toJSON(input));
373 public static class TupleNode extends NodeBase<TupleNode> {
375 public final String name;
377 public TupleNode(int id, String name) {
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"));
389 String s = Lens.toSilk(t);