text:
/* ----------------------------------------------------------------------- *//**
 *
 * @file dbconnector.hpp
 *
 * @brief This file should be included by user code (and nothing else)
 *
 *//* ----------------------------------------------------------------------- */

#ifndef MADLIB_POSTGRES_DBCONNECTOR_HPP
#define MADLIB_POSTGRES_DBCONNECTOR_HPP

// On platforms based on PostgreSQL we can include a different set of headers.
// Also, there may be different compatibility headers for other platforms.
#ifndef MADLIB_POSTGRES_HEADERS
#define MADLIB_POSTGRES_HEADERS

// A recent change in the postgres port.h file plays some havoc with /usr/include/stdlib.h
// (commit https://github.com/postgres/postgres/commit/a919937f112eb2f548d5f9bd1b3a7298375e6380)
// Since we don't need anything from ports.h we can cheat and say its already been declared.
// Warning: This could cause problems in the future...
#define PG_PORT_H

extern "C" {
    #include <postgres.h>
    #include <pg_config.h>         // Use the macro defined in the header to detect the platform
    #include <funcapi.h>
    #include <catalog/pg_proc.h>
    #include <catalog/pg_type.h>
    #include <executor/executor.h> // For GetAttributeByNum()
    #include <miscadmin.h>         // Memory allocation, e.g., HOLD_INTERRUPTS
    #include <utils/acl.h>
    #include <utils/array.h>
    #include <utils/builtins.h>    // needed for format_procedure()
    #include <utils/datum.h>
    #include <utils/lsyscache.h>   // for type lookup, e.g., type_is_rowtype
    #include <utils/memutils.h>
    #include <utils/syscache.h>    // for direct access to catalog, e.g., SearchSysCache()
    #include <utils/typcache.h>    // type conversion, e.g., lookup_rowtype_tupdesc
    #include "../../../../methods/svec/src/pg_gp/sparse_vector.h" // Legacy sparse vectors
} // extern "C"

#include "Compatibility.hpp"

#endif // !defined(MADLIB_POSTGRES_HEADERS)

// Unfortunately, we have to clean up some #defines in PostgreSQL headers. They
// interfere with C++ code.
// From c.h:
#ifdef Abs
#undef Abs
#endif

#ifdef Max
#undef Max
#endif

#ifdef Min
#undef Min
#endif

#ifdef gettext
#undef gettext
#endif

#ifdef dgettext
#undef dgettext
#endif

#ifdef ngettext
#undef ngettext
#endif

#ifdef dngettext
#undef dngettext
#endif

// Note: If errors occur in the following include files, it could indicate that
// new macros have been added to PostgreSQL header files.
#include <boost/mpl/if.hpp>
#include <boost/any.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/tr1/array.hpp>
#include <boost/tr1/functional.hpp>
#include <boost/tr1/tuple.hpp>
#include <algorithm>
#include <complex>
#include <limits>
#include <stdexcept>
#include <vector>
#include <fstream>

#include <dbal/dbal_proto.hpp>
#include <utils/Reference.hpp>
#include <utils/Math.hpp>

namespace std {
    // Import names from TR1.

    // The following are currently provided by boost.
    using tr1::array;
    using tr1::bind;
    using tr1::function;
    using tr1::get;
    using tr1::make_tuple;
    using tr1::tie;
    using tr1::tuple;
}

#if !defined(NDEBUG) && !defined(EIGEN_NO_DEBUG)
#define eigen_assert(x) \
    do { \
        if(!Eigen::internal::copy_bool(x)) \
            throw std::runtime_error(std::string( \
                "Internal error. Eigen assertion failed (" \
                EIGEN_MAKESTRING(x) ") in function ") + __PRETTY_FUNCTION__ + \
                " at " __FILE__ ":" EIGEN_MAKESTRING(__LINE__)); \
    } while(false)
#endif // !defined(NDEBUG) && !defined(EIGEN_NO_DEBUG)

// We need to make _oldContext volatile because if an exception occurs, the
// register holding its value might have been overwritten (and the variable
// value is read from in the PG_CATCH block). On the other hand, no need to make
// _errorData volatile: If no PG exception occurs, no register will be
// overwritten. If an exception occurs, _errorData will be set before it is read
// again.
#define MADLIB_PG_TRY \
    do { \
        volatile MemoryContext _oldContext \
            = CurrentMemoryContext; \
        ErrorData* _errorData = NULL; \
        PG_TRY();

// CopyErrorData() copies into the current memory context, so we
// need to switch away from the error context first
#define MADLIB_PG_CATCH \
        PG_CATCH(); { \
            MemoryContextSwitchTo(_oldContext); \
            _errorData = CopyErrorData(); \
            FlushErrorState(); \
        } PG_END_TRY(); \
        if (_errorData)

#define MADLIB_PG_END_TRY \
    } while(false)

#define MADLIB_PG_RE_THROW \
    throw PGException(_errorData)

#define MADLIB_PG_ERROR_DATA() \
    _errorData

#define MADLIB_PG_DEFAULT_CATCH_AND_END_TRY \
    MADLIB_PG_CATCH { \
        MADLIB_PG_RE_THROW; \
    } MADLIB_PG_END_TRY

#define MADLIB_WRAP_PG_FUNC(_returntype, _pgfunc, _arglist, _passedlist) \
inline \
_returntype \
madlib ## _ ## _pgfunc _arglist { \
    _returntype _result = static_cast<_returntype>(0); \
    MADLIB_PG_TRY { \
        _result = _pgfunc _passedlist; \
    } MADLIB_PG_DEFAULT_CATCH_AND_END_TRY; \
    return _result; \
}

#define MADLIB_WRAP_VOID_PG_FUNC(_pgfunc, _arglist, _passedlist) \
inline \
void \
madlib ## _ ## _pgfunc _arglist { \
    MADLIB_PG_TRY { \
        _pgfunc _passedlist; \
    } MADLIB_PG_DEFAULT_CATCH_AND_END_TRY; \
}

/**
 * The maximum number of arguments that can be passed to a function via a
 * FunctionHandle.
 * Note that PostgreSQL defines FUNC_MAX_ARGS, which we could use here. However,
 * this can be a fairly large number that might exceed BOOST_PP_LIMIT_REPEAT.
 * In fmgr.h, PostgreSQL provides support functions (FunctionCall*Coll) for only
 * up to 9 arguments.
 */
#define MADLIB_FUNC_MAX_ARGS 9

/**
 * The maximum number of dimensions in an array
 */
#define MADLIB_MAX_ARRAY_DIMS 2

#include "Allocator_proto.hpp"
#include "ArrayHandle_proto.hpp"
#include "ArrayWithNullException_proto.hpp"
#include "AnyType_proto.hpp"
#include "ByteString_proto.hpp"
#include "NativeRandomNumberGenerator_proto.hpp"
#include "PGException_proto.hpp"
#include "OutputStreamBuffer_proto.hpp"
#include "SystemInformation_proto.hpp"
#include "TransparentHandle_proto.hpp"
#include "TypeTraits_proto.hpp"
#include "UDF_proto.hpp"
// Need to move FunctionHandle down because it has dependencies
#include "FunctionHandle_proto.hpp"

// Several backend functions (APIs) need a wrapper, so that they can be called
// safely from a C++ context.
#include "Backend.hpp"

namespace madlib {

// Import MADlib types into madlib namespace
using dbconnector::postgres::Allocator;
using dbconnector::postgres::AnyType;
using dbconnector::postgres::ArrayHandle;
using dbconnector::postgres::ByteString;
using dbconnector::postgres::FunctionHandle;
using dbconnector::postgres::MutableArrayHandle;
using dbconnector::postgres::MutableByteString;
using dbconnector::postgres::NativeRandomNumberGenerator;
using dbconnector::postgres::TransparentHandle;

// Import MADlib functions into madlib namespace
using dbconnector::postgres::AnyType_cast;
using dbconnector::postgres::defaultAllocator;
using dbconnector::postgres::funcPtr;
using dbconnector::postgres::Null;

// Import MADlib exceptions into madlib namespace
using dbconnector::postgres::ArrayWithNullException;

namespace dbconnector {

namespace postgres {

#ifndef NDEBUG
extern std::ostream dbout;
extern std::ostream dberr;
#endif
} // namespace postgres

} // namespace dbconnector

#ifndef NDEBUG
// Import MADlib global variables into madlib namespace
using dbconnector::postgres::dbout;
using dbconnector::postgres::dberr;
#endif

// A wrapper function added as a workaround to fix MADLIB-769
// To support grouping, we cannot throw an exception because
// it will exit the process. Instead, we print a message and
// set an internal status in the state variable in the C++ code.
inline void warning(std::string msg){
    elog(WARNING, "%s", msg.c_str());
}

} // namespace madlib

#include <dbal/dbal_impl.hpp>

// FIXME: The following include should be further up. Currently dependent on
// dbal_impl.hpp which depends on the memory allocator.
#include "EigenIntegration_proto.hpp"

#include "Allocator_impl.hpp"
#include "AnyType_impl.hpp"
#include "ArrayHandle_impl.hpp"
#include "ByteString_impl.hpp"
#include "EigenIntegration_impl.hpp"
#include "FunctionHandle_impl.hpp"
#include "NativeRandomNumberGenerator_impl.hpp"
#include "OutputStreamBuffer_impl.hpp"
#include "TransparentHandle_impl.hpp"
#include "TypeTraits_impl.hpp"
#include "UDF_impl.hpp"
#include "SystemInformation_impl.hpp"

namespace madlib {

typedef dbal::DynamicStructRootContainer<
    ByteString, dbconnector::postgres::TypeTraits> RootContainer;
typedef dbal::DynamicStructRootContainer<
    MutableByteString, dbconnector::postgres::TypeTraits> MutableRootContainer;

} // namespace madlib

#define DECLARE_UDF(_module, _name) \
    namespace madlib { \
    namespace modules { \
    namespace _module { \
    struct _name : public dbconnector::postgres::UDF { \
        inline _name() { }  \
        AnyType run(AnyType &args); \
        inline void *SRF_init(AnyType&) {return NULL;}; \
        inline AnyType SRF_next(void *, bool *){return AnyType();}; \
    }; \
    } \
    } \
    }

#define DECLARE_SR_UDF(_module, _name) \
    namespace madlib { \
    namespace modules { \
    namespace _module { \
    struct _name : public dbconnector::postgres::UDF { \
        inline _name() { }  \
        inline AnyType run(AnyType &){return AnyType();}; \
        void *SRF_init(AnyType &args); \
        AnyType SRF_next(void *user_fctx, bool *is_last_call); \
    }; \
    } \
    } \
    }

