pax_global_header00006660000000000000000000000064135342515100014511gustar00rootroot0000000000000052 comment=a6dec0140878ba81da90387c77740756fefa4920 rampler-1.1.1/000077500000000000000000000000001353425151000131535ustar00rootroot00000000000000rampler-1.1.1/.gitignore000066400000000000000000000000371353425151000151430ustar00rootroot00000000000000# Compiled Object files build/ rampler-1.1.1/.gitmodules000066400000000000000000000001431353425151000153260ustar00rootroot00000000000000[submodule "vendor/bioparser"] path = vendor/bioparser url = https://github.com/rvaser/bioparser rampler-1.1.1/.travis.yml000066400000000000000000000017321353425151000152670ustar00rootroot00000000000000dist: trusty language: cpp compiler: - clang - gcc before_install: # cmake 3.2 - sudo add-apt-repository ppa:george-edison55/cmake-3.x -y # g++4.8.1 - if [ "$CXX" == "g++" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi # clang 3.4 - if [ "$CXX" == "clang++" ]; then sudo add-apt-repository -y ppa:h-rayflood/llvm; fi - sudo apt-get update -qq install: # cmake 3.2 - sudo apt-get install cmake cmake-data # g++4.8.1 - if [ "$CXX" == "g++" ]; then sudo apt-get install -qq g++-4.8; fi - if [ "$CXX" == "g++" ]; then export CXX="g++-4.8"; fi # clang 3.4 - if [ "$CXX" == "clang++" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.4; fi - if [ "$CXX" == "clang++" ]; then export CXX="clang++-3.4"; fi script: - mkdir build - cd build - cmake -DCMAKE_BUILD_TYPE=Release .. - make notifications: email: on_success: change on_failure: always rampler-1.1.1/CMakeLists.txt000066400000000000000000000011641353425151000157150ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.2) project(rampler) 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) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(rampler src/main.cpp src/sequence.cpp src/sampler.cpp) if (NOT TARGET bioparser) add_subdirectory(vendor/bioparser EXCLUDE_FROM_ALL) endif() target_link_libraries(rampler bioparser) install(TARGETS rampler DESTINATION bin) rampler-1.1.1/LICENSE000066400000000000000000000020551353425151000141620ustar00rootroot00000000000000MIT 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-1.1.1/README.md000066400000000000000000000053231353425151000144350ustar00rootroot00000000000000# 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.org/rvaser/rampler.svg?branch=master)](https://travis-ci.org/rvaser/rampler) Standalone module for sampling genomic sequences. It supports two modes, random subsampling of sequencer data to a desired depth (given the reference length) and file splitting to desired size in bytes. Rampler takes as first input argument a file in FASTA/FASTQ format which can be compressed with gzip. The rest of input parameters depend on the mode of operation. The output is stored into a file(s) which is in the same format as the input file but uncompressed. ## Dependencies 1. gcc 4.8+ or clang 3.4+ 2. cmake 3.2+ ## Installation To install 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 ``` After successful installation, an executable named `rampler` will appear in `build/bin`. Optionally, you can run `sudo make install` to install rampler executable to your machine. ***Note***: if you omitted `--recursive` from `git clone`, run `git submodule update --init --recursive` before proceeding with compilation. ## Usage Usage of rampler is as following: usage: rampler [options ...] subsample [ ...] input file in FASTA/FASTQ format (can be compressed with gzip) containing sequences to be subsampled integer denoting length of the reference genome (or assembly) from which the sequences originate 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 ## Contact information For additional information, help and bug reports please send an email to one of the following: robert.vaser@fer.hr ## Acknowledgment This work has been supported in part by Croatian Science Foundation under the project UIP-11-2013-7353. rampler-1.1.1/src/000077500000000000000000000000001353425151000137425ustar00rootroot00000000000000rampler-1.1.1/src/main.cpp000066400000000000000000000072171353425151000154010ustar00rootroot00000000000000#include #include #include #include "sequence.hpp" #include "sampler.hpp" #include "bioparser/bioparser.hpp" static const char* version = "v1.1.1"; static struct option options[] = { {"out-directory", required_argument, 0, 'o'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; void help(); int main(int argc, char** argv) { std::vector input_parameters; std::string out_directory = "."; char argument; while ((argument = getopt_long(argc, argv, "o:h", options, nullptr)) != -1) { switch (argument) { case 'o': out_directory = optarg; break; case 'v': printf("%s\n", version); exit(0); case 'h': help(); exit(0); default: exit(1); } } if (optind == argc) { fprintf(stderr, "[rampler::] error: too few arguments!\n"); help(); exit(1); } for (int32_t i = optind; i < argc; ++i) { input_parameters.emplace_back(argv[i]); } bool do_subsample = false, do_split = false; if (input_parameters[0] == "subsample") { do_subsample = true; } else if (input_parameters[0] == "split") { do_split = true; } else { fprintf(stderr, "[rampler::] error: unkown mode %s!\n", input_parameters[0].c_str()); exit(1); } if ((do_subsample && input_parameters.size() < 4) || (do_split && input_parameters.size() < 3)) { fprintf(stderr, "[rampler::] error: missing input parameter(s)!\n"); exit(1); } auto sampler = rampler::createSampler(input_parameters[1]); sampler->initialize(); if (do_split) { sampler->split(out_directory, atoi(input_parameters[2].c_str())); } else if (do_subsample) { uint32_t reference_length = atoi(input_parameters[2].c_str()); for (uint32_t i = 3; i < input_parameters.size(); ++i) { sampler->subsample(out_directory, reference_length, atoi(input_parameters[i].c_str())); } } return 0; } void help() { printf( "usage: rampler [options ...] \n" "\n" " \n" " subsample [ ...]\n" "\n" " \n" " input file in FASTA/FASTQ format (can be compressed with gzip)\n" " containing sequences to be subsampled\n" " \n" " integer denoting length of the reference genome (or\n" " assembly) from which the sequences originate\n" " \n" " integer denoting desired coverage of the subsampled\n" " sequences\n" "\n" " split \n" "\n" " \n" " input file in FASTA/FASTQ format (can be compressed with gzip)\n" " containing sequences which will be split into smaller chunks\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"); } rampler-1.1.1/src/sampler.cpp000066400000000000000000000126611353425151000161170ustar00rootroot00000000000000/*! * @file sampler.cpp * * @brief Sampler class source file */ #include #include "sequence.hpp" #include "sampler.hpp" #include "bioparser/bioparser.hpp" namespace rampler { constexpr uint32_t kChunkSize = 1024 * 1024 * 1024; // ~ 1GB std::unique_ptr createSampler(const std::string& sequences_path) { std::unique_ptr> sparser = nullptr; std::string base_name = sequences_path.substr(sequences_path.rfind('/') + 1); base_name = base_name.substr(0, base_name.find('.')); std::string extension; auto is_suffix = [](const std::string& src, const std::string& suffix) -> bool { if (src.size() < suffix.size()) { return false; } return src.compare(src.size() - suffix.size(), suffix.size(), suffix) == 0; }; if (is_suffix(sequences_path, ".fasta") || is_suffix(sequences_path, ".fa") || is_suffix(sequences_path, ".fasta.gz") || is_suffix(sequences_path, ".fa.gz")) { sparser = bioparser::createParser( sequences_path); extension = ".fasta"; } else if (is_suffix(sequences_path, ".fastq") || is_suffix(sequences_path, ".fq") || is_suffix(sequences_path, ".fastq.gz") || is_suffix(sequences_path, ".fq.gz")) { sparser = bioparser::createParser( sequences_path); extension = ".fastq"; } else { fprintf(stderr, "[rampler::createSampler] error: " "file %s has unsupported format extension (valid extensions: " ".fasta, .fasta.gz, .fa, .fa.gz, .fastq, .fastq.gz, .fq, .fq.gz)!\n", sequences_path.c_str()); exit(1); } return std::unique_ptr(new Sampler(std::move(sparser), base_name, extension)); } 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) { } Sampler::~Sampler() { } void Sampler::initialize() { if (sequences_length_ != 0) { fprintf(stderr, "[rampler::Sampler::initialize] warning: " "object already initialized!\n"); return; } sparser_->reset(); while (true) { std::vector> sequences; auto status = sparser_->parse(sequences, kChunkSize); for (const auto& it: sequences) { sequences_length_ += it->data().size(); } if (!status) { break; } } } void Sampler::subsample(const std::string& out_directory, uint32_t reference_length, uint32_t coverage) { if (coverage * reference_length > sequences_length_) { fprintf(stderr, "[rampler::Sampler::subsample] warning: " "insufficient data for coverage %u!\n", coverage); 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_); std::string out_path = out_directory + "/" + base_name_ + "_" + std::to_string(coverage) + "x" + extension_; auto out = fopen(out_path.c_str(), "w"); if (out == nullptr) { fprintf(stderr, "rampler::Sampler::subsample] error: " "unable to create file %s!\n", out_path.c_str()); exit(1); } sparser_->reset(); while (true) { std::vector> sequences; auto status = sparser_->parse(sequences, kChunkSize); for (const auto& it: sequences) { if (distribution(generator) < ratio) { if (it->quality().empty()) { fprintf(out, ">%s\n%s\n", it->name().c_str(), it->data().c_str()); } else { fprintf(out, "@%s\n%s\n+\n%s\n", it->name().c_str(), it->data().c_str(), it->quality().c_str()); } } } if (!status) { break; } } fclose(out); } void Sampler::split(const std::string& out_directory, uint32_t chunk_size) { if (chunk_size > sequences_length_) { fprintf(stderr, "[rampler::Sampler::split] warning: " "insufficient data for chunk size %u!\n", chunk_size); return; } uint32_t chunk_number = 0; sparser_->reset(); while (true) { std::vector> sequences; auto status = sparser_->parse(sequences, chunk_size); std::string out_path = out_directory + "/" + base_name_ + "_" + std::to_string(chunk_number) + extension_; auto out = fopen(out_path.c_str(), "w"); if (out == nullptr) { fprintf(stderr, "rampler::Sampler::subsample] error: " "unable to create file %s!\n", out_path.c_str()); exit(1); } for (const auto& it: sequences) { if (it->quality().empty()) { fprintf(out, ">%s\n%s\n", it->name().c_str(), it->data().c_str()); } else { fprintf(out, "@%s\n%s\n+\n%s\n", it->name().c_str(), it->data().c_str(), it->quality().c_str()); } } fclose(out); ++chunk_number; if (!status) { break; } } } } rampler-1.1.1/src/sampler.hpp000066400000000000000000000020201353425151000161100ustar00rootroot00000000000000/*! * @file sampler.hpp * * @brief Sampler class header file */ #pragma once #include #include #include namespace bioparser { template class Parser; } namespace rampler { class Sampler; std::unique_ptr createSampler(const std::string& sequences_path); class Sampler { public: ~Sampler(); void initialize(); void subsample(const std::string& out_directory, uint32_t reference_length, uint32_t coverage); void split(const std::string& out_directory, uint32_t chunk_size); friend std::unique_ptr createSampler(const std::string& sequences_path); private: Sampler(std::unique_ptr> sparser, const std::string& base_name, const std::string& extension); Sampler(const Sampler&) = delete; const Sampler& operator=(const Sampler&) = delete; std::unique_ptr> sparser_; uint64_t sequences_length_; std::string base_name_; std::string extension_; }; } rampler-1.1.1/src/sequence.cpp000066400000000000000000000010561353425151000162600ustar00rootroot00000000000000/*! * @file sequence.cpp * * @brief Sequence class source file */ #include "sequence.hpp" namespace rampler { Sequence::Sequence(const char* name, uint32_t name_length, const char* data, uint32_t data_length) : name_(name, name_length), data_(data, data_length), quality_() { } Sequence::Sequence(const char* name, uint32_t name_length, const char* data, uint32_t data_length, const char* quality, uint32_t quality_length) : name_(name, name_length), data_(data, data_length), quality_(quality, quality_length) { } } rampler-1.1.1/src/sequence.hpp000066400000000000000000000020261353425151000162630ustar00rootroot00000000000000/*! * @file sequence.hpp * * @brief Sequence class header file */ #pragma once #include #include namespace bioparser { template class FastaParser; template class FastqParser; } namespace rampler { class Sequence { public: ~Sequence() = default; const std::string& name() const { return name_; } const std::string& data() const { return data_; } const std::string& quality() const { return quality_; } friend bioparser::FastaParser; friend bioparser::FastqParser; private: Sequence(const char* name, uint32_t name_length, const char* data, uint32_t data_length); Sequence(const char* name, uint32_t name_length, const char* data, uint32_t data_length, const char* quality, uint32_t quality_length); Sequence(const Sequence&) = delete; const Sequence& operator=(const Sequence&) = delete; std::string name_; std::string data_; std::string quality_; }; } rampler-1.1.1/vendor/000077500000000000000000000000001353425151000144505ustar00rootroot00000000000000rampler-1.1.1/vendor/bioparser/000077500000000000000000000000001353425151000164365ustar00rootroot00000000000000