View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 utgenome.org
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  // utgb-core Project
18  //
19  // InfoSilkGenerator.java
20  // Since: May 25, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.bio;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.ListIterator;
30  
31  /**
32   * Generating read information in Silk format, which is used for displaying mouse over message
33   * 
34   * @author leo
35   * 
36   */
37  public class InfoSilkGenerator extends OnGenomeDataVisitorBase {
38  
39  	public ArrayList<String> lines = new ArrayList<String>();
40  
41  	public String getSilk() {
42  		StringBuilder buf = new StringBuilder();
43  		for (String each : lines) {
44  			buf.append(each);
45  			buf.append("\n");
46  		}
47  		return buf.toString();
48  	}
49  
50  	public List<String> getLines() {
51  		return lines;
52  	}
53  
54  	public void addLine(String text) {
55  		lines.add(text);
56  	}
57  
58  	@Override
59  	public void visitBEDGene(BEDGene g) {
60  		visitGene(g);
61  		addLine("-score:" + g.score);
62  
63  		if (!g.getCDS().isEmpty()) {
64  			// Note that only one CDS region is contained in BED files.
65  			CDS cds = g.getCDS().get(0);
66  			addLine("-thick start: " + cds.start);
67  			addLine("-thick end: " + cds.end);
68  			addLine("-thick width: " + (cds.end - cds.start));
69  
70  			// compute the CDS sequence length
71  			int cdsLength = 0;
72  			for (Exon e : g.getExon()) {
73  				Interval overlap = e.intersect(cds);
74  				if (overlap == null)
75  					continue;
76  				cdsLength += overlap.length();
77  			}
78  			addLine("-thick length: " + cdsLength);
79  
80  			int len_UTR5 = 0;
81  			for (Exon e : g.getExon()) {
82  				Interval overlap = e.intersect(cds);
83  				if (overlap == null) {
84  					len_UTR5 += e.length();
85  					continue;
86  				}
87  				else {
88  					len_UTR5 += (overlap.getStart() - e.getStart());
89  					break;
90  				}
91  			}
92  
93  			int len_UTR3 = 0;
94  			for (ListIterator<Exon> it = g.getExon().listIterator(g.getExon().size()); it.hasPrevious();) {
95  				Exon e = it.previous();
96  				Interval overlap = e.intersect(cds);
97  				if (overlap == null) {
98  					len_UTR3 += e.length();
99  					continue;
100 				}
101 				else {
102 					len_UTR3 += (e.getEnd() - overlap.getEnd());
103 					break;
104 				}
105 			}
106 
107 			if (g.isAntiSense()) {
108 				// swap
109 				int tmp = len_UTR5;
110 				len_UTR5 = len_UTR3;
111 				len_UTR3 = tmp;
112 			}
113 
114 			if (len_UTR5 > 0)
115 				addLine("-UTR length 5': " + len_UTR5);
116 			if (len_UTR3 > 0)
117 				addLine("-UTR length 3': " + len_UTR3);
118 		}
119 
120 	}
121 
122 	@Override
123 	public void visitGap(Gap g) {
124 
125 	}
126 
127 	@Override
128 	public void visitInterval(Interval interval) {
129 		addLine("-name: " + interval.getName());
130 		addLine("-start: " + interval.getStart());
131 		addLine("-end: " + interval.getEnd());
132 		addLine("-length: " + interval.length());
133 	}
134 
135 	@Override
136 	public void visitRead(Read r) {
137 		visitInterval(r);
138 		addLine("-strand: " + r.getStrand());
139 	}
140 
141 	@Override
142 	public void visitSAMRead(SAMRead r) {
143 		visitSAMReadLight(r);
144 		//addLine("-QV: " + r.qual);
145 		if (r.iSize != 0)
146 			addLine("-insert size: " + r.iSize);
147 		addLine("-mapq: " + r.mapq);
148 		if (r.isPairedRead()) {
149 			addLine("-mate ref: " + r.mrnm);
150 			addLine("-mate start: " + r.mStart);
151 		}
152 
153 		addLine("-tag");
154 		for (String key : r.tag.keySet()) {
155 			addLine("  -" + key + ": " + r.tag.get(key));
156 		}
157 	}
158 
159 	@Override
160 	public void visitSAMReadLight(SAMReadLight r) {
161 		visitInterval(r);
162 		addLine("-flag: " + Integer.toBinaryString(r.flag));
163 		addLine("-strand: " + (r.isSense() ? "+" : "-"));
164 		addLine("-cigar: " + r.cigar);
165 	}
166 
167 	@Override
168 	public void visitSequence(ReferenceSequence referenceSequence) {
169 		// TODO Auto-generated method stub
170 	}
171 
172 	@Override
173 	public void visitReadCoverage(ReadCoverage readCoverage) {
174 		addLine("-name: " + readCoverage.getName());
175 		addLine("-start: " + readCoverage.getStart());
176 		addLine("-end: " + readCoverage.getEnd());
177 		addLine("-pixel width: " + readCoverage.pixelWidth);
178 	}
179 
180 	@Override
181 	public void visitSAMReadPairFragment(SAMReadPairFragment fragment) {
182 		super.visitSAMReadPairFragment(fragment);
183 		addLine("-mate start: " + fragment.mateStart);
184 
185 	}
186 
187 	@Override
188 	public void visitGraph(GraphData graph) {
189 		addLine("graph data");
190 	}
191 
192 	@Override
193 	public void visitReadList(ReadList readList) {
194 		visitInterval(readList);
195 	}
196 
197 }