#define DECLARE_UDF_EXTERNAL(_module, _name) \
    namespace external { \
        extern "C" { \
            PG_FUNCTION_INFO_V1(_name); \
            Datum _name(PG_FUNCTION_ARGS) { \
                return madlib::dbconnector::postgres::UDF::call< \
                    madlib::modules::_module::_name>(fcinfo); \
            } \
        } \
    }

#endif // defined(MADLIB_POSTGRES_DBCONNECTOR_HPP)


text:
// Copyright (C) 2015 National ICT Australia (NICTA)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// -------------------------------------------------------------------
//
// Written by Conrad Sanderson - http://conradsanderson.id.au

#include <armadillo>
#include "catch.hpp"

using namespace arma;

TEST_CASE("attributes_1")
  {
  mat A(5,6);
  REQUIRE(A.n_rows == 5);
  REQUIRE(A.n_cols == 6);
  REQUIRE(A.n_elem == 30);

  vec B(5);
  REQUIRE(B.n_rows == 5);
  REQUIRE(B.n_cols == 1);
  REQUIRE(B.n_elem == 5);

  rowvec C(6);
  REQUIRE(C.n_rows == 1);
  REQUIRE(C.n_cols == 6);
  REQUIRE(C.n_elem == 6);

  cube D(5,6,2);
  REQUIRE(D.n_rows   == 5);
  REQUIRE(D.n_cols   == 6);
  REQUIRE(D.n_slices == 2);
  REQUIRE(D.n_elem   == 60);

  sp_mat E(50,60);
  E(0,0) = 1.0;
  E(E.n_rows-1,E.n_cols-1) = 1.0;

  REQUIRE(E.n_rows    == 50);
  REQUIRE(E.n_cols    == 60);
  REQUIRE(E.n_elem    == 3000);
  REQUIRE(E.n_nonzero == 2);

  field<mat> G(5,6,2);
  REQUIRE(G.n_rows   == 5);
  REQUIRE(G.n_cols   == 6);
  REQUIRE(G.n_slices == 2);
  REQUIRE(G.n_elem   == 60);
  }

ext: cpp

max_issues_repo_name: orestis-z/pyjet

max_forks_repo_name: orestis-z/pyjet


text:
#include <Eigen/Dense>

#include "DenseMatrix.h"

struct RawSubmatrixData::Impl {
    Eigen::MatrixXd RawData;

  public:
    Impl() = default;
};

RawSubmatrixData::RawSubmatrixData() : pImpl {std::make_unique<Impl>()} {}

RawSubmatrixData::~RawSubmatrixData() = default;
RawSubmatrixData::RawSubmatrixData(RawSubmatrixData&&) noexcept = default;
RawSubmatrixData& RawSubmatrixData::operator=(RawSubmatrixData&&) noexcept = default;

void RawSubmatrixData::add_to_position(double value, uint32_t i, uint32_t j) {
    pImpl->RawData(i, j) += value;
}

std::ostream& operator<<(std::ostream& os, const RawSubmatrixData& decomposition) {
    os << decomposition.pImpl->RawData << std::endl;
    return os;
}

void RawSubmatrixData::resize(
    uint32_t matrix_in_space_basis_size_i,
    uint32_t matrix_in_space_basis_size_j) {
    // TODO: is it the fastest way to initialize pImpl->RawData?
    pImpl->RawData.resize(matrix_in_space_basis_size_i, matrix_in_space_basis_size_j);
    pImpl->RawData.fill(0);
}

void RawSubmatrixData::diagonalize() {
    Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es;
    es.compute(pImpl->RawData, Eigen::ComputeEigenvectors);
    std::cout << es.eigenvalues() << std::endl;
    std::cout << es.eigenvectors() << std::endl;
}


text:
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Sylko Olzscher
 *
 */

#include "binary_consumer.h"
#include "../message_ids.h"
#include <cyng/async/task/task_builder.hpp>
#include <cyng/dom/reader.h>
#include <cyng/io/serializer.h>
#include <cyng/io/io_bytes.hpp>
#include <cyng/value_cast.hpp>
#include <cyng/set_cast.h>
#include <cyng/chrono.h>
#include <cyng/factory/set_factory.h>
#include <fstream>
#include <sstream>
#include <boost/filesystem.hpp>

namespace node
{

	binary_consumer::binary_consumer(cyng::async::base_task* btp
		, cyng::logging::log_ptr logger
		, std::size_t ntid	//	network task id
		, std::string root_dir
		, std::string prefix
		, std::string suffix
		, std::chrono::seconds period)
	: base_(*btp)
		, logger_(logger)
		, ntid_(ntid)
		, root_dir_(root_dir)
		, prefix_(prefix)
		, suffix_(suffix)
		, period_(period)
		, task_state_(TASK_STATE_INITIAL)
		, total_bytes_(0)
	{
		CYNG_LOG_INFO(logger_, "task #"
			<< base_.get_id()
			<< " <"
			<< base_.get_class_name()
			<< "> output to "
			<< root_dir
			<< '/'
			<< prefix_
			<< "...."
			<< suffix_);

	}

	cyng::continuation binary_consumer::run()
	{
		switch (task_state_) {
		case TASK_STATE_INITIAL:
			//
			//	register as SML:XML consumer
			//
			register_consumer();
			task_state_ = TASK_STATE_REGISTERED;
			break;
		default:
			//CYNG_LOG_INFO(logger_, base_.get_class_name()
			//	<< " has processed "
			//	<< cyng::bytes_to_str(total_bytes_));
			break;
		}
		base_.suspend(period_);
		return cyng::continuation::TASK_CONTINUE;
	}

