Learn R Programming

rbamtools (version 2.16.17)

bamReader-class: Class "bamReader": Representation for file connection to BAM file for reading access.

Description

A bamReader object encapulates functionality for reading of bamAligns from a BAM-file. It optionally contains a pointer to a bam-index structure which allows fast access to aligns that overlap a specified region (random access). The index is loaded via the loadIndex function.

Arguments

Objects from the Class

Objects can be created by calls of the form reader<-bamReader(filename).

Slots

filename:

Character denoting name of BAM-file.

%
reader:

External pointer to opened BAM-file.

%
startpos:

Numeric value returned by bam_tell immediately after opening BAM-file. It is used as target position for rewinding by bam_seek.

%
index:

External pointer to BAM index (used for retrieving bamRange objects from BAM-file.

Methods

bamClose

signature(object = "bamReader"): Closes connection to BAM-file.

%
bamCopy

signature(object = "bamReader", writer="bamWriter",refids,verbose): Copies all aligns for given refids from reader to writer. When no refids are given, aligns for all refids are copied. refids refer to Reference-ID's returned by 'getRefData' (ID column). Missing matches (from refids into Reference-ID's) will terminate the function with an error.

%
bamSave

signature(object = "bamReader", writer="bamWriter"): Copies all aligns from reader to writer.

%
bamSort

signature(object = "bamReader", prefix="character", byName=FALSE, maxmem=1e+9, path=dirname(filename(object))): Sorting an existing BAM-file.

%
createIndex

signature(object = "bamReader", idx_filename="character"): Creates an index file for opened BAM-file. Therefore the BAM-file must be position-sorted. idx_filename will be the name of the new BAM-index file. idx_filename is an optional argument. The standard value is "bam_filename.bam".bai.

%
loadIndex

signature(object="bamReader", filename="character"): Loads index from given index file. A loaded index is a prerequisite for random access to a BAM file.

%
indexInitialized

signature(object="bamReader"): Returns TRUE when a loaded BAM index is present in bamReader.

% %
filename

signature(object = "bamReader"): Returns filename of opened BAM-file.

%
getHeader

signature(object = "bamReader"): Returns object of class bamHeader which contains binary representation of bam-header data.

%
getHeaderText

signature(object = "bamReader"): Returns object of class bamHeaderText which contains textual representation of bam-header data.

%
getNextAlign

signature(object = "bamReader"): Returns object of class bamAlign which contains data of next Align from file. When EOF is reached the function returns NULL.

%
getRefCoords

signature(object="bamReader", sn="character"): Helper function takes a sequence name and returns coordinates of entire reference sequence for usage with bamRange, gapList or siteList function. The function returns a vector of length 3. The vector elements are named "refid","start","stop".

%
getRefCount

signature(object = "bamReader"): Returns number of reference sequences.

%
getRefData

signature(object = "bamReader"): Returns data frame which contains three columns: For each reference sequence, the corresponding row contains the Reference-ID (1st column, refID), the Reference name (2nd column, refName) and the length of the Reference sequence (3rd column, refLength).

%
isOpen

signature(object = "bamReader"): Returns TRUE when file connection is open.

%
rangeSegCount

signature(object = "bamReader", coords="numeric", segments="numeric", complex="logical"): Counts alignments for specified genomic segment regions (genes, exons, ...)

%
rewind

signature(object = "bamReader"): Resets current file position. The subsequent call to getNextAlign will return the first align in the BAM-file.

%
initialize

signature(.Object = "bamReader"): Initializes object and opens BAM-file for reading

References

The SAM Format Specification (v1.4-r985). The SAM Format Specification Working Group. September 7, 2011.

Examples

Run this code
# NOT RUN {
bam<- system.file("extdata", "accepted_hits.bam", package="rbamtools")
idx<- system.file("extdata", "accepted_hits.bam.bai", package="rbamtools")
#
# Open BAM-file for reading
reader<-bamReader(bam)
getNextAlign(reader)
rewind(reader)
getNextAlign(reader)
isOpen(reader)
#
# Create and load new index
# }
# NOT RUN {
new_idx<-"index.bam.bai"
createIndex(reader,new_idx)
loadIndex(reader,new_idx)
indexInitialized(reader)
# }
# NOT RUN {
#
# Load package provided index
loadIndex(reader,idx)
indexInitialized(reader)
#
# Read align
align<-getNextAlign(reader)
coords<-as.integer(c(0,0,249250621))
range<-bamRange(reader,coords)
align<-getNextAlign(range)
#
# Copy all aligns
# }
# NOT RUN {
writer<-bamWriter(getHeader(reader),"newFile1.bam")
bamSave(reader,writer)
bamClose(writer)
# }
# NOT RUN {
#
# reader2<-bamReader("newFile1.bam")
# }
# NOT RUN {
bamSort(reader,"newFile1s")
# Copy align for Reference-ID '0'
writer<-bamWriter(getHeader(reader),"newFile2.bam")
bamCopy(reader,writer,refid=0)
bamClose(writer)
# }
# NOT RUN {
#
# Closing reader
bamClose(reader)
# }

Run the code above in your browser using DataLab