Skip to content
Snippets Groups Projects
Commit f7990341 authored by Aaron's avatar Aaron
Browse files

BEDTools now supports FIFOs and named pipes. Many thanks Nate Weeks and...

BEDTools now supports FIFOs and named pipes. Many thanks Nate Weeks and Michael Hoffman for the suggestions.
parent c1811a8a
No related branches found
No related tags found
No related merge requests found
......@@ -132,39 +132,18 @@ void BedFile::Open(void) {
if (bedFile == "stdin" || bedFile == "-") {
_bedStream = &cin;
}
// New method thanks to Assaf Gordon
else if ((isGzipFile(bedFile) == false) && (isRegularFile(bedFile) == true)) {
// open an ifstream
ifstream beds(bedFile.c_str(), ios::in);
// can we open the file?
if ( !beds ) {
cerr << "Error: The requested bed file (" << bedFile << ") could not be opened. Exiting!" << endl;
exit (1);
}
else {
// if so, close it (this was just a test)
beds.close();
// now set a pointer to the stream so that we
_bedStream = new ifstream(bedFile.c_str(), ios::in);
else {
_bedStream = new ifstream(bedFile.c_str(), ios::in);
if (isGzipFile(_bedStream) == true) {
delete _bedStream;
_bedStream = new igzstream(bedFile.c_str(), ios::in);
}
}
else if ((isGzipFile(bedFile) == true) && (isRegularFile(bedFile) == true)) {
igzstream beds(bedFile.c_str(), ios::in);
if ( !beds ) {
// can we open the file?
if ( !(_bedStream->good()) ) {
cerr << "Error: The requested bed file (" << bedFile << ") could not be opened. Exiting!" << endl;
exit (1);
}
else {
// if so, close it (this was just a test)
beds.close();
// now set a pointer to the stream so that we
_bedStream = new igzstream(bedFile.c_str(), ios::in);
}
}
else {
cerr << "Error: Unexpected file type (" << bedFile << "). Exiting!" << endl;
exit(1);
}
}
......
/*****************************************************************************
fileType.cpp
fileType.cpp
(c) 2009 - Aaron Quinlan
Hall Laboratory
Department of Biochemistry and Molecular Genetics
University of Virginia
aaronquinlan@gmail.com
(c) 2009 - Aaron Quinlan
Hall Laboratory
Department of Biochemistry and Molecular Genetics
University of Virginia
aaronquinlan@gmail.com
Licensed under the GNU General Public License 2.0 license.
Licensed under the GNU General Public License 2.0 license.
******************************************************************************/
#include "fileType.h"
/*
returns TRUE if the file is a regular file:
not a pipe/device.
returns TRUE if the file is a regular file:
not a pipe/device.
This implies that the file can be opened/closed/seek'd multiple times without losing information
*/
This implies that the file can be opened/closed/seek'd multiple times without losing information
*/
bool isRegularFile(const string& filename) {
struct stat buf ;
int i;
i = stat(filename.c_str(), &buf);
if (i!=0) {
cerr << "Error: can't determine file type of '" << filename << "': " << strerror(errno) << endl;
exit(1);
}
if (S_ISREG(buf.st_mode))
return true;
return false;
struct stat buf ;
int i;
i = stat(filename.c_str(), &buf);
if (i!=0) {
cerr << "Error: can't determine file type of '" << filename << "': " << strerror(errno) << endl;
exit(1);
}
if (S_ISREG(buf.st_mode))
return true;
return false;
}
/*
returns TRUE if the file has a GZIP header.
Should only be run on regular files.
*/
bool isGzipFile(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
struct {
unsigned char id1;
unsigned char id2;
unsigned char cm;
} gzip_header;
ifstream f(filename.c_str(), ios::in|ios::binary);
if (!f)
return false;
if (!f.read((char*)&gzip_header, sizeof(gzip_header)))
return false;
if ( gzip_header.id1 == 0x1f
&&
gzip_header.id2 == 0x8b
&&
gzip_header.cm == 8 )
return true;
return false;
struct {
unsigned char id1;
unsigned char id2;
unsigned char cm;
} gzip_header;
if (!file->read((char*)&gzip_header, sizeof(gzip_header)))
return false;
if ( gzip_header.id1 == 0x1f
&&
gzip_header.id2 == 0x8b
&&
gzip_header.cm == 8 )
return true;
return false;
}
......@@ -33,6 +33,6 @@ using namespace std;
******************************************************************************/
string string_error(int errnum);
bool isRegularFile(const string& filename);
bool isGzipFile(const string& filename);
bool isGzipFile(istream *file);
#endif /* FILETYPE_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment