Debian Codegraph @

uk/ac/babraham/FastQC/Sequence/BAMFile.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-12 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Sequence;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import htsjdk.samtools.CigarElement;
29 import htsjdk.samtools.CigarOperator;
30 import htsjdk.samtools.SAMFormatException;
31 import htsjdk.samtools.SAMRecord;
32 import htsjdk.samtools.SamInputResource;
33 import htsjdk.samtools.SamReader;
34 import htsjdk.samtools.SamReaderFactory;
35 import htsjdk.samtools.ValidationStringency;
36
37
38 public class BAMFile implements SequenceFile {
39
40 private File file;
41 private boolean onlyMapped;
42 private long fileSize = 0;
43 private long recordSize = 0;
44
45 // We keep the file stream around just so we can see how far through
46 // the file we've got. We don't read from this directly, but it's the
47 // only way to access the file pointer.
48 private FileInputStream fis;
49
50 private SamReader br;
51 private String name;
52 private Sequence nextSequence = null;
53 Iterator<SAMRecord> it;
54
55
56 protected BAMFile (File file, boolean onlyMapped) throws SequenceFormatException, IOException {
57 this.file = file;
58 fileSize = file.length();
59 name = file.getName();
60 this.onlyMapped = onlyMapped;
61
62 fis = new FileInputStream(file);
63
64 br = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(SamInputResource.of(fis));
65
66 it = br.iterator();
67 readNext();
68 }
69
70 public String name () {
71 return name;
72 }
73
74 public int getPercentComplete() {
75 if (!hasNext()) return 100;
76
77 try {
78 int percent = (int) (((double)fis.getChannel().position()/ fileSize)*100);
79 return percent;
80 }
81 catch (IOException e) {
82 e.printStackTrace();
83 }
84 return 0;
85 }
86
87 public boolean isColorspace () {
88 return false;
89 }
90
91 public boolean hasNext() {
92 return nextSequence != null;
93 }
94
95 public Sequence next () throws SequenceFormatException {
96 Sequence returnSeq = nextSequence;
97 readNext();
98 return returnSeq;
99 }
100
101 private void readNext() throws SequenceFormatException {
102
103 SAMRecord record;
104
105 while (true) {
106
107 if (!it.hasNext()) {
108 nextSequence = null;
109 try {
110 br.close();
111 fis.close();
112 }
113 catch (IOException ioe) {
114 ioe.printStackTrace();
115 }
116 return;
117 }
118
119 try {
120 record = it.next();
121 }
122 catch (SAMFormatException sfe) {
123 throw new SequenceFormatException(sfe.getMessage());
124 }
125
126 // We skip over entries with no mapping if that's what the user asked for
127 if (onlyMapped && record.getReadUnmappedFlag()) {
128 continue;
129 }
130 else {
131 break;
132 }
133 }
134
135 // This is a very rough calculation of the record size so we can approximately track progress
136 // through the file.
137 if (recordSize == 0) {
138 recordSize = (record.getReadLength()*2)+150;
139 if (br.type() != SamReader.Type.SAM_TYPE) {
140 recordSize /= 4;
141 }
142 }
143
144
145 String sequence = record.getReadString();
146 String qualities = record.getBaseQualityString();
147
148
149 // TODO: TEST THIS!!!
150 // If we're only working with mapped data then we need to exclude any regions which have been either
151 // hard or soft clipped by our aligner.
152 if (onlyMapped) {
153 List<CigarElement> elements = record.getCigar().getCigarElements();
154
155 // We need to clip the 3' end first otherwise the numbers at the 5' end won't be right.
156 if (elements.get(elements.size()-1).getOperator().equals(CigarOperator.S)) {
157 int value = elements.get(elements.size()-1).getLength();
158 sequence = sequence.substring(0,sequence.length()-value);
159 qualities = qualities.substring(0,qualities.length()-value);
160
161 }
162
163
164 if (elements.get(0).getOperator().equals(CigarOperator.S)) {
165 int value = elements.get(0).getLength();
166 sequence = sequence.substring(value);
167 qualities = qualities.substring(value);
168 }
169
170
171 }
172
173
174 // BAM/SAM files always show sequence relative to the top strand of
175 // the mapped reference so if this sequence maps to the reverse strand
176 // we need to reverse complement the sequence and reverse the qualities
177 // to get the original orientation of the read.
178 if (record.getReadNegativeStrandFlag()) {
179 sequence = reverseComplement(sequence);
180 qualities = reverse(qualities);
181 }
182
183 nextSequence = new Sequence(this, sequence, qualities, record.getReadName());
184
185 }
186
187
188 private String reverseComplement (String sequence) {
189
190 char [] letters = reverse(sequence).toUpperCase().toCharArray();
191 char [] rc = new char[letters.length];
192
193 for (int i=0;i<letters.length;i++) {
194 switch(letters[i]) {
195 case 'G': rc[i] = 'C';break;
196 case 'A': rc[i] = 'T';break;
197 case 'T': rc[i] = 'A';break;
198 case 'C': rc[i] = 'G';break;
199 default: rc[i] = letters[i];
200 }
201 }
202
203 return new String(rc);
204
205 }
206
207 private String reverse (String sequence) {
208 char [] starting = sequence.toCharArray();
209 char [] reversed = new char[starting.length];
210
211 for (int i=0;i<starting.length;i++) {
212 reversed[reversed.length-(1+i)] = starting[i];
213 }
214
215 return new String(reversed);
216 }
217
218 public File getFile() {
219 return file;
220 }
221
222 }