@@ -290,20 +290,54 @@ setMethod("readGAlignmentPairs", "character",
290290
291291setGeneric ("readGAlignmentsList ", signature="file",
292292 function (file , index = file , use.names = FALSE , param = ScanBamParam(),
293- with.which_label = FALSE )
293+ with.which_label = FALSE , strandMode = NA )
294294 standardGeneric(" readGAlignmentsList" )
295295)
296296
297- .matesFromBam <- function (file , use.names , param , what0 , with.which_label )
297+ .setRealStrand <- function (gal , param , strandMode ) {
298+ if (strandMode == 0L )
299+ strand(gal ) <- Rle(strand(" *" ), length(gal ))
300+ else {
301+ gal_mcols <- mcols(gal , use.names = FALSE )
302+ if (is.null(gal_mcols $ flag ))
303+ warning(" Flag information missing in GAlignmentsList object. Strand information might not be accurate." )
304+ else {
305+ mask_first_mate <- bamFlagTest(gal_mcols $ flag , " isFirstMateRead" )
306+ if (strandMode == 1L )
307+ strand(gal [! mask_first_mate ]) <-
308+ invertStrand(strand(gal [! mask_first_mate ]))
309+ else if (strandMode == 2L )
310+ strand(gal [mask_first_mate ]) <-
311+ invertStrand(strand(gal [mask_first_mate ]))
312+ else
313+ stop(" strandMode should be either 0, 1 or 2." )
314+ }
315+ }
316+ # # if the user didn't request the 'flag' info
317+ # # then remove it to reduce memory footprint
318+ if (! " flag" %in% bamWhat(param ))
319+ mcols(gal )$ flag <- NULL
320+ gal
321+ }
322+ .matesFromBam <- function (file , use.names , param , what0 , with.which_label ,
323+ strandMode )
298324{
299325 bamcols <- .load_bamcols_from_BamFile(file , param , what0 ,
300326 with.which_label = with.which_label )
301327 seqlengths <- .load_seqlengths_from_BamFile(file , levels(bamcols $ rname ))
302328 gal <- GAlignments(seqnames = bamcols $ rname , pos = bamcols $ pos ,
303329 cigar = bamcols $ cigar , strand = bamcols $ strand ,
304330 seqlengths = seqlengths )
305- gal <- .bindExtraData(gal , use.names = FALSE , param , bamcols ,
306- with.which_label = with.which_label )
331+ if (! is.na(strandMode )) {
332+ flag0 <- scanBamFlag()
333+ what0 <- " flag"
334+ param2 <- .normargParam(param , flag0 , what0 )
335+ gal <- .bindExtraData(gal , use.names = FALSE , param2 , bamcols ,
336+ with.which_label = with.which_label )
337+ gal <- .setRealStrand(gal , param , strandMode )
338+ } else
339+ gal <- .bindExtraData(gal , use.names = FALSE , param , bamcols ,
340+ with.which_label = with.which_label )
307341 if (asMates(file )) {
308342 f <- factor (bamcols $ groupid )
309343 gal <- unname(split(gal , f ))
@@ -320,30 +354,34 @@ setGeneric("readGAlignmentsList", signature="file",
320354
321355.readGAlignmentsList.BamFile <- function (file , index = file ,
322356 use.names = FALSE , param = ScanBamParam(),
323- with.which_label = FALSE )
357+ with.which_label = FALSE ,
358+ strandMode = NA )
324359{
325360 if (! isTRUEorFALSE(use.names ))
326361 stop(" 'use.names' must be TRUE or FALSE" )
327362 if (! asMates(file ))
328363 bamWhat(param ) <- setdiff(bamWhat(param ),
329364 c(" groupid" , " mate_status" ))
330365 what0 <- c(" rname" , " strand" , " pos" , " cigar" , " groupid" , " mate_status" )
366+ if (! is.na(strandMode ))
367+ what0 <- c(what0 , " flag" )
331368 if (use.names )
332369 what0 <- c(what0 , " qname" )
333- .matesFromBam(file , use.names , param , what0 , with.which_label )
370+ .matesFromBam(file , use.names , param , what0 , with.which_label , strandMode )
334371}
335372
336373setMethod ("readGAlignmentsList ", "BamFile", .readGAlignmentsList.BamFile)
337374
338375setMethod ("readGAlignmentsList ", "character",
339376 function (file , index = file , use.names = FALSE , param = ScanBamParam(),
340- with.which_label = FALSE )
377+ with.which_label = FALSE , strandMode = NA )
341378 {
342379 bam <- .open_BamFile(file , index = index , asMates = TRUE , param = param )
343380 on.exit(close(bam ))
344381 readGAlignmentsList(bam , character (0 ),
345382 use.names = use.names , param = param ,
346- with.which_label = with.which_label )
383+ with.which_label = with.which_label ,
384+ strandMode = strandMode )
347385 }
348386)
349387
0 commit comments