Commit 431c1c8c authored by Celine Mercier's avatar Celine Mercier

Merge branch 'Eric_new_Python_API' of

git@git.metabarcoding.org:obitools/obitools3.git into
Eric_new_Python_API

Conflicts:
	python/obitools3/obidms/_obidms.pxd
	python/obitools3/obidms/_obidms.pyx
	python/obitools3/obidms/_obidmscolumn_bool.pyx
	python/obitools3/obidms/_obidmscolumn_str.pyx
	python/obitools3/obidms/_obiseq.pxd
	python/obitools3/obidms/_obiseq.pyx
	python/obitools3/obidms/_obitaxo.pxd
	python/obitools3/obidms/_obitaxo.pyx
	python/obitools3/obidms/_obiview.pxd
	python/obitools3/obidms/_obiview.pyx
	python/obitools3/obidms/_obiview_nuc_seq.pxd
	python/obitools3/obidms/_obiview_nuc_seq.pyx
	python/obitools3/obidms/_obiview_nuc_seq_qual.pxd
	python/obitools3/obidms/_obiview_nuc_seq_qual.pyx
	python/obitools3/obidms/capi/obialign.pxd
	python/obitools3/obidms/capi/obidmscolumn.pxd
	python/obitools3/obidms/capi/obitaxonomy.pxd
	python/obitools3/obidms/capi/obiview.pxd
