k1a  1.1
Accelerated functionalities for k1lib
k1a Namespace Reference

Classes

class  StrIter
 String iterator. More...
 
class  StrIterCat
 Line iterator from file. More...
 
struct  PyStrIterCat
 
class  StrIterInter
 Intermediate string iterator. More...
 
struct  PyStrIterInter
 

Typedefs

typedef std::string(* transformF) (std::string)
 

Functions

PyObject * k1a_system (PyObject *self, PyObject *args)
 
PyObject * k1a_test (PyObject *self, PyObject *args)
 
PyObject * k1a_str_split (PyObject *self, PyObject *args)
 Splits a string up into multiple pieces, respecting quotation marks. More...
 
PyObject * k1a_str_alpha_numeric (PyObject *self, PyObject *args)
 Gets rid of special characters and only retain alpha numeric characters. More...
 
PyObject * k1a_str_kmers (PyObject *self, PyObject *args)
 Returns all k-mers for the given string. More...
 
PyObject * k1a_str_has_number (PyObject *self, PyObject *args)
 Returns whether the string has any numeric characters inside them. More...
 
PyObject * PyStrIterCat_new (std::string fileName)
 Creates a new PyStrIterCat object. More...
 
PyObject * PyStrIterCat_new (PyTypeObject *type, PyObject *args, PyObject *kwargs)
 
PyObject * PyStrIterCat_conjugate (PyStrIterCat *self, PyObject *Py_UNUSED(ignored))
 
PyObject * PyStrIterInter_new (StrIter *og, transformF f)
 
void log_clear ()
 
PyObject * k1a_log_clear (PyObject *self, PyObject *args)
 
std::string demangle (const char *name)
 Demangles C++ signatures. More...
 
template<class T >
void log_print (T s)
 
template<class T >
void log_println (T s)
 

Variables

PyMethodDef K1aMethods []
 
struct PyModuleDef k1amodule
 
PyMethodDef PyStrIterCat_methods []
 
PyTypeObject PyStrIterCat_Type
 
PyTypeObject PyStrIterInter_Type
 
bool debug = false
 

Typedef Documentation

◆ transformF

typedef std::string(* k1a::transformF) (std::string)

Definition at line 13 of file StrIter.h.

Function Documentation

◆ demangle()

std::string k1a::demangle ( const char *  name)

Demangles C++ signatures.

Parameters
name
Returns
std::string

Definition at line 39 of file utils.cpp.

39  {
40  int status = -4; // some arbitrary value to eliminate the compiler warning
41 
42  // enable c++11 by passing the flag -std=c++11 to g++
43  std::unique_ptr<char, void (*)(void *)> res{
44  abi::__cxa_demangle(name, NULL, NULL, &status),
45  std::free};
46 
47  return (status == 0) ? res.get() : name;
48 }

◆ k1a_log_clear()

PyObject * k1a::k1a_log_clear ( PyObject *  self,
PyObject *  args 
)

Definition at line 23 of file utils.cpp.

23  {
24  log_clear();
25  Py_RETURN_NONE;
26 }
void log_clear()
Definition: utils.cpp:16
Here is the call graph for this function:

◆ k1a_str_alpha_numeric()

PyObject* k1a::k1a_str_alpha_numeric ( PyObject *  self,
PyObject *  args 
)

Gets rid of special characters and only retain alpha numeric characters.

Example:

# returns "abg 8 86"
k1a.str_alpha_numeric("abg$%^8,;86")

Definition at line 92 of file funcs.cpp.

92  {
93  char *_str, *str;
94  if (!PyArg_ParseTuple(args, "s", &_str)) return NULL;
95  str = (char *)malloc((strlen(_str) + 1) * sizeof(char));
96  int i = 0, j = 0;
97  bool stillSpecial = false;
98 
99  while (_str[i] != NULL) {
100  unsigned int a = _str[i];
101  if ((65 <= a && a <= 90) || (97 <= a && a <= 122) || (48 <= a && a <= 57)) { // legit characters
102  str[j] = a;
103  j++;
104  stillSpecial = false;
105  } else {
106  if (!stillSpecial) {
107  str[j] = ' ';
108  j++;
109  }
110  stillSpecial = true;
111  }
112  i++;
113  }
114  str[j] = '\0';
115  auto res = PyUnicode_FromString(str);
116  free(str);
117  return res;
118 }

◆ k1a_str_has_number()

PyObject* k1a::k1a_str_has_number ( PyObject *  self,
PyObject *  args 
)

Returns whether the string has any numeric characters inside them.

Example:

# returns True
k1a.str_has_number("gh365")
# returns False
k1a.str_has_number("ghj")

Definition at line 161 of file funcs.cpp.

161  {
162  char *str;
163  if (!PyArg_ParseTuple(args, "s", &str)) return NULL;
164  int i = 0;
165  while (str[i] != NULL) {
166  unsigned int a = str[i];
167  if (48 <= a && a <= 57) {
168  Py_INCREF(Py_True);
169  return Py_True;
170  }
171  i++;
172  }
173  Py_INCREF(Py_False);
174  return Py_False;
175 }

◆ k1a_str_kmers()

