Skip to content

Commit

Permalink
[ENH] Properly handle empty, yet existent files. Thanks to @jpaulLD and
Browse files Browse the repository at this point in the history
  • Loading branch information
arq5x committed Dec 27, 2012
1 parent 2a16ea9 commit ca50f59
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 25 deletions.
12 changes: 8 additions & 4 deletions src/utils/bedFile/bedFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ void BedFile::Open(void) {
delete _bedStream;
_bedStream = new igzstream(bedFile.c_str(), ios::in);
}
if ( !(_bedStream->good()) ) {
cerr << "Error: The requested bed file ("
<< bedFile
<< ") could not be opened. Exiting!" << endl;
if ( _bedStream->fail() ) {
cerr << "Error: The requested file ("
<< bedFile
<< ") "
<< "could not be opened. "
<< "Error message: ("
<< strerror(errno)
<< "). Exiting!" << endl;
exit (1);
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/utils/bedFilePE/bedFilePE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ void BedFilePE::Open(void) {
_bedStream = new igzstream(bedFile.c_str(), ios::in);
}
// can we open the file?
if ( !(_bedStream->good()) ) {
cerr << "Error: The requested bed file (" << bedFile << ") could not be opened. Exiting!" << endl;
if ( _bedStream->fail() ) {
cerr << "Error: The requested BEDPE file ("
<< bedFile
<< ") "
<< "could not be opened. "
<< "Error message: ("
<< strerror(errno)
<< "). Exiting!" << endl;
exit (1);
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/utils/bedGraphFile/bedGraphFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ void BedGraphFile::Open() {
_bedGraphStream = new igzstream(bedGraphFile.c_str(), ios::in);
}
// can we open the file?
if ( !(_bedGraphStream->good()) ) {
cerr << "Error: The requested bed file (" << bedGraphFile << ") could not be opened. Exiting!" << endl;
if ( _bedGraphStream->fail() ) {
cerr << "Error: The requested file ("
<< bedGraphFile
<< ") "
<< "could not be opened. "
<< "Error message: ("
<< strerror(errno)
<< "). Exiting!" << endl;
exit (1);
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/utils/fileType/fileType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ bool isRegularFile(const string& filename) {
returns TRUE if the file has a GZIP header.
Should only be run on regular files.
*/
bool isGzipFile(istream *file) {
//see http://www.gzip.org/zlib/rfc-gzip.html#file-format

bool isGzipFile(istream *file) {
/*
11-Sep-2011:
We now only peek at the first byte and test for GZIPiness.
Expand All @@ -54,18 +52,19 @@ bool isGzipFile(istream *file) {
// unsigned char cm;
} gzip_header;

if (!file->read((char*)&gzip_header, sizeof(gzip_header))) {
/*
26-Dec-2012:
Just peek at the first byte instead of reading it so that we don't
affect the istream's failbit. This modification was wisely proposed
by John Marshall in response to Issue 30:
https://github.com/arq5x/bedtools/issues/30
*/

// Check to see if the first byte matches expectations for a
// GZIP header.
// Deatils: http://www.gzip.org/zlib/rfc-gzip.html#file-format
if (file->peek() != 0x1f)
return false;
}

if ( gzip_header.id1 == 0x1f )
// &&
// gzip_header.id2 == 0x8b
// &&
// gzip_header.cm == 8 )
{
else
return true;
}
file->putback(gzip_header.id1);
return false;
}
10 changes: 8 additions & 2 deletions src/utils/tabFile/tabFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ void TabFile::Open(void) {
delete _tabStream;
_tabStream = new igzstream(_tabFile.c_str(), ios::in);
}
if ( !(_tabStream->good()) ) {
cerr << "Error: The requested file (" << _tabFile << ") could not be opened. Exiting!" << endl;
if ( _tabStream->fail() ) {
cerr << "Error: The requested file ("
<< _tabFile
<< ") "
<< "could not be opened. "
<< "Error message: ("
<< strerror(errno)
<< "). Exiting!" << endl;
exit (1);
}
}
Expand Down
Empty file added test/general/empty.bed
Empty file.
Binary file added test/general/non-empty.bed.gz
Binary file not shown.
31 changes: 31 additions & 0 deletions test/general/test-general.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,34 @@ echo "It looks as though you have less than 3 columns at line: 1. Are you sure
check obs exp
rm obs exp


###########################################################
# Fail on non-existent files.
###########################################################
echo " general.t06...\c"
$BT merge -i idontexist.bed 2> obs
echo "Error: The requested file (idontexist.bed) could not be opened. Error message: (No such file or directory). Exiting!" > exp
check obs exp
rm obs exp


###########################################################
# Don't fail on existent, yet empty files.
###########################################################
echo " general.t07...\c"
$BT merge -i empty.bed > obs
touch exp
check obs exp
rm obs exp


###########################################################
# Process gzipped files.
###########################################################
echo " general.t08...\c"
$BT merge -i non-empty.bed.gz > obs
echo "chr1 10 21" > exp
check obs exp
rm obs exp


0 comments on commit ca50f59

Please sign in to comment.