parents f23315e2 cf7f2de0
......@@ -36,7 +36,7 @@ extensions = [
'sphinx.ext.pngmath',
'sphinx.ext.ifconfig',
'sphinx.ext.viewcode',
# 'breathe',
'breathe',
]
# Add any paths that contain templates here, relative to this directory.
......@@ -295,4 +295,6 @@ texinfo_documents = [
sys.path.append( "breathe/" )
breathe_projects = { "OBITools3": "doxygen/xml/" }
breathe_default_project = "OBITools3"
#breathe_projects_source = {
# "auto" : ( "../src", ["obidms.h", "obiavl.h"] )
# }
\ No newline at end of file
......@@ -11,7 +11,7 @@ OBITools3 documentation
Programming guidelines <guidelines>
Data structures <data>
Code documentation <code_doc/codedoc>
Indices and tables
------------------
......
......@@ -4,6 +4,7 @@ OBITypes
.. image:: ./UML/OBITypes_UML.png
:download:`html version of the OBITypes UML file <UML/OBITypes_UML.class.violet.html>`
......
build/lib.macosx-10.6-intel-3.5
build/lib.macosx-10.6-intel-3.4
from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport
from obitools3.obidms._obidms import OBIDMS, OBIView, OBIView_line_selection # TODO cimport doesn't work
from functools import reduce
import time
__title__="Grep view lines that match the given predicates"
default_config = { 'inputview' : None,
'outputview' : None
}
def addOptions(parser):
# TODO put this common group somewhere else but I don't know where
group=parser.add_argument_group('DMS and view options')
group.add_argument('--default-dms','-d',
action="store", dest="obi:defaultdms",
metavar='<DMS NAME>',
default=None,
type=str,
help="Name of the default DMS for reading and writing data.")
group.add_argument('--input-view','-i',
action="store", dest="obi:inputview",
metavar='<INPUT VIEW NAME>',
default=None,
type=str,
help="Name of the input view, either raw if the view is in the default DMS,"
" or in the form 'dms:view' if it is in another DMS.")
group.add_argument('--output-view','-o',
action="store", dest="obi:outputview",
metavar='<OUTPUT VIEW NAME>',
default=None,
type=str,
help="Name of the output view, either raw if the view is in the default DMS,"
" or in the form 'dms:view' if it is in another DMS.")
group=parser.add_argument_group('obi grep specific options')
group.add_argument('--predicate','-p',
action="append", dest="grep:predicates",
metavar='<PREDICATE>',
default=None,
type=str,
help="Grep lines that match the given python expression on <line> or <sequence>.")
def run(config):
# Open DMS
d = OBIDMS(config['obi']['defaultdms'])
# Open input view 1
iview = d.open_view(config['obi']['inputview'])
# Initialize the progress bar
pb = ProgressBar(len(iview), config, seconde=5)
# Apply filter
selection = OBIView_line_selection(iview)
for i in range(len(iview)) :
pb(i)
line = iview[i]
loc_env = {'sequence': line, 'line': line} # TODO add taxonomy
good = (reduce(lambda bint x, bint y: x and y,
(bool(eval(p, loc_env, line))
for p in config['grep']['predicates']), True))
if good :
selection.append(i)
# Create output view with the line selection
oview = d.new_view(config['obi']['outputview'], line_selection=selection, comments="obi grep: "+str(config['grep']['predicates'])+"\n")
#print("\n")
#print(repr(oview))
iview.close()
oview.close()
d.close()
\ No newline at end of file
from .dms import DMS # @UnresolvedImport
#cython: language_level=3
from obitools3.obidms.capi.obidms cimport OBIDMS_p
from obitools3.obidms.capi.obitypes cimport const_char_p
cdef extern from "obi_align.h" nogil:
int obi_lcs_align_one_column(OBIDMS_p dms,
const_char_p seq_view_name,
const_char_p seq_column_name,
const_char_p seq_elt_name,
const_char_p id_column_name,
const_char_p output_view_name,
const_char_p output_view_comments,
bint print_seq,
bint print_count,
double threshold,
bint normalize,
int reference,
bint similarity_mode,
int thread_count)
int obi_lcs_align_two_columns(OBIDMS_p dms,
const_char_p seq1_view_name,
const_char_p seq2_view_name,
const_char_p seq1_column_name,
const_char_p seq2_column_name,
const_char_p seq1_elt_name,
const_char_p seq2_elt_name,
const_char_p id1_column_name,
const_char_p id2_column_name,
const_char_p output_view_name,
const_char_p output_view_comments,
bint print_seq,
bint print_count,
double threshold,
bint normalize,
int reference,
bint similarity_mode)
#cython: language_level=3
from .obitypes cimport const_char_p
cdef extern from "obidms.h" nogil:
struct OBIDMS_t:
const_char_p dms_name
ctypedef OBIDMS_t* OBIDMS_p
OBIDMS_p obi_dms(const_char_p dms_name)
int obi_close_dms(OBIDMS_p dms)
char* obi_dms_get_dms_path(OBIDMS_p dms)
char* obi_dms_get_full_path(OBIDMS_p dms, const_char_p path_name)
This diff is collapsed.
#cython: language_level=3
cdef extern from "obierrno.h":
int obi_errno
#cython: language_level=3
from .obitypes cimport const_char_p
from .obidms cimport OBIDMS_p
from libc.stdint cimport int32_t
cdef extern from "obidms_taxonomy.h" nogil:
struct ecotxnode :
int32_t taxid
int32_t rank
int32_t farest
ecotxnode* parent
char* name
char* preferred_name
ctypedef ecotxnode ecotx_t
struct ecotxidx_t :
int32_t count
int32_t max_taxid
int32_t buffer_size
ecotx_t* taxon
struct OBIDMS_taxonomy_t :
# ecorankidx_t* ranks
# econameidx_t* names
ecotxidx_t* taxa
ctypedef OBIDMS_taxonomy_t* OBIDMS_taxonomy_p
OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const_char_p taxonomy_name, bint read_alternative_names)
OBIDMS_taxonomy_p obi_read_taxdump(const_char_p taxdump)
int obi_write_taxonomy(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const_char_p tax_name)
int obi_close_taxonomy(OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_parent_at_rank(ecotx_t* taxon, int32_t rankidx)
ecotx_t* obi_taxo_get_taxon_with_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid)
bint obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid)
ecotx_t* obi_taxo_get_species(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_genus(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_family(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_kingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
int obi_taxo_add_local_taxon(OBIDMS_taxonomy_p tax, const char* name, const char* rank_name, int32_t parent_taxid, int32_t min_taxid)
int obi_taxo_add_preferred_name_with_taxid(OBIDMS_taxonomy_p tax, int32_t taxid, const char* preferred_name)
int obi_taxo_add_preferred_name_with_taxon(OBIDMS_taxonomy_p tax, ecotx_t* taxon, const char* preferred_name)
#cython: language_level=3
from libc.stdint cimport int32_t, int64_t, uint8_t
from posix.types cimport time_t
cdef extern from *:
ctypedef char* const_char_p "const char*"
cdef extern from "encode.h" nogil:
bint only_ATGC(const_char_p seq)
cdef extern from "obitypes.h" nogil:
enum OBIType:
OBI_VOID,
OBI_INT,
OBI_FLOAT,
OBI_BOOL,
OBI_CHAR,
OBI_QUAL,
OBI_STR,
OBI_SEQ,
OBI_IDX
ctypedef OBIType OBIType_t
enum OBIBool:
pass
ctypedef OBIBool obibool_t
ctypedef int32_t obiint_t
ctypedef double obifloat_t
ctypedef char obichar_t
ctypedef int64_t index_t
ctypedef int32_t obiversion_t
extern obiint_t OBIInt_NA
extern index_t OBIIdx_NA
extern obifloat_t OBIFloat_NA
extern obichar_t OBIChar_NA
extern obibool_t OBIBool_NA
extern const_char_p OBISeq_NA
extern const_char_p OBIStr_NA
extern const_char_p OBIQual_char_NA
extern uint8_t* OBIQual_int_NA
const_char_p name_data_type(int data_type)
ctypedef OBIType_t obitype_t
#cython: language_level=3
from posix.types cimport time_t
from ..capi.obitypes cimport const_char_p
cdef extern from "utils.h" nogil:
const_char_p obi_format_date(time_t date)
This diff is collapsed.
#cython: language_level=3
from .capi.obitypes cimport index_t, \
obitype_t
from .capi.obidmscolumn cimport OBIDMS_column_p
from .view cimport View
cdef class Column:
cdef OBIDMS_column_p* _pointer
cdef View _view
cpdef close(self)
cdef class Column_line:
cdef Column _column
cdef index_t _index
cpdef update(self, data)
cdef register_column_class(obitype_t obitype,
type classe,
type python)
#cython: language_level=3
from .capi.obitypes cimport name_data_type
from .capi.obidmscolumn cimport OBIDMS_column_header_p, \
obi_close_column, \
obi_column_prepare_to_get_value
from .capi.obiutils cimport obi_format_date
from .dms cimport __OBIDMS_COLUMN_CLASS__
from obitools3.utils cimport bytes2str
cdef class Column :
"""
The obitools3.dms.column.Column class wraps a C instance of a column in the context of a View
"""
# Note: should only be initialized through a subclass
def __init__(self,
View view,
int __internalCall__):
'''
Creates a new OBDMS column objected referring to a already created column
in the context of a view.
This constructor is normally only called by subclass constructor.
@param view: The view object containing the column.
@type view: OBIView
'''
cdef OBIDMS_column_p* column_pp
if __internalCall__!=987654:
raise RuntimeError('OBIView constructor cannot be called directly')
# Check that the class is only created as a subclass instance
if type(self)==Column or not isinstance(self, Column):
raise RuntimeError('OBIDMS.Column constructor cannot be called directly')
# Fill structure
self._pointer = NULL
self._view = view
def __len__(self):
'''
Implements the len() function for the Column class
@rtype: `int`
'''
return self.lines_used
def __sizeof__(self):
'''
returns the size of the C object wrapped by the Column instance
'''
cdef OBIDMS_column_header_p header = self._pointer[0].header
return header.header_size + header.data_size
def __iter__(self):
cdef index_t line_nb
for line_nb in range(self.lines_used):
yield self[line_nb]
def __str__(self) :
cdef str to_print
to_print = ''
for line in self :
to_print = to_print + str(line) + "\n"
return to_print
def __repr__(self) :
return b"%s, original name: %s, version %d, data type: %d" % (
self._alias,
self.original_name,
self.version,
self.data_type
)
cpdef close(self):
if self._pointer != NULL:
if obi_close_column(self._pointer[0]) < 0 :
raise Exception("Problem closing column %s" % bytes2str(self.name))
# Column alias property getter and setter
@property
def name(self):
return self._alias
@name.setter
def name(self, new_alias): # @DuplicatedSignature
self._view.change_column_alias(self._alias, new_alias)
# elements_names property getter
@property
def elements_names(self):
return (((self._pointer)[0].header).elements_names).split(b';')
# nb_elements_per_line property getter
@property
def nb_elements_per_line(self):
return ((self._pointer)[0].header).nb_elements_per_line
# data_type property getter
@property
def data_type(self):
return name_data_type(((self._pointer)[0].header).returned_data_type)
# original_name property getter
@property
def original_name(self):
return ((self._pointer)[0].header).name
# version property getter
@property
def version(self):
return ((self._pointer)[0].header).version
# lines_used property getter
@property
def lines_used(self):
return (self._pointer)[0].header.lines_used
# comments property getter
@property
def comments(self):
return (self._pointer)[0].header.comments
# creation_date property getter
@property
def creation_date(self):
return obi_format_date((self._pointer)[0].header.creation_date)
######################################################################################################
cdef class Column_line :
def __init__(self, Column column, index_t line_nb) :
self._index = line_nb
self._column = column
if obi_column_prepare_to_get_value(self._column._pointer[0],line_nb) < 0:
raise IndexError("Cannot access to the line %d" % line_nb)
def __contains__(self, str element_name):
pass
#return (element_name in self._column.elements_names)
def __repr__(self) :
return str(self._column.get_line(self._index))
cpdef update(self, data):
if isinstance(data, dict):
data=data.items()
for key,value in data:
if key in self:
self[key]=value
######################################################################################################
cdef register_column_class(obitype_t obitype,
type classe,
type python):
"""
Each sub class of `OBIDMS_column` needs to be registered after its declaration
to declare its relationship with an `OBIType_t`
"""
global __OBIDMS_COLUMN_CLASS__
assert issubclass(classe,Column)
__OBIDMS_COLUMN_CLASS__[obitype]=(classe,python)
from .column import column # @UnresolvedImport
\ No newline at end of file
#cython: language_level=3
from cpython.bool cimport bool, PyBool_FromLong
from ..capi.obitypes cimport index_t, \
const_char_p, \
OBIType_t, \
obibool_t, \
OBI_BOOL, \
OBIBool_NA
from ..capi.obiview cimport obi_get_bool_with_elt_name_and_col_p_in_view, \
obi_get_bool_with_elt_idx_and_col_p_in_view, \
obi_set_bool_with_elt_name_and_col_p_in_view, \
obi_set_bool_with_elt_idx_and_col_p_in_view
from ..capi.obidmscolumn cimport obi_column_get_obibool_with_elt_name, \
obi_column_get_obibool_with_elt_idx, \
obi_column_set_obibool_with_elt_name, \
obi_column_set_obibool_with_elt_idx, \
OBIDMS_column_p
from ..capi.obierrno cimport obi_errno
from .column cimport Column, \
Column_line, \
register_column_class
from obitools3.utils cimport str2bytes, bytes2str
cdef class Column_line_bool(OBIDMS_column_line) :
@staticmethod
cdef bool obibool_t2bool(obibool_t value)
@staticmethod
cdef bool2obibool_t(bool value)
cpdef bool get_bool_item_by_name(self,bytes element_name)
cpdef bool get_bool_item_by_idx(self,index_t index)
cpdef set_bool_item_by_name(self,bytes element_name,bool value)
cpdef set_bool_item_by_idx(self,index_t index,bool value)
# cdef obibool_t [:] _data_view
cdef class Column_bool(OBIDMS_column):
cdef Column _new(OBIView view,
bytes column_name,
index_t nb_elements_per_line=1,
object elements_names=None,
bytes comments=b""):
cpdef object get_line(self, index_t line_nb)
cpdef set_line(self, index_t line_nb, object value)
This diff is collapsed.
#cython: language_level=3
from ..capi.obitypes cimport index_t, \
obitype_t
from ..capi.obidmscolumn cimport OBIDMS_column_p
from ..view cimport View
cdef class Column:
cdef OBIDMS_column_p* _pointer
cdef View _view
cpdef close(self)
cdef class Column_line:
cdef Column _column
cdef index_t _index
cpdef update(self, data)
cdef register_column_class(obitype_t obitype,
type classe,
type matrix,
type python)
#cython: language_level=3
from .capi.obitypes cimport name_data_type
from .capi.obidmscolumn cimport OBIDMS_column_header_p, \
obi_close_column, \
obi_column_prepare_to_get_value
from .capi.obiutils cimport obi_format_date
from .dms cimport __OBIDMS_COLUMN_CLASS__
from obitools3.utils cimport bytes2str
cdef class Column :
"""
The obitools3.dms.column.Column class wraps a c instance of a column in the context of a View
"""
# Note: should only be initialized through a subclass
def __init__(self,
View view,
int __internalCall__):
'''
Create a new OBDMS column objected referring to a already created column
in the context of a view.
This constructor is normally only called by subclass constructor.
@param view: The view object containing the column.
@type view: OBIView
'''
cdef OBIDMS_column_p* column_pp
if __internalCall__!=987654:
raise RuntimeError('OBIView constructor cannot be called directly')
# Check that the class is only created as a subclass instance
if type(self)==Column or not isinstance(self, Column):
raise RuntimeError('OBIDMS.Column constructor cannot be called directly')
# Fill structure
self._pointer = NULL
self._view = view
@staticmethod
def new(OBIView view,
object column_name,
index_t nb_elements_per_line=1,
object elements_names=None,
object comments=b""):
cdef bytes column_name_b = tobytes(column_name)
cdef bytes comments_b
cdef bytes elements_names_b
cdef char* elements_names_p
cdef OBIDMS_column new_column
if comments is not None:
comments_b = tobytes(comments)
else:
comments_b = b''
if elements_names is not None:
elements_names_b = b''.join([tobytes(x) for x in elements_names])
elements_names_p = elements_names_b
else:
elements_names_p = NULL
if (obi_view_add_column(view = view._pointer,
column_name = column_name_b,
version_number = -1,
alias = NULL,
data_type = self.pointer.header.returned_data_type,
nb_lines = len(view),
nb_elements_per_line = nb_elements_per_line,
elements_names = elements_names_p,
indexer_name = NULL,
associated_column_name = NULL,
associated_column_version = -1,
comments = comments_b,
create = True)<0):
raise RuntimeError("Cannot create column %s in view %s" % (bytes2str(column_name),
bytes2str(view.name)))
view.__init_columns__()
new_column = self._columns[column_name]
return new_column
def __len__(self):
'''
implements the len() function for the Column class
@rtype: `int`
'''
return self.lines_used
def __sizeof__(self):
'''
returns the size of the C object wrapped by the Column instance