File: asBam.R

package info (click to toggle)
r-bioc-rsamtools 2.22.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,488 kB
  • sloc: ansic: 4,904; cpp: 1,586; sh: 40; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
setMethod(asSam, "character",
    function(file, destination=sub("\\.bam", "", file), ..., overwrite=FALSE)
{
    file <- .normalizePath(file)
    destination <- .normalizePath(destination)
    d0 <- paste(destination, "sam", sep=".")

    if (!overwrite && file.exists(d0)) {
        msg <- sprintf("'%s' exists, '%s' is FALSE\n  %s: %s",
                       "destination", "overwrite", "destination",
                       d0)
        stop(msg)
    }

    tryCatch({
        result <- .Call(.as_bam, file, d0, FALSE)
        if (!file.exists(d0))
            stop("failed to create 'SAM' file")
    }, error=function(err) {
        msg <- sprintf("'asSam' %s\n  SAM file: '%s'\n",
                       conditionMessage(err), file)
        stop(msg)
    })
    d0
})

setMethod(asBam, "character",
    function(file, destination=sub("\\.sam(\\.gz)?", "", file), ...,
             overwrite=FALSE, indexDestination=TRUE)
{
    file <- .normalizePath(file)
    destination <- .normalizePath(destination)
    d0 <- paste(destination, "bam", sep=".")

    ofl <- tempfile()
    on.exit(unlink(ofl))
    if (!overwrite && file.exists(d0)) {
        msg <- sprintf("'%s' exists, '%s' is FALSE\n  %s: %s",
                       "destination", "overwrite", "destination",
                       d0)
        stop(msg)
    }
    tryCatch({
        result <- .Call(.as_bam, file, ofl, TRUE)
        if (!file.exists(ofl))
            stop("failed to create 'BAM' file")
        if (indexDestination) {
            destination <- sortBam(ofl, destination)
            indexBam(destination)
        } else {
            destination <- d0
            .file.rename(ofl, destination)
        }
    }, error=function(err) {
        msg <- sprintf("'asBam' %s\n  SAM file: '%s'\n",
                       conditionMessage(err), file)
        stop(msg)
    })
    destination
})