	void binary_consumer::stop()
	{
		CYNG_LOG_INFO(logger_, "task #"
			<< base_.get_id()
			<< " <"
			<< base_.get_class_name()
			<< " stopped");
	}

	cyng::continuation binary_consumer::process(std::uint32_t channel
		, std::uint32_t source
		, std::string protocol
		, std::string const& target
		, cyng::buffer_t const& data)
	{
		auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
		std::tm time = cyng::chrono::convert_utc(tt);

		//
		//	remove ':' from protocol name
		//
		protocol.erase(std::remove(protocol.begin(), protocol.end(), ':'), protocol.end());

		//
		//	generate file name
		//
		std::stringstream ss;
		ss
			<< prefix_
			<< '-'
			<< std::setfill('0')
			<< '-'
			<< protocol
			<< '-'
			<< std::dec
			<< cyng::chrono::year(time)
			<< std::setw(2)
			<< cyng::chrono::month(time)
			<< 'T'
			<< std::setw(2)
			<< cyng::chrono::day(time)
			<< std::setw(2)
			<< cyng::chrono::hour(time)
			<< std::setw(2)
			<< cyng::chrono::minute(time)
			<< '-'
			<< target
			<< std::hex
			<< std::setw(4)
			<< channel
			<< '-'
			<< std::setw(4)
			<< source
			<< '.'
			<< suffix_
			;

		boost::filesystem::path file = boost::filesystem::path(root_dir_) / ss.str();

		std::ofstream of(file.string(), std::ios::out | std::ios::binary | std::ios::app);
		if (of.is_open())
		{
			CYNG_LOG_TRACE(logger_, "task #"
				<< base_.get_id()
				<< " <"
				<< base_.get_class_name()
				<< "> write "
				<< data.size()
				<< " bytes to "
				<< file);

			of.write(data.data(), data.size());
			total_bytes_ += data.size();
		}
		else
		{
			CYNG_LOG_ERROR(logger_, "task #"
				<< base_.get_id()
				<< " <"
				<< base_.get_class_name()
				<< "> cannot open file "
				<< file);

		}

		CYNG_LOG_INFO(logger_, base_.get_class_name()
			<< " has processed "
			<< cyng::bytes_to_str(total_bytes_));

		return cyng::continuation::TASK_CONTINUE;
	}

	void binary_consumer::register_consumer()
	{
		base_.mux_.post(ntid_, STORE_EVENT_REGISTER_CONSUMER, cyng::tuple_factory("ALL:RAW", base_.get_id()));
	}

}

max_stars_repo_name: solosTec/node

max_issues_repo_name: solosTec/node

max_forks_repo_name: solosTec/node


text: #include <boost/archive/polymorphic_xml_wiarchive.hpp>

ext: hpp

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include <omp.h>
#include <boost/program_options.hpp>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <limits>
#include <csignal>
#include "Aligner.h"
#include "stream.hpp"
#include "ThreadReadAssertion.h"
#include "EValue.h"

