# should start1 and end1 be the same??? # if`index_file = TRUE`, sort, compress and index file. if FALSE, a text-like svcnv file is created --- used mainly for checking purpuses. bedpe_to_svcnv = function(annotated_sv, output_prefix, index_file = TRUE, overwrite_files = FALSE){ # check paramters if(index_file){ sort_file = TRUE compress_file = TRUE } # create the json column json = with( annotated_sv, gettextf('{"dt": 5, "chrB": "%s", "posB": %i, "sample": "%s", "strandA": "%s", "strandB": "%s"}', chrom2, start2, tumour_sample_id, strand1, strand2) ) # create the svcnv table svcnv = dplyr::select(annotated_sv, chrom1, start1, end1) %>% bind_cols(json = json) # write the svcnv file if(index_file){ svcnv_file = tempfile(pattern = "svcnv_") }else{ svcnv_file = paste0(output_prefix, ".svcnv") if(!overwrite_files & file.exists(svcnv_file)){ k = gettextf("File %s already exists. Set `overwrite_files` to TRUE if you want to overwrite it.", svcnv_file) stop(k) } } write.table(svcnv, svcnv_file, quote = FALSE, sep = "\t", row.names = FALSE, col.names = FALSE) if(index_file){ # sort svcnv file sorted_svcnv_file = tempfile(pattern = "sorted_svcnv_") cmd = gettextf("sort -k1,1 -k2,2n %s > %s", svcnv_file, sorted_svcnv_file) system(cmd) unlink(svcnv_file) # compress svcnv file compressed_svcnv_file = paste0(output_prefix, "_sorted.svcnv.gz") if(!overwrite_files & file.exists(compressed_svcnv_file)){ k = gettextf("File %s already exists. Set `overwrite_files` to TRUE if you want to overwrite it.", compressed_svcnv_file) stop(k) } cmd = gettextf("bgzip %s", sorted_svcnv_file) system(cmd) cmd = paste0(sorted_svcnv_file, ".gz") %>% gettextf("mv %s %s", ., compressed_svcnv_file) system(cmd) unlink(sorted_svcnv_file) # index svcnv file index_svcnv_file = sub("\\.gz$", ".tbi", compressed_svcnv_file) if(!overwrite_files & file.exists(index_svcnv_file)){ k = gettextf("File %s already exists. Set `overwrite_files` to TRUE if you want to overwrite it.", index_svcnv_file) stop(k) } cmd = gettextf("tabix -p bed %s", compressed_svcnv_file) system(cmd) } }