Tabix Class Reference
Inheritance diagram for Tabix:
[legend]
Collaboration diagram for Tabix:
[legend]

## Public Types

enum | Format { FORMAT_GENERIC = 0 , FORMAT_SAM = 1 , FORMAT_VCF = 2 }

## Public Member Functions

void | resetIndex ()
| Reset the member data for a new index file.
StatGenStatus::Status | readIndex (const char *filename)
bool | getStartPos (const char *refName, int32_t start, uint64_t &fileStartPos) const
| Get the starting file offset to look for the specified start position. More...
const char * | getRefName (unsigned int indexNum) const
| Return the reference name at the specified index or throws an exception if out of range.
int32_t | getFormat () const
Public Member Functions inherited from IndexBase
int32_t | getNumRefs () const
| Get the number of references in this index. More...
bool | getMinOffsetFromLinearIndex (int32_t refID, uint32_t position, uint64_t &minOffset) const

## Additional Inherited Members

Static Protected Member Functions inherited from IndexBase
static void | getBinsForRegion (uint32_t start, uint32_t end, bool binMap[MAX_NUM_BINS+1])
Protected Attributes inherited from IndexBase
int32_t | n_ref
std::vector< Reference > | myRefs
Static Protected Attributes inherited from IndexBase
static const uint32_t | MAX_NUM_BINS = 37450
static const uint32_t | MAX_POSITION = 536870911
static const uint32_t | LINEAR_INDEX_SHIFT = 14

## Detailed Description

Definition at line 31 of file Tabix.h.

## Member Function Documentation

## getStartPos()

bool Tabix::getStartPos | ( | const char * | refName,
|  | int32_t | start,
|  | uint64_t & | fileStartPos
| ) |  | const

Get the starting file offset to look for the specified start position.

For an entire reference ID, set start to -1. To start at the beginning of the region, set start to 0/-1.

Definition at line 218 of file Tabix.cpp.

{
// Look for the reference name in the list.
int refID = 0;
for(refID = 0; refID < n_ref; refID++)
{
if(strcmp(refName, myChromNamesVector[refID]) == 0)
{
// found the reference
break;
}
}
if(refID >= n_ref)
{
// Didn't find the refName, so return false.
return(false);
}
// Look up in the linear index.
if(start < 0)
{
// Negative index, so start at 0.
start = 0;
}
return(getMinOffsetFromLinearIndex(refID, start, fileStartPos));
}

## readIndex()

StatGenStatus::Status Tabix::readIndex | ( | const char * | filename | ) |

virtual

Parameters

  filename | the bam index file to be read.

Returns
  the status of the read.

Implements IndexBase.

Definition at line 52 of file Tabix.cpp.

