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.sam;
24
25 import java.io.File;
26 import java.util.Iterator;
27 import java.util.NoSuchElementException;
28
29 import net.sf.samtools.SAMFileReader;
30 import net.sf.samtools.SAMRecord;
31 import net.sf.samtools.SAMRecordIterator;
32
33 import org.utgenome.gwt.utgb.client.bio.ChrLoc;
34 import org.utgenome.gwt.utgb.client.bio.SAMRead;
35
36
37
38
39
40
41
42 public class SAMReadIterator implements Iterator<SAMRead> {
43
44 private final SAMFileReader sam;
45 private SAMRecordIterator it;
46
47 public SAMReadIterator(File bamFile, String chr) {
48 this(bamFile, chr, 0, 0);
49 }
50
51 public SAMReadIterator(File bamFile, String chr, int start, int end) {
52 sam = new SAMFileReader(bamFile, SAMReader.getBamIndexFile(bamFile));
53 it = sam.queryOverlapping(chr, start, end);
54 }
55
56 public boolean hasNext() {
57 if (it == null)
58 return false;
59
60 boolean hasNext = it.hasNext();
61 if (hasNext == false) {
62 it.close();
63 sam.close();
64 it = null;
65 }
66 return hasNext;
67 }
68
69 public SAMRead next() {
70 SAMRecord r = (it != null) ? it.next() : null;
71 if (r == null)
72 throw new NoSuchElementException();
73
74 return SAM2SilkReader.convertToSAMRead(r);
75 }
76
77 public void remove() {
78 throw new UnsupportedOperationException("remove");
79 }
80
81 public static Iterator<SAMRead> getIteratorOnChr(File bamFile, String chr) {
82 return new SAMReadIterator(bamFile, chr);
83 }
84
85 public static Iterator<SAMRead> getOverlappingIterator(File bamFile, ChrLoc loc) {
86 return new SAMReadIterator(bamFile, loc.chr, loc.start, loc.end);
87 }
88
89 }