Skip to content

Instantly share code, notes, and snippets.

@mr-eyes
Created September 23, 2022 23:26
Show Gist options
  • Save mr-eyes/c80d9caf43b26a57c6ca0bc2162d9d30 to your computer and use it in GitHub Desktop.
Save mr-eyes/c80d9caf43b26a57c6ca0bc2162d9d30 to your computer and use it in GitHub Desktop.
Call sourmash ani in C++
#include <Python.h>
#include <iostream>
using namespace std;
class toANI {
public:
PyObject* moduleMainString, * moduleMain, * func;
toANI() {
Py_Initialize();
PyRun_SimpleString("from sourmash.distance_utils import containment_to_distance\n");
moduleMainString = PyUnicode_FromString("__main__");
moduleMain = PyImport_Import(moduleMainString);
PyRun_SimpleString("def to_ani(a,b,c,d): return containment_to_distance(a, b, c, n_unique_kmers=d).ani");
func = PyObject_GetAttrString(moduleMain, "to_ani");
}
float calculate(float containment, long kSize, long scale, long n_unique_kmers) {
PyObject* args = PyTuple_Pack(4,
PyFloat_FromDouble(containment),
PyLong_FromLong(kSize),
PyLong_FromLong(scale),
PyLong_FromLong(n_unique_kmers)
);
PyObject* result = PyObject_CallObject(func, args);
return PyFloat_AsDouble(result);
}
};
int main() {
toANI ANI;
int kSize = 31;
float containment = 0.0174494;
int scale = 1000;
int n_unique_kmers = 4996000;
float ani = ANI.calculate(containment, kSize, scale, n_unique_kmers);
cout << "ani= " << ani << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment