Permalink
Browse files

adding a header attribute to toSamTable function for easy SAM writing

  • Loading branch information...
1 parent d4e613f commit 4675218b2000027231b422a2765427c9e4cb9322 @lianos committed Apr 13, 2012
Showing with 11 additions and 2 deletions.
  1. +11 −2 R/pkg/R/sam.R
View
@@ -13,7 +13,8 @@
##' @param tag.prefix columns that who are destined to wind up in the tag fields
##' of the alignments must start with this prefix in order to be processed
##' correctly, eg: \code{tag.NM}, \code{tag.XA}, etc.
-toSamTable <- function(x, is.paired=FALSE, tag.prefix="tag.") {
+toSamTable <- function(x, is.paired=FALSE, tag.prefix="tag.",
+ seqlens=seqlengths(x)) {
if (!inherits(x, 'GRanges')) {
stop("Only work on GRanges objects")
}
@@ -79,13 +80,21 @@ toSamTable <- function(x, is.paired=FALSE, tag.prefix="tag.") {
sam <- data.frame(qname=as.character(meta$qname), flag=flag,
rname=as.character(seqnames(x)), pos=start(x),
mapq=mapq, cigar=cigar, mrnm=mrnm, mpos=mpos, isize=isize,
- seq=seq, qual=qual)
+ seq=seq, qual=qual, stringsAsFactors=FALSE)
sam.tags <- combineIntoSamTagsVector(meta, tag.prefix)
if (!is.null(sam.tags)) {
sam$tags <- sam.tags
}
+ if (is.numeric(seqlens)) {
+ ## "@SQ\tSN:chr2L\tLN:23011544"
+ sheader <- paste("@SQ",
+ sprintf("SN:%s", names(seqlens)),
+ sprintf("LN:%d", seqlens), sep="\t")
+ attributes(sam) <- list(header=sheader)
+ }
+
sam
}

0 comments on commit 4675218

Please sign in to comment.