PyObject* k1a::k1a_str_kmers ( PyObject *  self,
PyObject *  args 
)

Returns all k-mers for the given string.

Example:

# returns ['012', '123', '234', '345', '456']
k1a.str_kmers("0123456", 3)

Definition at line 130 of file funcs.cpp.

130  {
131  char *str;
132  int k;
133  if (!PyArg_ParseTuple(args, "si", &str, &k)) return NULL;
134  PyObject *plist = PyList_New(0);
135 
136  bool inBlock = false;
137  int n = strlen(str);
138  if (n < k) return plist;
139 
140  for (int i = 0; i < n - (k - 1); i++) {
141  char tmp = str[i + k];
142  str[i + k] = '\0';
143  PyList_Append(plist, PyUnicode_FromString(str + i));
144  str[i + k] = tmp;
145  }
146  return plist;
147 }

◆ k1a_str_split()

PyObject* k1a::k1a_str_split ( PyObject *  self,
PyObject *  args 
)

Splits a string up into multiple pieces, respecting quotation marks.

Example:

# returns ['ab', "c'd.e'f"]
k1a.str_split("ab.c'd.e'f", ".")

Definition at line 52 of file funcs.cpp.

52  {
53  char *_str, *_delim, *str, *begin;
54  if (!PyArg_ParseTuple(args, "ss", &_str, &_delim)) return NULL;
55  char delim = _delim[0], quoteChar = '"';
56 
57  begin = str = (char *)malloc((strlen(_str) + 1) * sizeof(char));
58  strcpy(str, _str);
59  PyObject *plist = PyList_New(0);
60 
61  int i = 0;
62  bool inBlock = false;
63 
64  while (str[i] != NULL) {
65  char a = str[i];
66  if (a == delim && !inBlock) {
67  str[i] = NULL;
68  PyList_Append(plist, PyUnicode_FromString(begin));
69  begin = str + (i + 1);
70  } else if (!inBlock && (a == '"' || a == '\'')) { // new block
71  inBlock = true;
72  quoteChar = a;
73  } else if (inBlock && a == quoteChar)
74  inBlock = false; // exiting block
75  i++;
76  }
77  PyList_Append(plist, PyUnicode_FromString(begin));
78  free(str);
79  return plist;
80 }

◆ k1a_system()

PyObject* k1a::k1a_system ( PyObject *  self,
PyObject *  args 
)

Definition at line 17 of file funcs.cpp.

17  {
18  const char *command;
19  int sts;
20 
21  if (!PyArg_ParseTuple(args, "s", &command)) return NULL;
22  sts = system(command);
23  if (sts < 0) {
24  // TODO: add exception string
25  return NULL;
26  }
27  return PyLong_FromLong(sts);
28 }

◆ k1a_test()

PyObject* k1a::k1a_test ( PyObject *  self,
PyObject *  args 
)

Definition at line 30 of file funcs.cpp.

30  {
31  const char *str;
32  if (!PyArg_ParseTuple(args, "s", &str)) return NULL;
33  const std::string a = str;
34  auto b = (PyStrIterCat *)PyStrIterCat_new(a);
35  auto f1 = [](std::string x) { return x + " |end"; };
36  auto f2 = [](std::string x) { return "begin| " + x; };
37  return b->val->transform(std::vector<transformF>{f1, f2}, true)->pyObj;
38  // return PyUnicode_FromString("def");
39  // return PyUnicode_FromString((a + "end").c_str());
40 }
PyObject * PyStrIterCat_new(std::string fileName)
Creates a new PyStrIterCat object.
Definition: StrIterCat.cpp:37
Here is the call graph for this function:

◆ log_clear()

void k1a::log_clear ( )

Definition at line 16 of file utils.cpp.

16  {
17  std::ofstream f;
18  f.open("/home/kelvin/repos/labs/k1a/logs.txt");
19  f << "";
20  f.close();
21 }

◆ log_print()

template<class T >
void k1a::log_print ( s)

Definition at line 13 of file utils.h.

13  {
14  std::ofstream f;
15  f.open("/home/kelvin/repos/labs/k1a/logs.txt", std::ios_base::app);
16  f << s;
17  f.close();
18 }

◆ log_println()

template<class T >
void k1a::log_println ( s)

Definition at line 21 of file utils.h.

21  {
22  log_print(s);
23  log_print("\n");
24 }
void log_print(T s)
Definition: utils.h:13
Here is the call graph for this function:

◆ PyStrIterCat_conjugate()

PyObject* k1a::PyStrIterCat_conjugate ( PyStrIterCat self,
PyObject *  Py_UNUSEDignored 
)

Definition at line 56 of file StrIterCat.cpp.

56  {
57  return PyUnicode_FromString("str_iter_conjugate");
58 };

◆ PyStrIterCat_new() [1/2]

PyObject * k1a::PyStrIterCat_new ( PyTypeObject *  type,
PyObject *  args,
PyObject *  kwargs 
)

Definition at line 44 of file StrIterCat.cpp.

