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  // SAM2SilkReader.java
20  // Since: Mar 15, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.format.sam;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.Reader;
30  import java.io.StringWriter;
31  import java.io.Writer;
32  
33  import net.sf.samtools.SAMFileReader;
34  import net.sf.samtools.SAMRecord;
35  import net.sf.samtools.SAMRecord.SAMTagAndValue;
36  import net.sf.samtools.util.CloseableIterator;
37  
38  import org.apache.tools.ant.util.ReaderInputStream;
39  import org.utgenome.format.FormatConversionReader;
40  import org.utgenome.gwt.utgb.client.bio.SAMRead;
41  import org.utgenome.gwt.utgb.client.bio.SAMReadLight;
42  import org.utgenome.gwt.utgb.client.util.Properties;
43  import org.xerial.silk.SilkWriter;
44  
45  /**
46   * Reader for converting SAM into Silk
47   * 
48   * @author leo
49   * 
50   */
51  public class SAM2SilkReader extends FormatConversionReader {
52  
53  	public SAM2SilkReader(InputStream input) throws IOException {
54  		super(input, new Converter());
55  	}
56  
57  	public SAM2SilkReader(Reader input) throws IOException {
58  		this(new ReaderInputStream(input));
59  	}
60  
61  	private static class Converter extends FormatConversionReader.PipeConsumer {
62  
63  		@Override
64  		public void consume(InputStream in, Writer out) throws Exception {
65  			if (out == null)
66  				return;
67  
68  			SAMFileReader samReader = new SAMFileReader(in);
69  
70  			SilkWriter w = new SilkWriter(out);
71  			w.preamble();
72  			w.preamble("schema record(qname, flag, rname, start, end, mapq, cigar, mrnm, mpos, isize, seq, qual, tag, vtype, tag*)");
73  			for (CloseableIterator<SAMRecord> it = samReader.iterator(); it.hasNext();) {
74  				SAMRecord rec = it.next();
75  				toSilk(rec, w);
76  			}
77  
78  		}
79  
80  	}
81  
82  	/**
83  	 * convert a SAMRecord into a SAMRead, which can be used in GWT code.
84  	 * 
85  	 * @param record
86  	 * @return
87  	 */
88  	public static SAMRead convertToSAMRead(SAMRecord record) {
89  		SAMRead read = new SAMRead(record.getAlignmentStart(), record.getAlignmentEnd() + 1);
90  		if (record != null) {
91  			read.qname = record.getReadName();
92  			read.flag = record.getFlags();
93  			read.rname = record.getReferenceName();
94  			read.mapq = record.getMappingQuality();
95  			read.cigar = record.getCigarString();
96  			read.mrnm = record.getMateReferenceName();
97  			read.mStart = record.getMateAlignmentStart();
98  			read.iSize = record.getInferredInsertSize();
99  			read.seq = record.getReadString();
100 			read.qual = record.getBaseQualityString();
101 			read.unclippedStart = record.getUnclippedStart();
102 			read.unclippedEnd = record.getUnclippedEnd() + 1;
103 			read.tag = new Properties();
104 			for (SAMTagAndValue tag : record.getAttributes()) {
105 				read.tag.add(tag.tag, String.valueOf(tag.value));
106 			}
107 		}
108 
109 		return read;
110 	}
111 
112 	/**
113 	 * convert a SAMRecord into a SAMRead, which can be used in GWT code.
114 	 * 
115 	 * @param record
116 	 * @return
117 	 */
118 	public static SAMReadLight convertToSAMReadLight(SAMRecord record) {
119 		SAMReadLight read = new SAMReadLight(record.getAlignmentStart(), record.getAlignmentEnd() + 1);
120 		if (record != null) {
121 			read.qname = record.getReadName();
122 			read.flag = record.getFlags();
123 			read.cigar = record.getCigarString();
124 			read.unclippedStart = record.getUnclippedStart();
125 			read.unclippedEnd = record.getUnclippedEnd() + 1;
126 		}
127 		return read;
128 	}
129 
130 	/**
131 	 * Convert an input SAMRecord into Silk format by using a given SilkWriter
132 	 * 
133 	 * @param rec
134 	 * @param w
135 	 */
136 	public static void toSilk(SAMRecord rec, SilkWriter w) {
137 		StringWriter buf = new StringWriter();
138 		SilkWriter rw = w.node("record");
139 		rw.leaf("qname", rec.getReadName());
140 		rw.leaf("flag", rec.getFlags());
141 		rw.leaf("rname", rec.getReferenceName());
142 		rw.leaf("start", rec.getAlignmentStart());
143 		rw.leaf("end", rec.getAlignmentEnd() + 1);
144 		rw.leaf("unclipped start", rec.getUnclippedStart());
145 		rw.leaf("unclipped end", rec.getUnclippedEnd() + 1);
146 		rw.leaf("mapq", rec.getMappingQuality());
147 		rw.leaf("cigar", rec.getCigarString());
148 		rw.leaf("mrname", rec.getMateReferenceName());
149 		rw.leaf("mstart", rec.getMateAlignmentStart());
150 		rw.leaf("isize", rec.getInferredInsertSize());
151 		rw.leaf("seq", rec.getReadString());
152 		rw.leaf("qual", String.format("\"%s\"", rec.getBaseQualityString()));
153 		SilkWriter tw = rw.node("tag");
154 		for (SAMTagAndValue each : rec.getAttributes()) {
155 			tw.leaf(each.tag, each.value);
156 		}
157 	}
158 
159 	/**
160 	 * Convert an input SAMRecord into Silk format
161 	 * 
162 	 * @param rec
163 	 * @return
164 	 */
165 	public static String toSilk(SAMRecord rec) {
166 		StringWriter buf = new StringWriter();
167 		SilkWriter w = new SilkWriter(buf);
168 		toSilk(rec, w);
169 		w.flush();
170 		return buf.toString();
171 	}
172 
173 }