1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.format.axt;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.utgenome.UTGBErrorCode;
36 import org.utgenome.UTGBException;
37 import org.xerial.lens.ObjectStreamHandler;
38 import org.xerial.lens.TextFormatLens;
39 import org.xerial.util.ArrayDeque;
40 import org.xerial.util.Deque;
41 import org.xerial.util.ObjectHandler;
42 import org.xerial.util.StringUtil;
43 import org.xerial.util.log.Logger;
44
45
46
47
48
49
50
51 public class AXTLens implements TextFormatLens {
52
53 private static Logger _logger = Logger.getLogger(AXTLens.class);
54
55 public static AXTAlignment lens(List<String> axtBlock) throws UTGBException {
56 if (axtBlock.size() < 3) {
57 throw new UTGBException(UTGBErrorCode.INVALID_FORMAT, "axtBlock.size() must be larger than 3: " + axtBlock.size());
58 }
59
60 AXTAlignment result = new AXTAlignment();
61 parseSummaryLine(result, axtBlock.get(0));
62 result.primaryAssembly = axtBlock.get(1);
63 result.aligningAssembly = axtBlock.get(2);
64 return result;
65 }
66
67 public static void parseSummaryLine(AXTAlignment obj, String summaryLine) throws UTGBException {
68 String[] c = summaryLine.split("[\\s]+");
69
70 if (c.length < 9)
71 throw new UTGBException(UTGBErrorCode.INVALID_FORMAT, summaryLine);
72
73 try {
74 obj.num = Integer.parseInt(c[0]);
75 obj.s_chr = c[1];
76 obj.s_start = Integer.parseInt(c[2]);
77 obj.s_end = Integer.parseInt(c[3]);
78 obj.d_chr = c[4];
79 obj.d_start = Integer.parseInt(c[5]);
80 obj.d_end = Integer.parseInt(c[6]);
81 obj.strand = c[7];
82 obj.score = Integer.parseInt(c[8]);
83 }
84 catch (NumberFormatException e) {
85 throw new UTGBException(UTGBErrorCode.INVALID_FORMAT, String.format("%s: %s", e.getMessage(), summaryLine));
86 }
87 }
88
89 private static class AXTStream implements Iterable<AXTAlignment> {
90
91 public AXTStream(BufferedReader in) {
92
93 }
94
95 public Iterator<AXTAlignment> iterator() {
96 return null;
97 }
98
99 }
100
101 private static class AXTIteator implements Iterator<AXTAlignment> {
102
103 private BufferedReader in;
104 private Deque<AXTAlignment> cache = new ArrayDeque<AXTAlignment>();
105
106 public AXTIteator(BufferedReader in) {
107 this.in = in;
108 }
109
110 public boolean hasNext() {
111 if (!cache.isEmpty())
112 return true;
113
114 return false;
115 }
116
117 public AXTAlignment next() {
118 if (hasNext())
119 return cache.pollFirst();
120
121 return null;
122 }
123
124 public void remove() {
125 throw new UnsupportedOperationException("remove");
126 }
127 }
128
129 public static Iterable<AXTAlignment> lens(BufferedReader in) throws UTGBException {
130 return new AXTStream(in);
131 }
132
133 public static void lens(BufferedReader in, ObjectHandler<AXTAlignment> handler) throws UTGBException {
134
135 try {
136 handler.init();
137 try {
138 for (;;) {
139 String line = in.readLine();
140 if (line == null)
141 break;
142
143 if (StringUtil.isWhiteSpace(line))
144 continue;
145
146 List<String> axtBlock = new ArrayList<String>(3);
147
148 String header = line;
149 String seq1 = in.readLine();
150 String seq2 = in.readLine();
151
152 if (seq1 == null || seq2 == null) {
153 break;
154 }
155
156 axtBlock.add(line);
157 axtBlock.add(seq1);
158 axtBlock.add(seq2);
159
160 try {
161 handler.handle(lens(axtBlock));
162 }
163 catch (Exception e) {
164 _logger.error(e);
165 }
166 }
167 }
168 finally {
169 handler.finish();
170 if (in != null)
171 in.close();
172 }
173 }
174 catch (Exception e) {
175 throw UTGBException.convert(e);
176 }
177
178 }
179
180 public static void lens(File axtFile, ObjectHandler<AXTAlignment> handler) throws UTGBException {
181 try {
182 lens(new BufferedReader(new FileReader(axtFile)), handler);
183 }
184 catch (Exception e) {
185 throw UTGBException.convert(e);
186 }
187
188 }
189
190 private BufferedReader in;
191
192 public AXTLens(BufferedReader in) {
193 this.in = in;
194 }
195
196 public AXTLens(URL axt) throws IOException {
197 in = new BufferedReader(new InputStreamReader(axt.openStream()));
198 }
199
200 public void convert(ObjectStreamHandler handler) throws Exception {
201 try {
202 try {
203 for (;;) {
204 String line = in.readLine();
205 if (line == null)
206 break;
207
208 if (StringUtil.isWhiteSpace(line))
209 continue;
210
211 List<String> axtBlock = new ArrayList<String>(3);
212
213 String header = line;
214 String seq1 = in.readLine();
215 String seq2 = in.readLine();
216
217 if (seq1 == null || seq2 == null) {
218 break;
219 }
220
221 axtBlock.add(line);
222 axtBlock.add(seq1);
223 axtBlock.add(seq2);
224
225 try {
226 AXTAlignment aln = lens(axtBlock);
227 handler.add("axt", aln);
228 }
229 catch (Exception e) {
230 _logger.error(e);
231 }
232 }
233 }
234 finally {
235 if (in != null)
236 in.close();
237 }
238 }
239 catch (Exception e) {
240 throw UTGBException.convert(e);
241 }
242
243 }
244
245 }