Skip to contents

This is the main MetaScope target library mapping function, using Rsubread and multiple libraries. Aligns to each library separately, filters unmapped reads from each file, and then merges and sorts the .bam files from each library into one output file. If desired, output can be passed to `filter_host()` to remove reads that also map to filter library genomes.

Usage

align_target(
  read1,
  read2 = NULL,
  lib_dir = NULL,
  libs,
  threads = 1,
  align_file = tools::file_path_sans_ext(read1),
  subread_options = align_details,
  quiet = TRUE
)

Arguments

read1

Path to the .fastq file to align.

read2

Optional: Location of the mate pair .fastq file to align.

lib_dir

Path to the index files for all libraries.

libs

A vector of character strings giving the basenames of the Subread index files for alignment. If ALL indices to be used are located in the current working directory, set lib_dir = NULL. Default is lib_dir = NULL.

threads

The number of threads that can be utilized by the function. Default is 1 thread.

align_file

The basename of the output alignment file (without trailing .bam extension).

subread_options

A named list specifying alignment parameters for the Rsubread::align() function, which is called inside align_target(). Elements should include type, nthreads, maxMismatches, nsubreads, phredOffset, unique, and nBestLocations. Descriptions of these parameters are available under ?Rsubread::align. Defaults to the global align_details object.

quiet

Turns off most messages. Default is TRUE.

Value

This function writes a merged and sorted .bam file after aligning to all reference libraries given, along with a summary report file, to the user's working directory. The function also outputs the new .bam filename.

Examples

#### Align example reads to an example reference library using Rsubread
# \donttest{
## Create temporary directory
target_ref_temp <- tempfile()
dir.create(target_ref_temp)

## Download genome
tax <- "Ovine atadenovirus D"
all_ref <- MetaScope::download_refseq(tax,
                                      reference = FALSE,
                                      representative = FALSE,
                                      compress = TRUE,
                                      out_dir = target_ref_temp,
                                      caching = TRUE)
#> No ENTREZ API key provided
#>  Get one via taxize::use_entrez()
#> See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
#> No ENTREZ API key provided
#>  Get one via taxize::use_entrez()
#> See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
#> Error in value[[3L]](cond) : NCBI request not granted
#> No ENTREZ API key provided
#>  Get one via taxize::use_entrez()
#> See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
#> No ENTREZ API key provided
#>  Get one via taxize::use_entrez()
#> See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
#> Error in value[[3L]](cond) : NCBI request not granted
#> No ENTREZ API key provided
#>  Get one via taxize::use_entrez()
#> See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
#> No ENTREZ API key provided
#>  Get one via taxize::use_entrez()
#> See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
#> Error in value[[3L]](cond) : NCBI request not granted
#> Error in MetaScope::download_refseq(tax, reference = FALSE, representative = FALSE,     compress = TRUE, out_dir = target_ref_temp, caching = TRUE): Process halted. Your input is not a valid taxon

## Create subread index
ind_out <- mk_subread_index(all_ref)
#> Error: object 'all_ref' not found

## Get path to example reads
readPath <- system.file("extdata", "reads.fastq",
                        package = "MetaScope")
## Copy the example reads to the temp directory
refPath <- file.path(target_ref_temp, "reads.fastq")
file.copy(from = readPath, to = refPath)
#> [1] TRUE

## Modify alignment parameters object
data("align_details")
align_details[["type"]] <- "rna"
align_details[["phredOffset"]] <- 50
# Just to get it to align - toy example!
align_details[["maxMismatches"]] <- 100

## Run alignment
target_map <- align_target(refPath,
                           libs = stringr::str_replace_all(tax, " ", "_"),
                           lib_dir = target_ref_temp,
                           subread_options = align_details)
#> 
#>         ==========     _____ _    _ ____  _____  ______          _____  
#>         =====         / ____| |  | |  _ \|  __ \|  ____|   /\   |  __ \ 
#>           =====      | (___ | |  | | |_) | |__) | |__     /  \  | |  | |
#>             ====      \___ \| |  | |  _ <|  _  /|  __|   / /\ \ | |  | |
#>               ====    ____) | |__| | |_) | | \ \| |____ / ____ \| |__| |
#>         ==========   |_____/ \____/|____/|_|  \_\______/_/    \_\_____/
#>        Rsubread 2.18.0
#> 
#> //================================= setting ==================================\\
#> ||                                                                            ||
#> || Function      : Read alignment (RNA-Seq)                                   ||
#> || Input file    : reads.fastq                                                ||
#> || Output file   : reads.Ovine_atadenovirus_D.bam (BAM)                       ||
#> || Index name    : Ovine_atadenovirus_D                                       ||
#> ||                                                                            ||
#> ||                    ------------------------------------                    ||
#> ||                                                                            ||
#> ||                               Threads : 1                                  ||
#> ||                          Phred offset : 64                                 ||
#> ||                             Min votes : 3 / 10                             ||
#> ||                        Max mismatches : 100                                ||
#> ||                      Max indel length : 5                                  ||
#> ||            Report multi-mapping reads : yes                                ||
#> || Max alignments per multi-mapping read : 16                                 ||
#> ||                                                                            ||
#> \\============================================================================//
#> 
#> //=============== Running (30-Oct-2024 12:30:16, pid=3904026) ================\\
#> ||                                                                            ||
#> || Check the input reads.                                                     ||
#> || The input file contains base space reads.                                  ||
#> || Initialise the memory objects.                                             ||
#> || Estimate the mean read length.                                             ||
#> || The range of Phred scores observed in the data is [-14,-14]                ||
#> || Create the output BAM file.                                                ||
#> || Check the index.                                                           ||
#> Unable top open index '/scratch/290807.1.ood/RtmpsBpExV/file3b921a57b4e677/Ovine_atadenovirus_D'. Please make sure that the correct prefix is specified and you have the permission to read these files. For example, if there are files '/opt/my_index.reads', '/opt/my_index.files' and etc, the index prefix should be specified as '/opt/my_index' without any suffix. 
#> 
#> Error in .stop_quietly(): 

## Remove temporary folder
unlink(target_ref_temp, recursive = TRUE)
# }