pax_global_header00006660000000000000000000000064141677703240014525gustar00rootroot0000000000000052 comment=856abd0f2c808c51eb47d176f40b857068902999 rampler-2.1.1/000077500000000000000000000000001416777032400131705ustar00rootroot00000000000000rampler-2.1.1/.github/000077500000000000000000000000001416777032400145305ustar00rootroot00000000000000rampler-2.1.1/.github/workflows/000077500000000000000000000000001416777032400165655ustar00rootroot00000000000000rampler-2.1.1/.github/workflows/rampler.yml000066400000000000000000000020731416777032400207540ustar00rootroot00000000000000name: rampler CI on: push: pull_request: branches: - master env: BUILD_TYPE: Release jobs: test: strategy: matrix: compiler: - g++ - g++-4.8 - clang++ - clang++-4.0 runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v2 - if: ${{ matrix.compiler == 'g++-4.8' }} name: Setup GCC uses: egor-tensin/setup-gcc@v1 with: version: "4.8" platform: x64 - if: ${{ matrix.compiler == 'clang++-4.0' }} name: Setup Clang uses: egor-tensin/setup-clang@v1 with: version: "4.0" platform: x64 - name: Configure CMake run: cmake -B ${{ github.workspace }}/build -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} env: CXX: ${{ matrix.compiler }} - name: Build run: cmake --build ${{ github.workspace }}/build --config ${{ env.BUILD_TYPE }} - name: Test working-directory: ${{ github.workspace }}/build run: | bin/rampler --version rampler-2.1.1/.gitignore000066400000000000000000000000371416777032400151600ustar00rootroot00000000000000# Compiled Object files build/ rampler-2.1.1/.gitmodules000066400000000000000000000001431416777032400153430ustar00rootroot00000000000000[submodule "vendor/bioparser"] path = vendor/bioparser url = https://github.com/rvaser/bioparser rampler-2.1.1/CMakeLists.txt000066400000000000000000000031711416777032400157320ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.11) project(rampler VERSION 2.1.1 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) include(FetchContent) include(GNUInstallDirs) find_package(bioparser 3.0.15 QUIET) if (NOT bioparser_FOUND) FetchContent_Declare( bioparser GIT_REPOSITORY https://github.com/rvaser/bioparser GIT_TAG 3.0.15) FetchContent_GetProperties(bioparser) if (NOT bioparser_POPULATED) FetchContent_Populate(bioparser) add_subdirectory( ${bioparser_SOURCE_DIR} ${bioparser_BINARY_DIR} EXCLUDE_FROM_ALL) endif () endif () find_package(biosoup 0.10.0 QUIET) if (NOT biosoup_FOUND) FetchContent_Declare( biosoup GIT_REPOSITORY https://github.com/rvaser/biosoup GIT_TAG 0.10.0) FetchContent_GetProperties(biosoup) if (NOT biosoup_POPULATED) FetchContent_Populate(biosoup) add_subdirectory( ${biosoup_SOURCE_DIR} ${biosoup_BINARY_DIR} EXCLUDE_FROM_ALL) endif () endif () add_executable(rampler src/main.cpp src/sampler.cpp) target_link_libraries(rampler bioparser::bioparser biosoup::biosoup) target_compile_definitions(rampler PRIVATE VERSION="${PROJECT_VERSION}") install(TARGETS rampler DESTINATION ${CMAKE_INSTALL_BINDIR}) rampler-2.1.1/LICENSE000066400000000000000000000020551416777032400141770ustar00rootroot00000000000000MIT 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.1.1/README.md000066400000000000000000000034661416777032400144600ustar00rootroot00000000000000# 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.1.1/src/000077500000000000000000000000001416777032400137575ustar00rootroot00000000000000rampler-2.1.1/src/main.cpp000066400000000000000000000114741416777032400154160ustar00rootroot00000000000000// 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 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('.'); *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")) { *ext = ".fasta"; 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")) { *ext = ".fastq"; 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 << 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, std::atoll(input_parameters[2].c_str())); } else if (subsample) { std::uint64_t reference_length = std::atoll(input_parameters[2].c_str()); for (std::uint32_t i = 3; i < input_parameters.size(); ++i) { sampler.Subsample( out_directory, reference_length, std::atoll(input_parameters[i].c_str())); } } return 0; } rampler-2.1.1/src/sampler.cpp000066400000000000000000000064721416777032400161370ustar00rootroot00000000000000// Copyright (c) 2021 Robert Vaser #include "sampler.hpp" #include #include #include #include #include namespace rampler { 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::uint64_t reference_length, std::uint64_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::uint64_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; } std::size_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.1.1/src/sampler.hpp000066400000000000000000000022051416777032400161320ustar00rootroot00000000000000// 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::uint64_t reference_length, std::uint64_t coverage); void Split(const std::string& out_directory, std::uint64_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.1.1/vendor/000077500000000000000000000000001416777032400144655ustar00rootroot00000000000000rampler-2.1.1/vendor/bioparser/000077500000000000000000000000001416777032400164535ustar00rootroot00000000000000