Skip to content

MakeDb Class API

MakeDb

Source code in src/AnnSQL/MakeDb.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class MakeDb:
	def __init__(self, adata=None, 	db_name=None, db_path="db/", create_all_indexes=False, create_basic_indexes=False, convenience_view=True, chunk_size=5000,make_buffer_file=False, layers=["X", "obs", "var", "var_names", "obsm", "varm", "obsp", "uns"], print_output=True):
		"""
		Initializes the MakeDb object. This object is used to create a database from an AnnData object by using the BuildDb method.

		Args:
			adata (AnnData, optional): The AnnData object to be used for creating the database.
			db_name (str, optional): The name of the database.
			db_path (str, optional): The path where the database will be created. Must have a trailing slash.
			create_all_indexes (bool, optional): Whether to create indexes for all layers in the database.
			create_basic_indexes (bool, optional): Whether to create indexes for basic layers in the database.
			convenience_view (bool, optional): Whether to create a convenience view for the database.
			chunk_size (int, optional): The number of cells to be processed in each chunk.
			make_buffer_file (bool, optional): Whether to create a buffer file for storing intermediate data. Necessary for low memory systems (<=12Gb).
			layers (list of str, optional): The layers to be included in the database.
			print_output (bool, optional): Whether to print output messages.

		Returns:
			None
		"""
		self.adata = adata
		self.db_name = db_name
		self.db_path = db_path
		if not self.db_path.endswith('/'): #add trailing slash
			self.db_path += '/'
		self.layers = layers
		self.create_all_indexes = create_all_indexes
		self.create_basic_indexes = create_basic_indexes
		self.convenience_view = convenience_view
		self.chunk_size = chunk_size
		self.make_buffer_file = make_buffer_file
		self.print_output = print_output
		self.validate_params()
		self.build_db()

	def validate_params(self):
		"""
		Validates the parameters required for creating a database.

		Raises:
			ValueError: If `db_name` is not provided or is not a string.
			ValueError: If `db_path` is not provided or is not a valid system path.
			ValueError: If `adata` is provided but is not an instance of `scanpy.AnnData`.
		"""

		if self.db_name is None:
			raise ValueError('db_name is required and must be a string')
		if self.db_path is None:
			raise ValueError('db_path is required and must be a valid system path')
		if self.adata is not None:
			if not isinstance(self.adata, sc.AnnData):
				raise ValueError('adata must be a scanpy AnnData object')

	def create_db(self):
		"""
		Creates a new database if it does not already exist.

		Raises:
			ValueError: If the database already exists.
		Returns:
			None
		"""
		if os.path.exists(self.db_path+self.db_name+'.asql'):
			raise ValueError('The database'+ self.db_path+self.db_name+'  exists already.')
		else:
			if not os.path.exists(self.db_path):
				os.makedirs(self.db_path)
			self.conn = duckdb.connect(self.db_path+self.db_name+'.asql')

	def build_db(self):
		"""
		Builds the database by creating it, executing the necessary SQL statements, and closing the connection.

		Parameters:
			None
		Returns:
			None
		"""
		self.create_db()
		BuildDb(adata=self.adata, conn=self.conn, create_all_indexes=self.create_all_indexes, create_basic_indexes=self.create_basic_indexes, convenience_view=self.convenience_view, layers=self.layers, chunk_size=self.chunk_size, db_path=self.db_path, db_name=self.db_name, make_buffer_file=self.make_buffer_file, print_output=self.print_output)
		self.conn.close()

__init__(adata=None, db_name=None, db_path='db/', create_all_indexes=False, create_basic_indexes=False, convenience_view=True, chunk_size=5000, make_buffer_file=False, layers=['X', 'obs', 'var', 'var_names', 'obsm', 'varm', 'obsp', 'uns'], print_output=True)

Initializes the MakeDb object. This object is used to create a database from an AnnData object by using the BuildDb method.

Parameters:

Name Type Description Default
adata AnnData

The AnnData object to be used for creating the database.

None
db_name str

The name of the database.

None
db_path str

The path where the database will be created. Must have a trailing slash.

'db/'
create_all_indexes bool

Whether to create indexes for all layers in the database.

False
create_basic_indexes bool

Whether to create indexes for basic layers in the database.

False
convenience_view bool

Whether to create a convenience view for the database.

True
chunk_size int

The number of cells to be processed in each chunk.

5000
make_buffer_file bool

