Difference between revisions of "C++ Class: InputFile"

From Genome Analysis Wiki
Jump to navigationJump to search
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Category:C++]]
 +
[[Category:libStatGen]]
 +
[[Category:libStatGen general]]
 +
 
= InputFile / IFILE =
 
= InputFile / IFILE =
This is our class for file operations.  It hides the underlying file structure from the user.  That way code can generically open and operate on a file using the exact same interface without having to know if the file is standard, gzip, or bgzf format (at least for reading - for writing the user has to specify which type to open).
+
This is our class for file operations.  It hides the underlying file structure from the user.  That way code can generically open and operate on a file using the exact same interface without having to know if the file is standard, gzip, or bgzf format (for reading from a file - for reading from stdin and writing, the user has to specify which type to open).
 +
 
 +
The IFILE class is made to mimic FILE.  THe typical way to use IFILE is to call the set of global methods contained in InputFile.h that take IFILE as a parameter
  
 +
To use IFILE, add the following include to your file.
 +
<source lang="cpp">
 +
#include "InputFile.h"
 +
</source>
  
=== Public Class Methods ===
+
=== Global IFILE Methods ===
{| style="margin: 1em 1em 1em 0; background-color: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;" border="1"
 
|-style="background: #f2f2f2; text-align: center;"
 
! Method Name !!  Description
 
|-
 
| ***NEEDS TO BE FILLED IN***
 
| ***NEEDS TO BE FILLED IN***
 
|}
 
  
=== Class Enums ===
+
See: http://csg.sph.umich.edu//mktrost/doxygen/current/InputFile_8h.html for documentation on the global IFILE methods.
{| style="margin: 1em 1em 1em 0; background-color: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;" border="1"
 
|-style="background: #f2f2f2; text-align: center;"
 
  
! Enum Value !!  Description
+
=== InputFile Class Documentation ===
|-
 
! colspan="2"| enum ifileCompression
 
|-
 
| DEFAULT
 
| Use the default method for determining file type.
 
Opens as UNCOMPRESSED unless the filename ends in ".gz", then opened as GZIP
 
|-
 
| UNCOMPRESSED
 
| Standard Uncompressed File Type.
 
|-
 
| GZIP
 
| Gzip File Type.
 
|-
 
| BGZF
 
| bgzf File Type.
 
|}
 
  
 +
See: http://csg.sph.umich.edu//mktrost/doxygen/current/classInputFile.html
  
 
=== BGZF Notes ===
 
=== BGZF Notes ===
Line 41: Line 27:
 
   BgzfFileType::setRequireEofBlock(false);
 
   BgzfFileType::setRequireEofBlock(false);
 
With that call, the empty block is not checked for when opening the file.
 
With that call, the empty block is not checked for when opening the file.
 +
 +
 +
=== stdin/stdout ===
 +
This class can be used to read/write to stdin/stdout.
 +
 +
To use stdin/stdout, specify the filename as "-" and use the ifileCompression parameter in ifopen to specify the compression type. 
 +
 +
NOTE: even when reading from stdin, you MUST specify the file type.  It does not read from stdin to determine the compression type like it does for other files.
 +
 +
'''When writing programs that will use stdout and pipes to send input from one program to the next, make sure that all error, debug, or info messages are written to stderr/cerr and not stdout/cout.'''
 +
 +
=== Example ===
 +
<source lang="cpp">
 +
// specify to open stdin for read.
 +
// replace the - with a filename in order to read from a file.
 +
const char* filename = "-";
 +
IFILE myFilePtr = ifopen(filename, "rb", InputFile::BGZF);
 +
if (myFilePtr == NULL)
 +
{
 +
    std::cerr << "Failed to open the file\n";
 +
}
 +
else
 +
{
 +
    // File was successfully opened.
 +
    // Read the magic string.
 +
    char magic[4];
 +
    if(ifread(myFilePtr, magic, 4) != 4)
 +
    {
 +
      std::cerr << "Could not read 4 bytes from the file\n";
 +
    }
 +
    ifclose(myFilePtr);
 +
    myFilePtr = NULL;
 +
}
 +
</source>

Latest revision as of 10:47, 2 February 2017


InputFile / IFILE

This is our class for file operations. It hides the underlying file structure from the user. That way code can generically open and operate on a file using the exact same interface without having to know if the file is standard, gzip, or bgzf format (for reading from a file - for reading from stdin and writing, the user has to specify which type to open).

The IFILE class is made to mimic FILE. THe typical way to use IFILE is to call the set of global methods contained in InputFile.h that take IFILE as a parameter

To use IFILE, add the following include to your file.

#include "InputFile.h"

Global IFILE Methods

See: http://csg.sph.umich.edu//mktrost/doxygen/current/InputFile_8h.html for documentation on the global IFILE methods.

InputFile Class Documentation

See: http://csg.sph.umich.edu//mktrost/doxygen/current/classInputFile.html

BGZF Notes

Newer BGZF files have a empty BGZF block at the end to mark the EOF. By default when opening BGZF Files for reading, the software requires the empty block and the fail opens if it is not there. To support files without the empty block, the following call must first be made:

 BgzfFileType::setRequireEofBlock(false);

With that call, the empty block is not checked for when opening the file.


stdin/stdout

This class can be used to read/write to stdin/stdout.

To use stdin/stdout, specify the filename as "-" and use the ifileCompression parameter in ifopen to specify the compression type.

NOTE: even when reading from stdin, you MUST specify the file type. It does not read from stdin to determine the compression type like it does for other files.

When writing programs that will use stdout and pipes to send input from one program to the next, make sure that all error, debug, or info messages are written to stderr/cerr and not stdout/cout.

Example

 // specify to open stdin for read.
 // replace the - with a filename in order to read from a file.
 const char* filename = "-";
 IFILE myFilePtr = ifopen(filename, "rb", InputFile::BGZF);
 if (myFilePtr == NULL)
 {
     std::cerr << "Failed to open the file\n";
 }
 else
 {
    // File was successfully opened.
    // Read the magic string.
    char magic[4];
    if(ifread(myFilePtr, magic, 4) != 4)
    {
       std::cerr << "Could not read 4 bytes from the file\n";
    }
    ifclose(myFilePtr);
    myFilePtr = NULL;
 }