tmp/samtoolsSort.R

samtools <- 'samtools'
threads <- 4
output_path <- '/media/msubirana/plab1/resorted/'
bam_path <- '.'
bams <- list.files(bam_path,
           pattern = '\\.bam$',
           full.names = T)


for(bam in bams){

  index <- paste0(bam, '.bai')

  if(!file.exists(index)){

    system(paste(samtools,
                 'index',
                 '-@', threads,
                 bam))
  }

  sorted_bam <- file.path(output_path, basename(bam))

  system(paste(samtools,
               'sort',
               '-@', threads,
               bam,
               '-O BAM -o', sorted_bam,
               ';', samtools,
               'index',
               '-@', threads,
               sorted_bam))
}
msubirana/ergWgsTools documentation built on June 8, 2020, 8:07 a.m.