queries¶
queries
¶
Composite read-only queries and reports.
These helpers join the hierarchy (projects → subjects → visits → samples)
and pivot the EAV metadata tables into wide-form tables suitable for
notebook analysis. Everything here is read-only: nothing in this module
issues INSERT/UPDATE/DELETE.
All functions take a cursor as their first argument, so callers control the transaction boundary just like with the CRUD modules:
from noxdb import queries, transaction
with transaction() as cur:
df = queries.samples_with_metadata(cur, project_id=1)
DataFrame-returning helpers import :mod:pandas lazily — pandas is an
optional dependency declared under the analysis extra in
pyproject.toml. If pandas is not installed the call raises an
ImportError with installation instructions.
Functions returning a dict (project_summary, integrity_check)
do not depend on pandas.
samples_for_project
¶
samples_for_project(cur, project_id: int, *, file_type: str | None = None, sample_type: str | None = None, has_files: bool | None = None, include_controls: bool = True) -> 'pd.DataFrame'
Return one row per sample in a project, joined with parent IDs.
Project membership lives entirely in project_samples; plate
controls (mockIP, anchor, NC) are linked there at import time, so
they come back from the same junction scan as the real samples. The
project_id column is therefore always the queried project.
Output columns: project_id, subject_id, subject_code,
visit_id, timepoint, sample_id, sample_name,
sample_type, SQR, SQRP, library, antibody_class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Project to scan. |
required |
file_type
|
str | None
|
Keep only samples that have at least one registered file of this type. |
None
|
sample_type
|
str | None
|
Keep only samples whose |
None
|
has_files
|
bool | None
|
If |
None
|
include_controls
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
samples_with_metadata
¶
samples_with_metadata(cur, project_id: int, keys: list[str] | None = None, *, include_visit_metadata: bool = True) -> 'pd.DataFrame'
Pivot EAV metadata into one row per sample (wide form).
The base columns come from
samples_for_project.
Each metadata key becomes a column. Sample-level keys are taken from
sample_metadata; visit-level keys from visit_metadata are
also added when include_visit_metadata is True, prefixed
with visit_ on name collisions.
Keys that do not match ^[A-Za-z_][A-Za-z0-9_]*$ are silently
dropped to keep DataFrame column names well-formed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Project to pivot. |
required |
keys
|
list[str] | None
|
Restrict to this set of metadata keys. |
None
|
include_visit_metadata
|
bool
|
Whether to also pivot visit-level keys. |
True
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
'pd.DataFrame'
|
per metadata key. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
controls_for_project
¶
controls_for_project(cur, project_id: int, *, sample_types: list[str] | None = None) -> 'pd.DataFrame'
Return the control samples linked to a project.
Controls (mockIP, anchor, NC) are linked into project_samples
at import time for every study project that shares their SQR+SQRP
plate coordinates, so this is a plain junction scan filtered to the
control sample_type\ s. A single physical control can be linked
to several projects — this query handles that naturally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Study project whose plate controls you want. |
required |
sample_types
|
list[str] | None
|
Control types to include. Defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
'pd.DataFrame'
|
|
'pd.DataFrame'
|
|
'pd.DataFrame'
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
list_inputs
¶
Return all input DNA samples.
Input samples are not associated with any study project. This is a
plain select of every sample stored under the input project.
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
'pd.DataFrame'
|
|
'pd.DataFrame'
|
|
'pd.DataFrame'
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
project_tidy_table
¶
Full tidy table for a project — every sample × every metadata key.
Convenience wrapper over
samples_with_metadata
with both sample- and visit-level metadata included. The intended
use is df.to_csv(...) / df.to_excel(...) for downstream
analysis in pandas or R.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Project to pivot. |
required |
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
files_for_project
¶
files_for_project(cur, project_id: int, *, file_type: str | None = None, storage_tier: str | None = None) -> 'pd.DataFrame'
Return one row per registered file in a project.
Columns include the sample_files row plus parent identifiers
(sample_name, subject_code, timepoint) so the result
can be filtered/grouped without further joins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Project to scan. |
required |
file_type
|
str | None
|
Optional |
None
|
storage_tier
|
str | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
project_summary
¶
Return counts for a project: subjects, visits, samples, files.
Does not raise if the project does not exist — the report just shows zeros. Has no pandas dependency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Project to summarize. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
``{"project_id", "n_subjects", "n_visits", "n_samples", |
dict[str, Any]
|
"n_files", "files_by_type"} |
dict[str, Any]
|
|
Source code in src/noxdb/queries.py
find_db_files_missing_on_disk
¶
Return DB-registered files whose path does not exist on disk.
Useful as a cron job to detect drift between the database and
storage. os.path.exists follows symlinks, so a dangling link
reads as missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int | None
|
Limit the scan to a single project; |
None
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
'pd.DataFrame'
|
|
'pd.DataFrame'
|
containing only rows whose path is missing. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
find_disk_files_missing_in_db
¶
Return regular files under roots that are NOT in sample_files.
This is a full filesystem walk; on a real archive it can take minutes. Restrict via roots in interactive use. Roots that don't exist on the current host are silently skipped, which makes the function safe to call from a laptop with no LiSC mount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
roots
|
list[str] | None
|
List of directories to walk. |
None
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
A |
'pd.DataFrame'
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If pandas is not installed. |
Source code in src/noxdb/queries.py
integrity_check
¶
Run a battery of sanity checks for a project. Read-only.
Does not touch the filesystem beyond realpath string operations
on the configured roots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur
|
Audit-logging cursor from |
required | |
project_id
|
int
|
Project to check. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A report dict with these keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Source code in src/noxdb/queries.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 | |