Skip to content

Commit 1380532

Browse files
committed
We can detecte and open bam file
1 parent a6cfd14 commit 1380532

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

FastQt.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ QMAKE_CXXFLAGS += -std=c++11
1919
unix {
2020
# COMPILE HTSLIB
2121
mytarget.target = $$PWD/htslib/libhts.so
22-
mytarget.commands = cd $$PWD/htslib; make -j4
22+
mytarget.commands = cd $$PWD/htslib; make -j 4
2323
mytarget_clean.commands = cd $$PWD/htslib; make clean
2424
QMAKE_EXTRA_TARGETS += mytarget
2525
PRE_TARGETDEPS += $$PWD/htslib/libhts.so

analysis/analysisrunner.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ void AnalysisRunner::run()
6464
QIODevice * file = Q_NULLPTR;
6565

6666
file = new QFile(mFilename);
67-
if (is_gz(file))
67+
bool ubam = false;
68+
if (is_ubam(file))
69+
{
70+
file = new QFile(mFilename);
71+
ubam = true;
72+
}
73+
else if (is_gz(file))
6874
{
6975
file = new KCompressionDevice(mFilename, KCompressionDevice::GZip);
7076
if (!is_fastq(file))
@@ -105,26 +111,34 @@ void AnalysisRunner::run()
105111
mSequenceCount = 0;
106112
mProgression = 0;
107113

108-
FastqReader reader(file);
114+
AbstractSequenceReader* reader = nullptr;
115+
if (ubam)
116+
{
117+
reader = new BamReader(static_cast<QFile*>(file));
118+
}
119+
else
120+
{
121+
reader = new FastqReader(file);
122+
}
109123
mStartTime.start();
110124

111125
// pre compute total size for sequencial access .
112126
//emitUpdate(tr("Analysis ..."));
113-
reader.computeTotalSize();
127+
reader->computeTotalSize();
114128

115129

116130

117131
for (Analysis * a : mAnalysisHash)
118132
a->before();
119133

120134

121-
while (reader.next() && !mCancel)
135+
while (reader->next() && !mCancel)
122136
{
123137

124138
// check if first sequence is valid..Means it's probably a good file
125139
if (mSequenceCount == 0)
126140
{
127-
if (!reader.sequence().isValid())
141+
if (!reader->sequence().isValid())
128142
{
129143
qCritical()<<Q_FUNC_INFO<<"Cannot read sequence. Are you sure it's a Fastq file ?";
130144
setStatus(Canceled);
@@ -137,7 +151,7 @@ void AnalysisRunner::run()
137151
// this is critcal and can decrease the speed. Send message only 1 sequence / 1000
138152
if (mSequenceCount % 1000 == 0)
139153
{
140-
int percentNow = reader.percentComplete();
154+
int percentNow = reader->percentComplete();
141155
// if percentNow is still null, return empty percent ...
142156
if ( (percentNow >= mProgression + 5) || (percentNow == 0))
143157
{
@@ -149,7 +163,7 @@ void AnalysisRunner::run()
149163

150164
for (Analysis * a : mAnalysisHash)
151165
{
152-
a->processSequence(reader.sequence());
166+
a->processSequence(reader->sequence());
153167
}
154168
}
155169

@@ -163,6 +177,7 @@ void AnalysisRunner::run()
163177

164178
mDuration = mStartTime.elapsed();
165179

180+
delete reader;
166181
}
167182

168183
else

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
Copyright Copyright 2016-17 Sacha Schutz
34
@@ -29,7 +30,7 @@ Copyright Copyright 2016-17 Sacha Schutz
2930
#include "cliparser.h"
3031
#include "maincli.h"
3132

32-
#include "bamreader.h"
33+
#include "htslib/hfile.h"
3334

3435
int main(int argc, char *argv[])
3536
{

sequence/bamreader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class BamReader : public AbstractSequenceReader
88
{
99
public:
10-
BamReader(QFile * device);
10+
BamReader(QFile *device);
1111
bool next() Q_DECL_OVERRIDE;
1212

1313

utils/format_detection.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,34 @@ bool is_fastq(QIODevice* file)
113113
return false;
114114
}
115115
}
116+
117+
bool is_ubam(QIODevice* file)
118+
{
119+
/*Check is a gzip file*/
120+
if(is_gz(file))
121+
{
122+
if (file->open(QIODevice::ReadOnly))
123+
{
124+
/*Check is a bgzp file*/
125+
/*We use https://samtools.github.io/hts-specs/SAMv1.pdf page 10*/
126+
file->seek(12);
127+
QByteArray magic_number = file->read(3);
128+
file->close();
129+
130+
if(magic_number.length() != 3)
131+
return false;
132+
133+
return static_cast<unsigned char>(magic_number.at(0)) == 'B' \
134+
&& static_cast<unsigned char>(magic_number.at(1)) == 'C' \
135+
&& static_cast<unsigned char>(magic_number.at(2)) == 2;
136+
}
137+
else
138+
{
139+
return false;
140+
}
141+
}
142+
else
143+
{
144+
return false;
145+
}
146+
}

utils/format_detection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ bool is_xz(QIODevice* file);
3232

3333
bool is_fastq(QIODevice* file);
3434

35+
bool is_ubam(QIODevice* file);
36+
3537
#endif // FORMAT_DETECTION_H

0 commit comments

Comments
 (0)