44  {
45  PyStrIterCat *return_value = PyObject_New(PyStrIterCat, type);
46  char *fileName;
47  PyArg_ParseTuple(args, "s", &fileName);
48  return PyStrIterCat_new(std::string(fileName));
49 }
PyObject * PyStrIterCat_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
Definition: StrIterCat.cpp:44
Here is the call graph for this function:

◆ PyStrIterCat_new() [2/2]

PyObject * k1a::PyStrIterCat_new ( std::string  fileName)

Creates a new PyStrIterCat object.

Parameters
fileName
Returns
PyObject*

Definition at line 37 of file StrIterCat.cpp.

37  {
38  if (debug) log_println("PyStrIterCat_new");
39  PyStrIterCat *res = PyObject_New(PyStrIterCat, &PyStrIterCat_Type);
40  res->val = new StrIterCat((PyObject *)res, fileName);
41  return (PyObject *)res;
42 }
bool debug
Definition: utils.cpp:14
PyTypeObject PyStrIterCat_Type
Definition: StrIterCat.cpp:64
void log_println(T s)
Definition: utils.h:21
Here is the call graph for this function:

◆ PyStrIterInter_new()

PyObject * k1a::PyStrIterInter_new ( StrIter og,
transformF  f 
)

Definition at line 18 of file StrIterInter.cpp.

18  {
19  PyStrIterInter *pyObj = PyObject_New(PyStrIterInter, &PyStrIterInter_Type);
20  char *fileName;
21  if (debug) log_println("PyStrIterInter_new");
22  pyObj->val = new StrIterInter((PyObject *)pyObj, og, f);
23  return (PyObject *)pyObj;
24 }
PyTypeObject PyStrIterInter_Type
Here is the call graph for this function:

Variable Documentation

◆ debug

bool k1a::debug = false

Definition at line 14 of file utils.cpp.

◆ K1aMethods

PyMethodDef k1a::K1aMethods
Initial value:
= {
{"system", k1a_system, METH_VARARGS, "Execute a shell command."},
{"test", k1a_test, METH_VARARGS,
"Test function for developing the library"},
{"clear", k1a_log_clear, METH_VARARGS, "Clear logs"},
{"str_split", k1a_str_split, METH_VARARGS,
"str_split(s, ch). Splits string into multiple fragments using a delimiter, respecting quotes."
"\n\nExample: k1a.str_split(\"'ab'cdb3\", \"b\") == [\"'ab'cd\", \"3\"]"},
{"str_alpha_numeric", k1a_str_alpha_numeric, METH_VARARGS, "str_alpha_numeric(s). Replaces characters that are not alpha numeric with white spaces. \n\nExample: str_alpha_numeric(\"4,.53-45ag\") == \"4 53 45ag\""},
{"str_kmers", k1a_str_kmers, METH_VARARGS, "str_kmers(s, k). Gets a string's k-mers. \n\nExample: k1a.str_kmers(\"abcde\", 2) == ['ab', 'bc', 'cd', 'de']"},
{"str_has_number", k1a_str_has_number, METH_VARARGS, "str_has_number(s). Returns whether the string has any numeric characters inside them."},
{NULL, NULL, 0, NULL}
}
PyObject * k1a_str_kmers(PyObject *self, PyObject *args)
Returns all k-mers for the given string.
Definition: funcs.cpp:130
PyObject * k1a_str_split(PyObject *self, PyObject *args)
Splits a string up into multiple pieces, respecting quotation marks.
Definition: funcs.cpp:52
PyObject * k1a_test(PyObject *self, PyObject *args)
Definition: funcs.cpp:30
PyObject * k1a_str_alpha_numeric(PyObject *self, PyObject *args)
Gets rid of special characters and only retain alpha numeric characters.
Definition: funcs.cpp:92
PyObject * k1a_log_clear(PyObject *self, PyObject *args)
Definition: utils.cpp:23
PyObject * k1a_system(PyObject *self, PyObject *args)
Definition: funcs.cpp:17
PyObject * k1a_str_has_number(PyObject *self, PyObject *args)
Returns whether the string has any numeric characters inside them.
Definition: funcs.cpp:161

Definition at line 177 of file funcs.cpp.

◆ k1amodule

struct PyModuleDef k1a::k1amodule
Initial value:
= {
PyModuleDef_HEAD_INIT, "k1a",
NULL,
-1,
PyMethodDef K1aMethods[]
Definition: funcs.cpp:177

Definition at line 177 of file funcs.cpp.

◆ PyStrIterCat_methods

PyMethodDef k1a::PyStrIterCat_methods[]
Initial value:
= {
{"conjugate", (PyCFunction)PyStrIterCat_conjugate, METH_NOARGS, "conjugate docs"},
{NULL, NULL}}
PyObject * PyStrIterCat_conjugate(PyStrIterCat *self, PyObject *Py_UNUSED(ignored))
Definition: StrIterCat.cpp:56

Definition at line 60 of file StrIterCat.cpp.

◆ PyStrIterCat_Type

PyTypeObject k1a::PyStrIterCat_Type

Definition at line 64 of file StrIterCat.cpp.

◆ PyStrIterInter_Type

PyTypeObject k1a::PyStrIterInter_Type

Definition at line 39 of file StrIterInter.cpp.