{
// Reset the index from anything that may previously be set.
resetIndex();
IFILE indexFile = ifopen(filename, "rb");
// Failed to open the index file.
if(indexFile == NULL)
{
return(StatGenStatus::FAIL_IO);
}
// read the tabix index structure.
// Read the magic string.
char magic[4];
if(ifread(indexFile, magic, 4) != 4)
{
// Failed to read the magic
return(StatGenStatus::FAIL_IO);
}
// If this is not an index file, set num references to 0.
if (magic[0] != 'T' || magic[1] != 'B' || magic[2] != 'I' || magic[3] != 1)
{
// Not a Tabix Index file.
return(StatGenStatus::FAIL_PARSE);
}
// It is a tabix index file.
// Read the number of reference sequences.
if(ifread(indexFile, &n_ref, 4) != 4)
{
// Failed to read.
return(StatGenStatus::FAIL_IO);
}
// Size the references.
myRefs.resize(n_ref);
// Read the Format configuration.
if(ifread(indexFile, &myFormat, sizeof(myFormat)) != sizeof(myFormat))
{
// Failed to read.
return(StatGenStatus::FAIL_IO);
}
// Read the length of the chromosome names.
uint32_t l_nm;
if(ifread(indexFile, &l_nm, sizeof(l_nm)) != sizeof(l_nm))
{
// Failed to read.
return(StatGenStatus::FAIL_IO);
}
// Read the chromosome names.
myChromNamesBuffer = new char[l_nm];
if(ifread(indexFile, myChromNamesBuffer, l_nm) != l_nm)
{
return(StatGenStatus::FAIL_IO);
}
myChromNamesVector.resize(n_ref);
// Parse out the chromosome names.
bool prevNull = true;
int chromIndex = 0;
for(uint32_t i = 0; i < l_nm; i++)
{
if(chromIndex >= n_ref)
{
// already set the pointer for the last chromosome name,
// so stop looping.
break;
}
if(prevNull == true)
{
myChromNamesVector[chromIndex++] = myChromNamesBuffer + i;
prevNull = false;
}
if(myChromNamesBuffer[i] == '\0')
{
prevNull = true;
}
}
for(int refIndex = 0; refIndex < n_ref; refIndex++)
{
// Read each reference.
Reference* ref = &(myRefs[refIndex]);
// Read the number of bins.
if(ifread(indexFile, &(ref->n_bin), 4) != 4)
{
// Failed to read the number of bins.
// Return failure.
return(StatGenStatus::FAIL_PARSE);
}
// Resize the bins.
ref->bins.resize(ref->n_bin + 1);
// Read each bin.
for(int binIndex = 0; binIndex < ref->n_bin; binIndex++)
{
uint32_t binNumber;
// Read in the bin number.
if(ifread(indexFile, &(binNumber), 4) != 4)
{
// Failed to read the bin number.
// Return failure.
return(StatGenStatus::FAIL_IO);
}
// Add the bin to the reference and get the
// pointer back so the values can be set in it.
Bin* binPtr = &(ref->bins[binIndex]);
binPtr->bin = binNumber;
// Read in the number of chunks.
if(ifread(indexFile, &(binPtr->n_chunk), 4) != 4)
{
// Failed to read number of chunks.
// Return failure.
return(StatGenStatus::FAIL_IO);
}
// Read in the chunks.
// Allocate space for the chunks.
uint32_t sizeOfChunkList = binPtr->n_chunk * sizeof(Chunk);
binPtr->chunks = (Chunk*)malloc(sizeOfChunkList);
if(ifread(indexFile, binPtr->chunks, sizeOfChunkList) != sizeOfChunkList)
{
// Failed to read the chunks.
// Return failure.
return(StatGenStatus::FAIL_IO);
}
}
// Read the number of intervals.
if(ifread(indexFile, &(ref->n_intv), 4) != 4)
{
// Failed to read, set to 0.
ref->n_intv = 0;
// Return failure.
return(StatGenStatus::FAIL_IO);
}
// Allocate space for the intervals and read them.
uint32_t linearIndexSize = ref->n_intv * sizeof(uint64_t);
ref->ioffsets = (uint64_t*)malloc(linearIndexSize);
if(ifread(indexFile, ref->ioffsets, linearIndexSize) != linearIndexSize)
{
// Failed to read the linear index.
// Return failure.
return(StatGenStatus::FAIL_IO);
}
}
// Successfully read teh bam index file.
return(StatGenStatus::SUCCESS);
}
ifopen
IFILE ifopen(const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT)
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
Definition: InputFile.h:562
ifread
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition: InputFile.h:600
Chunk
Definition: IndexBase.h:31
InputFile
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:37
StatGenStatus::SUCCESS
@ SUCCESS
method completed successfully.
Definition: StatGenStatus.h:32
StatGenStatus::FAIL_IO
@ FAIL_IO
method failed due to an I/O issue.
Definition: StatGenStatus.h:37
StatGenStatus::FAIL_PARSE
@ FAIL_PARSE
failed to parse a record/header - invalid format.
Definition: StatGenStatus.h:42
Tabix::resetIndex
void resetIndex()
Reset the member data for a new index file.
Definition: Tabix.cpp:39

References StatGenStatus::FAIL_IO, StatGenStatus::FAIL_PARSE, ifopen(), ifread(), resetIndex(), and StatGenStatus::SUCCESS.

The documentation for this class was generated from the following files:

- general/Tabix.h
- general/Tabix.cpp