pax_global_header00006660000000000000000000000064140012215220014477gustar00rootroot0000000000000052 comment=e2b6f8634e7527a0a75b2cd45b4c1584b19978b5 rampler-2.0.0/000077500000000000000000000000001400122152200131405ustar00rootroot00000000000000rampler-2.0.0/.gitignore000066400000000000000000000000371400122152200151300ustar00rootroot00000000000000# Compiled Object files build/ rampler-2.0.0/.gitmodules000066400000000000000000000001431400122152200153130ustar00rootroot00000000000000[submodule "vendor/bioparser"] path = vendor/bioparser url = https://github.com/rvaser/bioparser rampler-2.0.0/.travis.yml000066400000000000000000000017121400122152200152520ustar00rootroot00000000000000dist: trusty language: cpp matrix: include: - name: "GCC 4.8 (Linux)" # GCC 4.8.5 & CMake 3.9.2 os: linux addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-4.8 - cmake env: - SET_COMPILER="export CC=gcc-4.8 && export CXX=g++-4.8" - name: "Clang 4.0 (Linux)" # Clang 4.0 & CMake 3.9.2 os: linux addons: apt: sources: - llvm-toolchain-trusty-4.0 packages: - clang-4.0 - cmake env: - SET_COMPILER="export CC=clang-4.0 && export CXX=clang++-4.0" - name: "Clang Xcode 9.0 (OSX)" # Clang 9.0.0 & CMake 3.9.2 os: osx osx_image: xcode9 before_install: - eval "${SET_COMPILER}" install: - mkdir build && cd build - cmake -DCMAKE_BUILD_TYPE=Release .. && make script: - ./bin/rampler --version notifications: email: on_failure: always rampler-2.0.0/CMakeLists.txt000066400000000000000000000020221400122152200156740ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.9) project(rampler VERSION 2.0.0 LANGUAGES CXX DESCRIPTION "Rampler is a tool for subsampling or splitting FASTA/Q files.") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) if (NOT TARGET bioparser) add_subdirectory(vendor/bioparser EXCLUDE_FROM_ALL) endif () if (NOT TARGET biosoup) add_subdirectory(vendor/bioparser/vendor/biosoup EXCLUDE_FROM_ALL) endif () add_executable(${PROJECT_NAME} src/main.cpp src/sampler.cpp) target_link_libraries(${PROJECT_NAME} bioparser biosoup) target_compile_definitions(${PROJECT_NAME} PRIVATE RAMPLER_VERSION="v${PROJECT_VERSION}") include(GNUInstallDirs) install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) rampler-2.0.0/LICENSE000066400000000000000000000020551400122152200141470ustar00rootroot00000000000000MIT License Copyright (c) 2018 Robert Vaser Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rampler-2.0.0/README.md000066400000000000000000000034661400122152200144300ustar00rootroot00000000000000# Rampler [![Latest GitHub release](https://img.shields.io/github/release/rvaser/rampler.svg)](https://github.com/rvaser/rampler/releases/latest) [![Build status for c++/clang++](https://travis-ci.com/rvaser/rampler.svg?branch=master)](https://travis-ci.com/rvaser/rampler) Rampler is a standalone module for sampling genomic sequences. It supports two modes, random subsampling of sequencing data to a desired depth (given the reference length) and file splitting to desired size in bytes. ## Usage To build rampler run the following commands: ```bash git clone --recursive https://github.com/rvaser/rampler.git rampler cd rampler && mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=Release .. && make ./bin/rampler ``` which will display the following usage: ```bash usage: rampler [options ...] subsample [ ...] input file in FASTA/FASTQ format (can be compressed with gzip) integer denoting length of the reference genome (or assembly) integer denoting desired coverage of the subsampled sequences split input file in FASTA/FASTQ format (can be compressed with gzip) containing sequences which will be split into smaller chunks integer denoting the desired chunk size in bytes options: -o, --out-directory default: current directory path in which sampled files will be created --version prints the version number -h, --help prints out the help ``` #### Dependencies 1. gcc 4.8+ or clang 4.0+ 2. cmake 3.9+ 3. zlib ## Acknowledgment This work has been supported in part by Croatian Science Foundation under the project UIP-11-2013-7353. rampler-2.0.0/src/000077500000000000000000000000001400122152200137275ustar00rootroot00000000000000rampler-2.0.0/src/main.cpp000066400000000000000000000116401400122152200153610ustar00rootroot00000000000000// Copyright (c) 2021 Robert Vaser #include #include #include "bioparser/fasta_parser.hpp" #include "bioparser/fastq_parser.hpp" #include "sampler.hpp" std::atomic biosoup::Sequence::num_objects{0}; namespace { static const char* rampler_version = RAMPLER_VERSION; static struct option options[] = { {"out-directory", required_argument, nullptr, 'o'}, {"version", no_argument, nullptr, 'v'}, {"help", no_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0} }; std::unique_ptr> CreateParser( const std::string& path, std::string* name, std::string* ext) { auto is_suffix = [] (const std::string& s, const std::string& suff) { return s.size() < suff.size() ? false : s.compare(s.size() - suff.size(), suff.size(), suff) == 0; }; std::size_t c = path.rfind('/'); *name = (c == std::string::npos ? path : path.substr(c + 1)); c = name->find('.'); *ext = (c == std::string::npos ? "" : name->substr(c, name->find('.', c + 1) - c)); // NOLINT *name = (c == std::string::npos ? *name : name->substr(0, c)); if (is_suffix(path, ".fasta") || is_suffix(path, ".fa") || is_suffix(path, ".fasta.gz") || is_suffix(path, ".fa.gz")) { try { return bioparser::Parser::Create(path); // NOLINT } catch (const std::invalid_argument& exception) { std::cerr << exception.what() << std::endl; return nullptr; } } if (is_suffix(path, ".fastq") || is_suffix(path, ".fq") || is_suffix(path, ".fastq.gz") || is_suffix(path, ".fq.gz")) { try { return bioparser::Parser::Create(path); // NOLINT } catch (const std::invalid_argument& exception) { std::cerr << exception.what() << std::endl; return nullptr; } } std::cerr << "[rampler::CreateParser] error: file " << path << " has unsupported format extension (valid extensions: .fasta, " << ".fasta.gz, .fa, .fa.gz, .fastq, .fastq.gz, .fq, .fq.gz)" << std::endl; return nullptr; } void Help() { std::cout << "usage: rampler [options ...] \n" "\n" " \n" " subsample [ ...]\n" // NOLINT "\n" " \n" " input file in FASTA/FASTQ format (can be compressed with gzip)\n" " \n" " integer denoting length of the reference genome (or assembly)\n" " \n" " integer denoting desired coverage of the subsampled sequences\n" "\n" " split \n" "\n" " \n" " input file in FASTA/FASTQ format (can be compressed with gzip)\n" " \n" " integer denoting the desired chunk size in bytes\n" "\n" " options:\n" " -o, --out-directory \n" " default: current directory\n" " path in which sampled files will be created\n" " --version\n" " prints the version number\n" " -h, --help\n" " prints the usage\n"; } } // namespace int main(int argc, char** argv) { std::vector input_parameters; std::string out_directory = "."; int arg; while ((arg = getopt_long(argc, argv, "o:h", options, nullptr)) != -1) { switch (arg) { case 'o': out_directory = optarg; break; case 'v': std::cerr << rampler_version << std::endl; return 0; case 'h': Help(); return 0; default: return 1; } } if (argc == 1) { Help(); return 0; } if (optind == argc) { std::cerr << "[rampler::] error: missing arguments!" << std::endl; return 1; } for (std::int32_t i = optind; i < argc; ++i) { input_parameters.emplace_back(argv[i]); } bool subsample = false, split = false; if (input_parameters[0] == "subsample") { subsample = true; } else if (input_parameters[0] == "split") { split = true; } else { std::cerr << "[rampler::] error: unknown mode!" << std::endl; return 1; } if ((subsample && input_parameters.size() < 4) || (split && input_parameters.size() < 3)) { std::cerr << "[rampler::] error: missing arguments!" << std::endl; return 1; } std::string name{}, ext{}; auto sparser = CreateParser(input_parameters[1], &name, &ext); if (sparser == nullptr) { return 1; } rampler::Sampler sampler{std::move(sparser), name, ext}; sampler.Initialize(); if (split) { sampler.Split(out_directory, atoi(input_parameters[2].c_str())); } else if (subsample) { std::uint32_t reference_length = atoi(input_parameters[2].c_str()); for (std::uint32_t i = 3; i < input_parameters.size(); ++i) { sampler.Subsample( out_directory, reference_length, atoi(input_parameters[i].c_str())); } } return 0; } rampler-2.0.0/src/sampler.cpp000066400000000000000000000065661400122152200161130ustar00rootroot00000000000000// Copyright (c) 2021 Robert Vaser #include "sampler.hpp" #include #include #include #include #include namespace rampler { constexpr uint32_t kChunkSize = 1024 * 1024 * 1024; // ~ 1GB Sampler::Sampler( std::unique_ptr> sparser, const std::string& base_name, const std::string& extension) : sparser_(std::move(sparser)), sequences_length_(0), base_name_(base_name), extension_(extension) { } void Sampler::Initialize() { if (sequences_length_ != 0) { return; } sparser_->Reset(); while (true) { auto sequences = sparser_->Parse(1ULL << 30); if (sequences.empty()) { break; } for (const auto& it : sequences) { sequences_length_ += it->data.size(); } } } void Sampler::Subsample( const std::string& out_directory, std::uint32_t reference_length, std::uint32_t coverage) { if (coverage * reference_length > sequences_length_) { std::cerr << "[rampler::Sampler::subsample] warning: " << "insufficient data for coverage of " << coverage << std::endl; return; } std::random_device r; std::mt19937 generator(r()); std::uniform_real_distribution distribution(0.0, 1.0); double ratio = (coverage * reference_length) / static_cast(sequences_length_); // NOLINT std::string out_path = out_directory + "/" + base_name_ + "_" + std::to_string(coverage) + "x" + extension_; std::ofstream ofs(out_path); if (!ofs.is_open()) { throw std::runtime_error( "[rampler::Sampler::subsample] error: unable to create file on disk!"); } sparser_->Reset(); while (true) { auto sequences = sparser_->Parse(1ULL << 30); if (sequences.empty()) { break; } for (const auto& it : sequences) { if (distribution(generator) < ratio) { if (it->quality.empty()) { ofs << ">" << it->name << std::endl << it->data << std::endl; } else { ofs << "@" << it->name << std::endl << it->data << std::endl << "+" << std::endl << it->quality << std::endl; } } } } ofs.close(); } void Sampler::Split(const std::string& out_directory, std::uint32_t chunk_size) { // NOLINT if (chunk_size > sequences_length_) { std::cerr << "[rampler::Sampler::split] warning: " << "insufficient data for chunk size " << chunk_size << std::endl; return; } uint32_t chunk_number = 0; sparser_->Reset(); while (true) { auto sequences = sparser_->Parse(chunk_size); if (sequences.empty()) { break; } std::string out_path = out_directory + "/" + base_name_ + "_" + std::to_string(chunk_number++) + extension_; std::ofstream ofs(out_path); if (!ofs.is_open()) { throw std::runtime_error( "[rampler::Sampler::subsample] error: unable to create file on disk!"); // NOLINT } for (const auto& it : sequences) { if (it->quality.empty()) { ofs << ">" << it->name << std::endl << it->data << std::endl; } else { ofs << "@" << it->name << std::endl << it->data << std::endl << "+" << std::endl << it->quality << std::endl; } } ofs.close(); } } } // namespace rampler rampler-2.0.0/src/sampler.hpp000066400000000000000000000022051400122152200161020ustar00rootroot00000000000000// Copyright (c) 2021 Robert Vaser #ifndef RAMPLER_SAMPLER_HPP_ #define RAMPLER_SAMPLER_HPP_ #include #include #include #include #include "bioparser/parser.hpp" #include "biosoup/sequence.hpp" namespace rampler { class Sampler; std::unique_ptr createSampler(const std::string& sequences_path); class Sampler { public: Sampler( std::unique_ptr> sparser, const std::string& base_name, const std::string& extension); Sampler(const Sampler&) = delete; Sampler& operator=(const Sampler&) = delete; Sampler(Sampler&&) = delete; Sampler& operator=(Sampler&&) = delete; ~Sampler() = default; void Initialize(); void Subsample( const std::string& out_directory, std::uint32_t reference_length, std::uint32_t coverage); void Split(const std::string& out_directory, std::uint32_t chunk_size); private: std::unique_ptr> sparser_; std::uint64_t sequences_length_; std::string base_name_; std::string extension_; }; } // namespace rampler #endif // RAMPLER_SAMPLER_HPP_ rampler-2.0.0/vendor/000077500000000000000000000000001400122152200144355ustar00rootroot00000000000000rampler-2.0.0/vendor/bioparser/000077500000000000000000000000001400122152200164235ustar00rootroot00000000000000