int main(int argc, char** argv)
{
	GOOGLE_PROTOBUF_VERIFY_VERSION;

	std::cout << "GraphChainer " << VERSION << std::endl;
	std::cerr << "GraphChainer " << VERSION << std::endl;

#ifndef NOBUILTINPOPCOUNT
	if (__builtin_cpu_supports("popcnt") == 0)
	{
		std::cerr << "CPU does not support builtin popcount operation" << std::endl;
		std::cerr << "recompile with -DNOBUILTINPOPCOUNT" << std::endl;
		std::abort();
	}
#endif

	struct sigaction act;
	act.sa_handler = ThreadReadAssertion::signal;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	sigaction(SIGSEGV, &act, 0);

	boost::program_options::options_description mandatory("Mandatory parameters");
	mandatory.add_options()
		("graph,g", boost::program_options::value<std::string>(), "input graph (.gfa / .vg)")
		("reads,f", boost::program_options::value<std::vector<std::string>>()->multitoken(), "input reads (fasta or fastq, uncompressed or gzipped)")
		("alignments-out,a", boost::program_options::value<std::vector<std::string>>(), "output alignment file (.gaf/.gam/.json)")
	;
	boost::program_options::options_description clcparams("Colinear chaining parameters");
	clcparams.add_options()
		("speed", boost::program_options::value<long long>(), "Speed-up factor [default 1]. Use >1 (<1, >0) for faster (slower), but less (more) accurate alignments. It increases (decreases) the sampling rate of fragments.")
		("colinear-split-len", boost::program_options::value<long long>(), "The length of the fragments in which the long read is split to create anchors. [default 35]")
		("colinear-split-gap", boost::program_options::value<long long>(), "The distance between consecutive fragments [default 35]. If --speed is set, then always --colinear-split-gap = ceil(--speed * --colinear-split-len).")
		("colinear-gap", boost::program_options::value<long long>(), "When converting an optimal chain of anchors into an alignment path, split the path if the distance between consecutive anchors is greater than this value [default 10000].")
		// ("mpc-index,i", boost::program_options::value<std::string>(), "minimium path cover index filename")
		("fast-mode", "(Development purposes) Skip edit distance computation after chaining (output the path instead of alignment)")
	;

	boost::program_options::options_description general("General parameters");
	general.add_options()
		("help,h", "help message")
		("version", "print version")
		("threads,t", boost::program_options::value<size_t>(), "number of threads (int) (default 1)")
		("verbose", "print progress messages")
		("short-verbose", "print shorter progress messages")
	;

	boost::program_options::options_description generalGA("General parameters from GraphAligner");
	generalGA.add_options()
		("E-cutoff", boost::program_options::value<double>(), "discard alignments with E-value > arg")
		("all-alignments", "return all alignments instead of the best non-overlapping alignments")
		("extra-heuristic", "use heuristics to discard more seed hits")
		("try-all-seeds", "don't use heuristics to discard seed hits")
		("global-alignment", "force the read to be aligned end-to-end even if the alignment score is poor")
		("optimal-alignment", "calculate the optimal alignment (VERY SLOW)")
		("X-drop", boost::program_options::value<int>(), "use X-drop heuristic to end alignment with score cutoff arg (int)")
		("precise-clipping", boost::program_options::value<double>(), "clip the alignment ends more precisely with arg as the identity cutoff between correct / wrong alignments (float)")
		("cigar-match-mismatch", "use M for matches and mismatches in the cigar string instead of = and X")
		("generate-path", "generate a path and write to file instead of aligning reads from file")
		("generate-path-seed", boost::program_options::value<long long>(), "seed for generating path")
		("graph-statistics", "show statistics (size, width) of the input graph then exit")
	;

	boost::program_options::options_description seeding("Seeding parameters from GraphAligner");
	seeding.add_options()
		("seeds-clustersize", boost::program_options::value<size_t>(), "discard seed clusters with fewer than arg seeds (int)")
		("seeds-extend-density", boost::program_options::value<double>(), "extend up to approximately the best (arg * sequence length) seeds (double) (-1 for all)")
		("seeds-minimizer-length", boost::program_options::value<size_t>(), "k-mer length for minimizer seeding (int)")
		("seeds-minimizer-windowsize", boost::program_options::value<size_t>(), "window size for minimizer seeding (int)")
		("seeds-minimizer-density", boost::program_options::value<double>(), "keep approximately (arg * sequence length) least common minimizers (double) (-1 for all)")
		("seeds-minimizer-ignore-frequent", boost::program_options::value<double>(), "ignore arg most frequent fraction of minimizers (double)")
		("seeds-mum-count", boost::program_options::value<size_t>(), "arg longest maximal unique matches fully contained in a node (int) (-1 for all)")
		("seeds-mem-count", boost::program_options::value<size_t>(), "arg longest maximal exact matches fully contained in a node (int) (-1 for all)")
		("seeds-mxm-length", boost::program_options::value<size_t>(), "minimum length for maximal unique / exact matches (int)")
		("seeds-mxm-cache-prefix", boost::program_options::value<std::string>(), "store the mum/mem seeding index to the disk for reuse, or reuse it if it exists (filename prefix)")
		("seeds-file,s", boost::program_options::value<std::vector<std::string>>()->multitoken(), "external seeds (.gam)")
		("seedless-DP", "no seeding, instead use DP alignment algorithm for the entire first row. VERY SLOW except on tiny graphs")
		("DP-restart-stride", boost::program_options::value<size_t>(), "if --seedless-DP doesn't span the entire read, restart after arg base pairs (int)")
	;
	boost::program_options::options_description alignment("Extension parameters from GraphAligner");
	alignment.add_options()
		("bandwidth,b", boost::program_options::value<size_t>(), "alignment bandwidth (int)")
		("ramp-bandwidth,B", boost::program_options::value<size_t>(), "ramp bandwidth (int)")
		("tangle-effort,C", boost::program_options::value<size_t>(), "tangle effort limit, higher results in slower but more accurate alignments (int) (-1 for unlimited)")
		("high-memory", "use slightly less CPU but a lot more memory")
	;
	boost::program_options::options_description hidden("hidden");
	hidden.add_options()
		("schedule-inverse-E-sum", "optimally select a non-overlapping set based on the sum of inverse E-values")
		("schedule-inverse-E-product", "optimally select a non-overlapping set based on the product of inverse E-values")
		("schedule-score", "optimally select a non-overlapping set based on the alignment score")
		("schedule-length", "optimally select a non-overlapping set based on the alignment length")
		("greedy-length", "greedily select a non-overlapping alignment set based on alignment length")
		("greedy-E", "greedily select a non-overlapping alignment set based on E-value")
		("greedy-score", "greedily select a non-overlapping alignment set based on alignment score")
		("no-colinear-chaining", "do not run colinear chaining and align as in GraphAligner, default parameters")
	;

	boost::program_options::options_description cmdline_options;
	cmdline_options.add(mandatory).add(clcparams).add(general).add(generalGA).add(seeding).add(alignment).add(hidden);

	boost::program_options::variables_map vm;
	try
	{
		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, cmdline_options), vm);
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
		std::cerr << "run with option -h for help" << std::endl;
		std::exit(1);
	}
	boost::program_options::notify(vm);

	if (vm.count("help"))
	{
		std::cerr << mandatory << std::endl << clcparams << std::endl << general << std::endl << generalGA << std::endl << seeding << std::endl << alignment << std::endl;
		std::exit(0);
	}
	if (vm.count("version"))
	{
		std::cout << "Version " << VERSION << std::endl;
		std::exit(0);
	}

	AlignerParams params;
	params.graphFile = "";
	params.outputGAMFile = "";
	params.outputJSONFile = "";
	params.outputGAFFile = "";
	params.outputCorrectedFile = "";
	params.outputCorrectedClippedFile = "";
	params.numThreads = 1;
	params.initialBandwidth = 0;
	params.rampBandwidth = 0;
	params.dynamicRowStart = false;
	params.maxCellsPerSlice = std::numeric_limits<decltype(params.maxCellsPerSlice)>::max();
	params.verboseMode = false;
	params.shortVerboseMode = false;
	params.tryAllSeeds = false;
	params.highMemory = false;
	params.mxmLength = 20;
	params.mumCount = 0;
	params.memCount = 0;
	params.seederCachePrefix = "";
	params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::GreedyLength; //todo pick better default
	params.selectionECutoff = -1;
	params.forceGlobal = false;
	params.compressCorrected = false;
	params.compressClipped = false;
	params.preciseClipping = false;
	params.minimizerSeedDensity = 0;
	params.minimizerLength = 19;
	params.minimizerWindowSize = 30;
	params.seedClusterMinSize = 1;
	params.minimizerDiscardMostNumerousFraction = 0.0002;
	params.seedExtendDensity = 0.002;
	params.nondeterministicOptimizations = false;
	params.optimalDijkstra = false;
	params.preciseClippingIdentityCutoff = 0.5;
	params.Xdropcutoff = 0;
	params.DPRestartStride = 0;
	params.cigarMatchMismatchMerge = false;
	params.colinearChaining = true;
	params.generatePath = false;
	params.generatePathSeed = 0;
	params.IndexMpcFile = "";
	params.fastMode = false;
	params.graphStatistics = false;

	std::vector<std::string> outputAlns;
	bool paramError = false;

	// Using GraphAligner's vg presets:
	params.minimizerSeedDensity = 10;
	params.minimizerLength = 15;
	params.minimizerWindowSize = 20;
	params.seedExtendDensity = -1;
	params.minimizerDiscardMostNumerousFraction = 0.001;
	params.nondeterministicOptimizations = false;
	params.initialBandwidth = 10;

	if (vm.count("fast-mode"))
		params.fastMode = true;

	if (vm.count("no-colinear-chaining"))
		params.colinearChaining = false;

	if (params.colinearChaining)
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::All;
		params.tryAllSeeds = true;
		params.colinearGap = 10000;
		params.colinearSplitLen = 35;
		params.colinearSplitGap = 35;
        params.speed = 1;
	}

	if (vm.count("graph")) params.graphFile = vm["graph"].as<std::string>();
	if (vm.count("reads")) params.fastqFiles = vm["reads"].as<std::vector<std::string>>();
	if (vm.count("alignments-out")) outputAlns = vm["alignments-out"].as<std::vector<std::string>>();
	if (vm.count("threads")) params.numThreads = vm["threads"].as<size_t>();
	if (vm.count("bandwidth")) params.initialBandwidth = vm["bandwidth"].as<size_t>();

	if (vm.count("seeds-extend-density")) params.seedExtendDensity = vm["seeds-extend-density"].as<double>();
	if (vm.count("seeds-minimizer-ignore-frequent")) params.minimizerDiscardMostNumerousFraction = vm["seeds-minimizer-ignore-frequent"].as<double>();
	if (vm.count("seeds-clustersize")) params.seedClusterMinSize = vm["seeds-clustersize"].as<size_t>();
	if (vm.count("seeds-minimizer-density")) params.minimizerSeedDensity = vm["seeds-minimizer-density"].as<double>();
	if (vm.count("seeds-minimizer-length")) params.minimizerLength = vm["seeds-minimizer-length"].as<size_t>();
	if (vm.count("seeds-minimizer-windowsize")) params.minimizerWindowSize = vm["seeds-minimizer-windowsize"].as<size_t>();
	if (vm.count("seeds-file")) params.seedFiles = vm["seeds-file"].as<std::vector<std::string>>();
	if (vm.count("seeds-mxm-length")) params.mxmLength = vm["seeds-mxm-length"].as<size_t>();
	if (vm.count("seeds-mem-count")) params.memCount = vm["seeds-mem-count"].as<size_t>();
	if (vm.count("seeds-mum-count")) params.mumCount = vm["seeds-mum-count"].as<size_t>();
	if (vm.count("seeds-mxm-cache-prefix")) params.seederCachePrefix = vm["seeds-mxm-cache-prefix"].as<std::string>();
	if (vm.count("seedless-DP")) params.dynamicRowStart = true;

	if (vm.count("colinear-gap")) params.colinearGap = vm["colinear-gap"].as<long long>();
	if (vm.count("colinear-split-len")) params.colinearSplitLen = vm["colinear-split-len"].as<long long>();
	if (vm.count("colinear-split-gap")) params.colinearSplitGap = vm["colinear-split-gap"].as<long long>();
	if (vm.count("speed")) params.speed = vm["speed"].as<long long>();
	if (vm.count("mpc-index")) params.IndexMpcFile = vm["mpc-index"].as<std::string>();

	if (params.speed != 1)
	{
		if (vm.count("colinear-split-gap"))
		{
			std::cerr << "WARNING: --speed and --colinear-split-gap are both set! --colinear-split-gap will be ignored, and set to (--speed * --colinear-split-len)" << std::endl;
		}
		params.colinearSplitGap = ceil(params.speed * params.colinearSplitLen);
	}

	if (vm.count("DP-restart-stride")) params.DPRestartStride = vm["DP-restart-stride"].as<size_t>();

	if (vm.count("extra-heuristic")) params.nondeterministicOptimizations = true;
	if (vm.count("ramp-bandwidth")) params.rampBandwidth = vm["ramp-bandwidth"].as<size_t>();
	if (vm.count("tangle-effort")) params.maxCellsPerSlice = vm["tangle-effort"].as<size_t>();
	if (vm.count("verbose")) params.verboseMode = true;
	if (vm.count("short-verbose")) params.shortVerboseMode = true;
	if (vm.count("try-all-seeds")) params.tryAllSeeds = true;
	if (vm.count("high-memory")) params.highMemory = true;
	if (vm.count("cigar-match-mismatch")) params.cigarMatchMismatchMerge = true;
	if (vm.count("generate-path")) params.generatePath = true;
	if (vm.count("generate-path-seed")) params.generatePathSeed = vm["generate-path-seed"].as<long long>();
	if (vm.count("graph-statistics")) params.graphStatistics = true;

	int resultSelectionMethods = 0;
	if (vm.count("all-alignments"))
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::All;
		params.tryAllSeeds = true;
		resultSelectionMethods += 1;
	}
	if (vm.count("greedy-length"))
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::GreedyLength;
		resultSelectionMethods += 1;
	}
	if (vm.count("E-cutoff"))
	{
		params.selectionECutoff = vm["E-cutoff"].as<double>();
	}
	if (vm.count("schedule-inverse-E-sum"))
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::ScheduleInverseESum;
		resultSelectionMethods += 1;
	}
	if (vm.count("schedule-inverse-E-product"))
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::ScheduleInverseEProduct;
		resultSelectionMethods += 1;
	}
	if (vm.count("schedule-score"))
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::ScheduleScore;
		resultSelectionMethods += 1;
	}
	if (vm.count("schedule-length"))
	{
		params.alignmentSelectionMethod = AlignmentSelection::SelectionMethod::ScheduleLength;
		resultSelectionMethods += 1;
	}
	if (vm.count("verbose")) params.verboseMode = true;
	if (vm.count("short-verbose")) params.shortVerboseMode = true;
	if (vm.count("try-all-seeds")) params.tryAllSeeds = true;
	if (vm.count("high-memory")) params.highMemory = true;
	if (vm.count("global-alignment")) params.forceGlobal = true;
	if (vm.count("precise-clipping"))
	{
		params.preciseClipping = true;
		params.preciseClippingIdentityCutoff = vm["precise-clipping"].as<double>();
		if (params.preciseClippingIdentityCutoff < 0.001 || params.preciseClippingIdentityCutoff > 0.999)
		{
			std::cerr << "precise clipping identity cutoff must be between 0.001 and 0.999" << std::endl;
			paramError = true;
		}
		if (params.preciseClippingIdentityCutoff >= 0.001 && params.preciseClippingIdentityCutoff < 0.501)
		{
			std::cerr << "Warning: precise clipping identity cutoff set below 0.501. Output will almost certainly contain spurious alignments." << std::endl;
		}
	}
	if (vm.count("X-drop"))
	{
		params.Xdropcutoff = vm["X-drop"].as<int>();
		if (params.Xdropcutoff < 1)
		{
			std::cerr << "X-drop score cutoff must be > 1" << std::endl;
			paramError = true;
		}
	}
	if (vm.count("optimal-alignment")) params.optimalDijkstra = true;

	if (params.graphFile == "")
	{
		std::cerr << "graph file must be given" << std::endl;
		paramError = true;
	}
	if (params.fastqFiles.size() == 0)
	{
		std::cerr << "read file must be given" << std::endl;
		paramError = true;
	}
	if (outputAlns.size() == 0 && params.outputCorrectedFile == "" && params.outputCorrectedClippedFile == "")
	{
		std::cerr << "alignments-out must be given" << std::endl;
		paramError = true;
	}
	for (std::string file : outputAlns)
	{
		if (file.size() >= 4 && file.substr(file.size()-4) == ".gam")
		{
			params.outputGAMFile = file;
		}
		else if (file.size() >= 5 && file.substr(file.size()-5) == ".json")
		{
			params.outputJSONFile = file;
		}
		else if (file.size() >= 4 && file.substr(file.size()-4) == ".gaf")
		{
			params.outputGAFFile = file;
		}
		else
		{
			std::cerr << "unknown output alignment format (" << file << "), must be either .gaf, .gam or .json" << std::endl;
			paramError = true;
		}
	}
	if (params.outputCorrectedFile != "" && params.outputCorrectedFile.substr(params.outputCorrectedFile.size()-3) != ".fa" && params.outputCorrectedFile.substr(params.outputCorrectedFile.size()-6) != ".fasta" && params.outputCorrectedFile.substr(params.outputCorrectedFile.size()-6) != ".fa.gz" && params.outputCorrectedFile.substr(params.outputCorrectedFile.size()-9) != ".fasta.gz")
	{
		std::cerr << "unknown output corrected read format, must be .fa or .fa.gz" << std::endl;
		paramError = true;
	}
	if (params.outputCorrectedClippedFile != "" && params.outputCorrectedClippedFile.substr(params.outputCorrectedClippedFile.size()-3) != ".fa" && params.outputCorrectedClippedFile.substr(params.outputCorrectedClippedFile.size()-6) != ".fasta" && params.outputCorrectedClippedFile.substr(params.outputCorrectedClippedFile.size()-6) != ".fa.gz" && params.outputCorrectedClippedFile.substr(params.outputCorrectedClippedFile.size()-9) != ".fasta.gz")
	{
		std::cerr << "unknown output corrected read format, must be .fa or .fa.gz" << std::endl;
		paramError = true;
	}
	if (params.numThreads < 1)
	{
		std::cerr << "number of threads must be >= 1" << std::endl;
		paramError = true;
	}
	if (params.initialBandwidth < 1 && !params.optimalDijkstra)
	{
		std::cerr << "default bandwidth must be >= 1" << std::endl;
		paramError = true;
	}
	if (params.rampBandwidth != 0 && params.rampBandwidth <= params.initialBandwidth)
	{
		std::cerr << "ramp bandwidth must be higher than default bandwidth" << std::endl;
		paramError = true;
	}
	if (params.mxmLength < 2)
	{
		std::cerr << "mum/mem minimum length must be >= 2" << std::endl;
		paramError = true;
	}
	if (params.minimizerLength >= sizeof(size_t)*8/2)
	{
		std::cerr << "Maximum minimizer length is " << (sizeof(size_t)*8/2)-1 << std::endl;
		paramError = true;
	}
	if (params.minimizerDiscardMostNumerousFraction < 0 || params.minimizerDiscardMostNumerousFraction >= 1)
	{
		std::cerr << "Minimizer discard fraction must be 0 <= x < 1" << std::endl;
		paramError = true;
	}
	if (params.minimizerSeedDensity < 0 && params.minimizerSeedDensity != -1)
	{
		std::cerr << "Minimizer density can't be negative" << std::endl;
		paramError = true;
	}
	if (params.seedExtendDensity <= 0 && params.seedExtendDensity != -1)
	{
		std::cerr << "Seed extension density can't be negative" << std::endl;
		paramError = true;
	}
	int pickedSeedingMethods = ((params.dynamicRowStart) ? 1 : 0) + ((params.seedFiles.size() > 0) ? 1 : 0) + ((params.mumCount != 0) ? 1 : 0) + ((params.memCount != 0) ? 1 : 0) + ((params.minimizerSeedDensity != 0) ? 1 : 0);
	if (params.optimalDijkstra && (params.initialBandwidth > 0))
	{
		std::cerr << "--optimal-alignment set, ignoring parameter --bandwidth" << std::endl;
	}
	if (params.optimalDijkstra && (params.rampBandwidth > 0))
	{
		std::cerr << "--optimal-alignment set, ignoring parameter --ramp-bandwidth" << std::endl;
	}
	if (params.optimalDijkstra && (params.maxCellsPerSlice != std::numeric_limits<decltype(params.maxCellsPerSlice)>::max()))
	{
		std::cerr << "--optimal-alignment set, ignoring parameter --tangle-effort" << std::endl;
	}
	if (params.optimalDijkstra && pickedSeedingMethods > 0)
	{
		if (params.dynamicRowStart) std::cerr << "--optimal-alignment cannot be combined with --first-rows-DP" << std::endl;
		if (params.seedFiles.size() > 0) std::cerr << "--optimal-alignment cannot be combined with --seeds-file" << std::endl;
		if (params.mumCount > 0) std::cerr << "--optimal-alignment cannot be combined with --seeds-mum-count" << std::endl;
		if (params.memCount > 0) std::cerr << "--optimal-alignment cannot be combined with --seeds-mem-count" << std::endl;
		if (params.minimizerSeedDensity > 0) std::cerr << "--optimal-alignment cannot be combined with --seeds-minimizer-density" << std::endl;
		std::cerr << "pick only one seeding method" << std::endl;
		paramError = true;
	}
	if (pickedSeedingMethods == 0 && !params.optimalDijkstra)
	{
		std::cerr << "pick a seeding method" << std::endl;
		paramError = true;
	}
	if (pickedSeedingMethods > 1)
	{
		std::cerr << "pick only one seeding method" << std::endl;
		paramError = true;
	}
	if (params.Xdropcutoff > 0 && !params.preciseClipping)
	{
		std::cerr << "--X-drop is set but --precise-clipping is not, using default value of --precise-clipping .66" << std::endl;
		params.preciseClipping = true;
		params.preciseClippingIdentityCutoff = 0.66;
	}
	if (params.tryAllSeeds && vm.count("seeds-extend-density") && vm["seeds-extend-density"].as<double>() != -1)
	{
		std::cerr << "WARNING: --try-all-seeds and --seeds-extend-density are both set! --seeds-extend-density will be ignored" << std::endl;
		params.seedExtendDensity = -1;
	}
	if (resultSelectionMethods > 1)
	{
		std::cerr << "pick only one method for selecting outputted alignments" << std::endl;
		paramError = true;
	}

	if (paramError)
	{
		std::cerr << "run with option -h for help" << std::endl;
		std::exit(1);
	}

	if (params.outputCorrectedFile.size() >= 3 && params.outputCorrectedFile.substr(params.outputCorrectedFile.size()-3) == ".gz")
	{
		params.compressCorrected = true;
	}

	if (params.outputCorrectedClippedFile.size() >= 3 && params.outputCorrectedClippedFile.substr(params.outputCorrectedClippedFile.size()-3) == ".gz")
	{
		params.compressClipped = true;
	}

	omp_set_num_threads(params.numThreads);

	alignReads(params);

	return 0;
}

max_stars_repo_name: algbio/GraphChainer

max_issues_repo_name: algbio/GraphChainer

max_forks_repo_name: algbio/GraphChainer