Whether to create a buffer file for storing intermediate data. Necessary for low memory systems (<=12Gb).

False
layers list of str

The layers to be included in the database.

['X', 'obs', 'var', 'var_names', 'obsm', 'varm', 'obsp', 'uns']
print_output bool

Whether to print output messages.

True

Returns:

Type Description

None

Source code in src/AnnSQL/MakeDb.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(self, adata=None, 	db_name=None, db_path="db/", create_all_indexes=False, create_basic_indexes=False, convenience_view=True, chunk_size=5000,make_buffer_file=False, layers=["X", "obs", "var", "var_names", "obsm", "varm", "obsp", "uns"], print_output=True):
	"""
	Initializes the MakeDb object. This object is used to create a database from an AnnData object by using the BuildDb method.

	Args:
		adata (AnnData, optional): The AnnData object to be used for creating the database.
		db_name (str, optional): The name of the database.
		db_path (str, optional): The path where the database will be created. Must have a trailing slash.
		create_all_indexes (bool, optional): Whether to create indexes for all layers in the database.
		create_basic_indexes (bool, optional): Whether to create indexes for basic layers in the database.
		convenience_view (bool, optional): Whether to create a convenience view for the database.
		chunk_size (int, optional): The number of cells to be processed in each chunk.
		make_buffer_file (bool, optional): Whether to create a buffer file for storing intermediate data. Necessary for low memory systems (<=12Gb).
		layers (list of str, optional): The layers to be included in the database.
		print_output (bool, optional): Whether to print output messages.

	Returns:
		None
	"""
	self.adata = adata
	self.db_name = db_name
	self.db_path = db_path
	if not self.db_path.endswith('/'): #add trailing slash
		self.db_path += '/'
	self.layers = layers
	self.create_all_indexes = create_all_indexes
	self.create_basic_indexes = create_basic_indexes
	self.convenience_view = convenience_view
	self.chunk_size = chunk_size
	self.make_buffer_file = make_buffer_file
	self.print_output = print_output
	self.validate_params()
	self.build_db()

build_db()

Builds the database by creating it, executing the necessary SQL statements, and closing the connection.

Returns: None

Source code in src/AnnSQL/MakeDb.py
76
77
78
79
80
81
82
83
84
85
86
87
def build_db(self):
	"""
	Builds the database by creating it, executing the necessary SQL statements, and closing the connection.

	Parameters:
		None
	Returns:
		None
	"""
	self.create_db()
	BuildDb(adata=self.adata, conn=self.conn, create_all_indexes=self.create_all_indexes, create_basic_indexes=self.create_basic_indexes, convenience_view=self.convenience_view, layers=self.layers, chunk_size=self.chunk_size, db_path=self.db_path, db_name=self.db_name, make_buffer_file=self.make_buffer_file, print_output=self.print_output)
	self.conn.close()

create_db()

Creates a new database if it does not already exist.

Raises:

Type Description
ValueError

If the database already exists.

Returns: None

Source code in src/AnnSQL/MakeDb.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def create_db(self):
	"""
	Creates a new database if it does not already exist.

	Raises:
		ValueError: If the database already exists.
	Returns:
		None
	"""
	if os.path.exists(self.db_path+self.db_name+'.asql'):
		raise ValueError('The database'+ self.db_path+self.db_name+'  exists already.')
	else:
		if not os.path.exists(self.db_path):
			os.makedirs(self.db_path)
		self.conn = duckdb.connect(self.db_path+self.db_name+'.asql')

validate_params()

Validates the parameters required for creating a database.

Raises:

Type Description
ValueError

If db_name is not provided or is not a string.

ValueError

If db_path is not provided or is not a valid system path.

ValueError

If adata is provided but is not an instance of scanpy.AnnData.

Source code in src/AnnSQL/MakeDb.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def validate_params(self):
	"""
	Validates the parameters required for creating a database.

	Raises:
		ValueError: If `db_name` is not provided or is not a string.
		ValueError: If `db_path` is not provided or is not a valid system path.
		ValueError: If `adata` is provided but is not an instance of `scanpy.AnnData`.
	"""

	if self.db_name is None:
		raise ValueError('db_name is required and must be a string')
	if self.db_path is None:
		raise ValueError('db_path is required and must be a valid system path')
	if self.adata is not None:
		if not isinstance(self.adata, sc.AnnData):
			raise ValueError('adata must be a scanpy AnnData object')