Documentation for utils module

scdataloader.utils

Functions:

Name Description
createFoldersFor

will recursively create folders if needed until having all the folders required to save the file in this filepath

fileToList

loads an input file with a\n b\n.. into a list [a,b,..]

getBiomartTable

generate a genelist dataframe from ensembl's biomart

get_ancestry_mapping

This function generates a mapping of all elements to their ancestors in the ontology dataframe.

is_outlier

is_outlier detects outliers in adata.obs[metric]

length_normalize

length_normalize normalizes the counts by the gene length

listToFile

listToFile loads a list with [a,b,..] into an input file a\n b\n..

load_dataset_local

This function loads a remote lamindb dataset to local.

load_genes

Loads genes from the given organisms.

populate_my_ontology

creates a local version of the lamin ontologies and add the required missing values in base ontologies

random_str

Generate a random string of letters and digits

translate

translate translates the ontology term id to the name

validate

validate checks if the adata object is valid for lamindb

createFoldersFor

will recursively create folders if needed until having all the folders required to save the file in this filepath

Source code in scdataloader/utils.py
81
82
83
84
85
86
87
88
89
def createFoldersFor(filepath: str):
    """
    will recursively create folders if needed until having all the folders required to save the file in this filepath
    """
    prevval = ""
    for val in os.path.expanduser(filepath).split("/")[:-1]:
        prevval += val + "/"
        if not os.path.exists(prevval):
            os.mkdir(prevval)

fileToList

loads an input file with a\n b\n.. into a list [a,b,..]

Parameters:
  • input_str (str) –

    The input string to be completed.

Returns:
  • str( list ) –

    The completed string with 'complete' appended.

Source code in scdataloader/utils.py
24
25
26
27
28
29
30
31
32
33
34
35
def fileToList(filename: str, strconv: callable = lambda x: x) -> list:
    """
    loads an input file with a\\n b\\n.. into a list [a,b,..]

    Args:
        input_str (str): The input string to be completed.

    Returns:
        str: The completed string with 'complete' appended.
    """
    with open(filename) as f:
        return [strconv(val[:-1]) for val in f.readlines()]

getBiomartTable

generate a genelist dataframe from ensembl's biomart

Parameters:
  • ensemble_server (str, default: 'http://may2024.archive.ensembl.org/biomart' ) –

    the biomart server. Defaults to "http://may2023.archive.ensembl.org/biomart".

  • useCache (bool, default: False ) –

    whether to use the cache or not. Defaults to False.

  • cache_folder (str, default: '/tmp/biomart/' ) –

    the cache folder. Defaults to "/tmp/biomart/".

  • attributes (List[str], default: [] ) –

    the attributes to fetch. Defaults to [].

  • bypass_attributes (bool, default: False ) –

    whether to bypass the attributes or not. Defaults to False.

  • database (str, default: 'hsapiens_gene_ensembl' ) –

    the database to fetch from. Defaults to "hsapiens_gene_ensembl".

Raises:
  • ValueError

    should be a dataframe (when the result from the server is something else)

Returns:
  • pd.DataFrame: the dataframe

Source code in scdataloader/utils.py
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
def getBiomartTable(
    ensemble_server: str = "http://may2024.archive.ensembl.org/biomart",
    useCache: bool = False,
    cache_folder: str = "/tmp/biomart/",
    attributes: List[str] = [],
    bypass_attributes: bool = False,
    database: str = "hsapiens_gene_ensembl",
):
    """generate a genelist dataframe from ensembl's biomart

    Args:
        ensemble_server (str, optional): the biomart server. Defaults to "http://may2023.archive.ensembl.org/biomart".
        useCache (bool, optional): whether to use the cache or not. Defaults to False.
        cache_folder (str, optional): the cache folder. Defaults to "/tmp/biomart/".
        attributes (List[str], optional): the attributes to fetch. Defaults to [].
        bypass_attributes (bool, optional): whether to bypass the attributes or not. Defaults to False.
        database (str, optional): the database to fetch from. Defaults to "hsapiens_gene_ensembl".

    Raises:
        ValueError: should be a dataframe (when the result from the server is something else)

    Returns:
        pd.DataFrame: the dataframe
    """
    attr = (
        [
            "ensembl_gene_id",
            "hgnc_symbol",
            "gene_biotype",
            "entrezgene_id",
        ]
        if not bypass_attributes
        else []
    )
    assert cache_folder[-1] == "/"

    cache_folder = os.path.expanduser(cache_folder)
    createFoldersFor(cache_folder)
    cachefile = os.path.join(cache_folder, ".biomart.parquet")
    if useCache & os.path.isfile(cachefile):
        print("fetching gene names from biomart cache")
        res = pd.read_parquet(cachefile)
    else:
        print("downloading gene names from biomart")

        res = _fetchFromServer(ensemble_server, attr + attributes, database=database)
        res.to_parquet(cachefile, index=False)
    res.columns = attr + attributes
    if type(res) is not type(pd.DataFrame()):
        raise ValueError("should be a dataframe")
    res = res[~(res["ensembl_gene_id"].isna())]
    if "hgnc_symbol" in res.columns:
        res.loc[res[res.hgnc_symbol.isna()].index, "hgnc_symbol"] = res[
            res.hgnc_symbol.isna()
        ]["ensembl_gene_id"]
    return res

get_ancestry_mapping

This function generates a mapping of all elements to their ancestors in the ontology dataframe.

Parameters:
  • all_elem (list) –

    A list of all elements.

  • onto_df (DataFrame) –

    The ontology dataframe.

Returns:
  • dict

    A dictionary mapping each element to its ancestors.

Source code in scdataloader/utils.py
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
def get_ancestry_mapping(all_elem: List[str], onto_df: pd.DataFrame):
    """
    This function generates a mapping of all elements to their ancestors in the ontology dataframe.

    Args:
        all_elem (list): A list of all elements.
        onto_df (DataFrame): The ontology dataframe.

    Returns:
        dict: A dictionary mapping each element to its ancestors.
    """
    ancestors = {}
    full_ancestors = set()
    for val in all_elem:
        ancestors[val] = get_all_ancestors(val, onto_df) - set([val])

    for val in ancestors.values():
        full_ancestors |= set(val)
    # removing ancestors that are not in our datasets
    # full_ancestors = full_ancestors & set(ancestors.keys())
    leafs = set(all_elem) - full_ancestors
    full_ancestors = full_ancestors - leafs

    groupings = {}
    for val in full_ancestors:
        groupings[val] = set()
    for leaf in leafs:
        for ancestor in ancestors[leaf]:
            if ancestor in full_ancestors:
                groupings[ancestor].add(leaf)

    return groupings, full_ancestors, leafs

is_outlier

is_outlier detects outliers in adata.obs[metric]

Parameters:
  • adata (annData) –

    the anndata object

  • metric (str) –

    the metric column to use

  • nmads (int) –

    the number of median absolute deviations to use as a threshold

Returns:
  • pd.Series: a boolean series indicating whether a cell is an outlier or not

Source code in scdataloader/utils.py
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
def is_outlier(adata: AnnData, metric: str, nmads: int):
    """
    is_outlier detects outliers in adata.obs[metric]

    Args:
        adata (annData): the anndata object
        metric (str): the metric column to use
        nmads (int): the number of median absolute deviations to use as a threshold

    Returns:
        pd.Series: a boolean series indicating whether a cell is an outlier or not
    """
    M = adata.obs[metric]
    outlier = (M < np.median(M) - nmads * median_abs_deviation(M)) | (
        np.median(M) + nmads * median_abs_deviation(M) < M
    )
    return outlier

length_normalize

length_normalize normalizes the counts by the gene length

Parameters:
  • adata (anndata) –

    the anndata object

  • gene_lengths (list) –

    the gene lengths

Returns:
  • anndata

    the anndata object

Source code in scdataloader/utils.py
850
851
852
853
854
855
856
857
858
859
860
861
862
def length_normalize(adata: AnnData, gene_lengths: list):
    """
    length_normalize normalizes the counts by the gene length

    Args:
        adata (anndata): the anndata object
        gene_lengths (list): the gene lengths

    Returns:
        anndata: the anndata object
    """
    adata.X = csr_matrix((adata.X.T / gene_lengths).T)
    return adata

listToFile

listToFile loads a list with [a,b,..] into an input file a\n b\n..

Parameters:
  • l (list) –

    The list of elements to be written to the file.

  • filename (str) –

    The name of the file where the list will be written.

  • strconv (callable, default: lambda x: str(x) ) –

    A function to convert each element of the list to a string. Defaults to str.

Returns:
  • None

    None

Source code in scdataloader/utils.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def listToFile(
    li: List[str], filename: str, strconv: callable = lambda x: str(x)
) -> None:
    """
    listToFile loads a list with [a,b,..] into an input file a\\n b\\n..

    Args:
        l (list): The list of elements to be written to the file.
        filename (str): The name of the file where the list will be written.
        strconv (callable, optional): A function to convert each element of the list to a string. Defaults to str.

    Returns:
        None
    """
    with open(filename, "w") as f:
        for item in li:
            f.write("%s\n" % strconv(item))

load_dataset_local

This function loads a remote lamindb dataset to local.

Parameters:
  • lb (lamindb) –

    The lamindb instance.

  • remote_dataset (Dataset) –

    The remote Dataset.

  • download_folder (str) –

    The path to the download folder.

  • name (str) –

    The name of the dataset.

  • description (str) –

    The description of the dataset.

  • use_cache (bool, default: True ) –

    Whether to use cache. Defaults to True.

  • only (list, default: None ) –

    A list of indices to specify which files to download. Defaults to None.

Returns:
  • lamindb.Dataset: The local dataset.

Source code in scdataloader/utils.py
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
def load_dataset_local(
    remote_dataset: ln.Collection,
    download_folder: str,
    name: str,
    description: str,
    use_cache: bool = True,
    only: Optional[List[int]] = None,
):
    """
    This function loads a remote lamindb dataset to local.

    Args:
        lb (lamindb): The lamindb instance.
        remote_dataset (lamindb.Dataset): The remote Dataset.
        download_folder (str): The path to the download folder.
        name (str): The name of the dataset.
        description (str): The description of the dataset.
        use_cache (bool, optional): Whether to use cache. Defaults to True.
        only (list, optional): A list of indices to specify which files to download. Defaults to None.

    Returns:
        lamindb.Dataset: The local dataset.
    """
    saved_files = []
    default_storage = ln.Storage.filter(root=ln.settings.storage.as_posix()).one()
    files = (
        remote_dataset.artifacts.all()
        if not only
        else remote_dataset.artifacts.all()[only[0] : only[1]]
    )
    for file in files:
        organism = list(set([i.ontology_id for i in file.organism.all()]))
        if len(organism) > 1:
            print(organism)
            print("Multiple organisms detected")
            continue
        if len(organism) == 0:
            print("No organism detected")
            continue
        organism = bt.Organism.filter(ontology_id=organism[0]).one().name
        # bt.settings.organism = organism
        path = file.path
        try:
            file.save()
        except IntegrityError:
            print(f"File {file.key} already exists in storage")
        # if location already has a file, don't save again
        if use_cache and os.path.exists(os.path.expanduser(download_folder + file.key)):
            print(f"File {file.key} already exists in storage")
        else:
            path.download_to(download_folder + file.key)
        file.storage = default_storage
        try:
            file.save()
        except IntegrityError:
            print(f"File {file.key} already exists in storage")
        saved_files.append(file)
    dataset = ln.Collection(saved_files, name=name, description=description)
    dataset.save()
    return dataset

load_genes

Loads genes from the given organisms.

Parameters:
  • organisms (Union[str, List[str]], default: 'NCBITaxon:9606' ) –

    The organisms to load genes from.

Returns:
  • pd.DataFrame: The genes dataframe.

Source code in scdataloader/utils.py
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
def load_genes(
    organisms: Union[str, List[str]] = "NCBITaxon:9606",
):  # "NCBITaxon:10090",
    """
    Loads genes from the given organisms.

    Args:
        organisms (Union[str, List[str]]): The organisms to load genes from.

    Returns:
        pd.DataFrame: The genes dataframe.
    """
    organismdf = []
    if type(organisms) is str:
        organisms = [organisms]
    for organism in organisms:
        genesdf = bt.Gene.filter(
            organism_id=bt.Organism.filter(ontology_id=organism).first().id
        ).df()
        genesdf.loc[genesdf.ensembl_gene_id.isna(), "ensembl_gene_id"] = genesdf.loc[
            genesdf.ensembl_gene_id.isna(), "stable_id"
        ]
        genesdf = genesdf.drop_duplicates(subset="ensembl_gene_id")
        genesdf = genesdf.set_index("ensembl_gene_id").sort_index()
        # mitochondrial genes
        genesdf["mt"] = genesdf.symbol.astype(str).str.startswith("MT-")
        # ribosomal genes
        genesdf["ribo"] = genesdf.symbol.astype(str).str.startswith(("RPS", "RPL"))
        # hemoglobin genes.
        genesdf["hb"] = genesdf.symbol.astype(str).str.contains(("^HB[^(P)]"))
        genesdf["organism"] = organism
        organismdf.append(genesdf)
    organismdf = pd.concat(organismdf)
    for col in [
        "source_id",
        "run_id",
        "created_by_id",
        "updated_at",
        "stable_id",
        "created_at",
        "_aux",
        "_branch_code",
        "space_id",
        "ncbi_gene_ids",
        "synonyms",
        "description",
    ]:
        if col in organismdf.columns:
            organismdf.drop(columns=[col], inplace=True)
    # temp fix
    drop = {
        "ENSG00000112096",
        "ENSG00000137808",
        "ENSG00000161149",
        "ENSG00000182230",
        "ENSG00000203812",
        "ENSG00000204092",
        "ENSG00000205485",
        "ENSG00000212951",
        "ENSG00000215271",
        "ENSG00000221995",
        "ENSG00000224739",
        "ENSG00000224745",
        "ENSG00000225178",
        "ENSG00000225932",
        "ENSG00000226377",
        "ENSG00000226380",
        "ENSG00000226403",
        "ENSG00000227021",
        "ENSG00000227220",
        "ENSG00000227902",
        "ENSG00000228139",
        "ENSG00000228206",
        "ENSG00000228906",
        "ENSG00000229352",
        "ENSG00000231575",
        "ENSG00000232196",
        "ENSG00000232295",
        "ENSG00000233776",
        "ENSG00000236166",
        "ENSG00000236673",
        "ENSG00000236740",
        "ENSG00000236886",
        "ENSG00000236996",
        "ENSG00000237133",
        "ENSG00000237513",
        "ENSG00000237548",
        "ENSG00000237838",
        "ENSG00000239446",
        "ENSG00000239467",
        "ENSG00000239665",
        "ENSG00000244693",
        "ENSG00000244952",
        "ENSG00000249860",
        "ENSG00000251044",
        "ENSG00000253878",
        "ENSG00000254561",
        "ENSG00000254740",
        "ENSG00000255633",
        "ENSG00000255823",
        "ENSG00000256045",
        "ENSG00000256222",
        "ENSG00000256374",
        "ENSG00000256427",
        "ENSG00000256618",
        "ENSG00000256863",
        "ENSG00000256892",
        "ENSG00000258414",
        "ENSG00000258808",
        "ENSG00000258861",
        "ENSG00000259444",
        "ENSG00000259820",
        "ENSG00000259834",
        "ENSG00000259855",
        "ENSG00000260461",
        "ENSG00000261068",
        "ENSG00000261438",
        "ENSG00000261490",
        "ENSG00000261534",
        "ENSG00000261737",
        "ENSG00000261773",
        "ENSG00000261963",
        "ENSG00000262668",
        "ENSG00000263464",
        "ENSG00000267637",
        "ENSG00000268955",
        "ENSG00000269028",
        "ENSG00000269900",
        "ENSG00000269933",
        "ENSG00000269966",
        "ENSG00000270188",
        "ENSG00000270394",
        "ENSG00000270672",
        "ENSG00000271043",
        "ENSG00000271409",
        "ENSG00000271734",
        "ENSG00000271870",
        "ENSG00000272040",
        "ENSG00000272196",
        "ENSG00000272267",
        "ENSG00000272354",
        "ENSG00000272370",
        "ENSG00000272551",
        "ENSG00000272567",
        "ENSG00000272880",
        "ENSG00000272904",
        "ENSG00000272934",
        "ENSG00000273301",
        "ENSG00000273370",
        "ENSG00000273496",
        "ENSG00000273576",
        "ENSG00000273614",
        "ENSG00000273837",
        "ENSG00000273888",
        "ENSG00000273923",
        "ENSG00000276612",
        "ENSG00000276814",
        "ENSG00000277050",
        "ENSG00000277077",
        "ENSG00000277352",
        "ENSG00000277666",
        "ENSG00000277761",
        "ENSG00000278198",
        "ENSG00000278782",
        "ENSG00000278927",
        "ENSG00000278955",
        "ENSG00000279226",
        "ENSG00000279765",
        "ENSG00000279769",
        "ENSG00000279948",
        "ENSG00000280058",
        "ENSG00000280095",
        "ENSG00000280250",
        "ENSG00000280346",
        "ENSG00000280374",
        "ENSG00000280710",
        "ENSG00000282080",
        "ENSG00000282246",
        "ENSG00000282965",
        "ENSG00000283486",
        "ENSG00000284299",
        "ENSG00000284741",
        "ENSG00000285106",
        "ENSG00000285162",
        "ENSG00000285476",
        "ENSG00000285762",
        "ENSG00000286065",
        "ENSG00000286228",
        "ENSG00000286601",
        "ENSG00000286699",
        "ENSG00000286949",
        "ENSG00000286996",
        "ENSG00000287116",
        "ENSG00000287388",
        "ENSG00000288541",
        "ENSG00000288546",
        "ENSG00000288630",
        "ENSG00000288639",
        "ENSMUSG00000069518",
        "ENSMUSG00000073682",
        "ENSMUSG00000075014",
        "ENSMUSG00000075015",
        "ENSMUSG00000078091",
        "ENSMUSG00000094958",
        "ENSMUSG00000095547",
        "ENSMUSG00000095891",
        "ENSMUSG00000096385",
        "ENSMUSG00000096519",
        "ENSMUSG00000096923",
        "ENSMUSG00000097078",
    }
    organismdf = organismdf[~organismdf.index.isin(drop)]
    return organismdf

populate_my_ontology

creates a local version of the lamin ontologies and add the required missing values in base ontologies

run this function just one for each new lamin storage

erase everything with bt.$ontology.filter().delete()

add whatever value you need afterward like it is done here with:

bt.$ontology(name="ddd", ontolbogy_id="ddddd").save()

df["assay_ontology_term_id"].unique()

Parameters:
  • sex (list, default: ['PATO:0000384', 'PATO:0000383'] ) –

    List of sexes. Defaults to ["PATO:0000384", "PATO:0000383"].

  • celltypes (list, default: [] ) –

    List of cell types. Defaults to [].

  • ethnicities (list, default: [] ) –

    List of ethnicities. Defaults to [].

  • assays (list, default: [] ) –

    List of assays. Defaults to [].

  • tissues (list, default: [] ) –

    List of tissues. Defaults to [].

  • diseases (list, default: [] ) –

    List of diseases. Defaults to [].

  • dev_stages (list, default: [] ) –

    List of developmental stages. Defaults to [].

  • organisms_clade (list, default: ['vertebrates', 'plants', 'metazoa'] ) –

    List of organisms clade. Defaults to ["vertebrates", "plants"].

Source code in scdataloader/utils.py
658
659
660
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
800
801
802
803
804
805
def populate_my_ontology(
    sex: List[str] = ["PATO:0000384", "PATO:0000383"],
    celltypes: List[str] = [],
    ethnicities: List[str] = [],
    assays: List[str] = [],
    tissues: List[str] = [],
    diseases: List[str] = [],
    dev_stages: List[str] = [],
    organisms_clade: List[str] = ["vertebrates", "plants", "metazoa"],
    genes_from: List[str] = ["NCBITaxon:10090", "NCBITaxon:9606"],
):
    """
    creates a local version of the lamin ontologies and add the required missing values in base ontologies

    run this function just one for each new lamin storage

    erase everything with bt.$ontology.filter().delete()

    add whatever value you need afterward like it is done here with:

    `bt.$ontology(name="ddd", ontolbogy_id="ddddd").save()`

    `df["assay_ontology_term_id"].unique()`

    Args:
        sex (list, optional): List of sexes. Defaults to ["PATO:0000384", "PATO:0000383"].
        celltypes (list, optional): List of cell types. Defaults to [].
        ethnicities (list, optional): List of ethnicities. Defaults to [].
        assays (list, optional): List of assays. Defaults to [].
        tissues (list, optional): List of tissues. Defaults to [].
        diseases (list, optional): List of diseases. Defaults to [].
        dev_stages (list, optional): List of developmental stages. Defaults to [].
        organisms_clade (list, optional): List of organisms clade. Defaults to ["vertebrates", "plants"].
    """
    # cell type
    if celltypes is not None:
        if len(celltypes) == 0:
            bt.CellType.import_source()
        else:
            names = bt.CellType.public().df().index if not celltypes else celltypes
            records = bt.CellType.from_values(names, field="ontology_id")
            ln.save(records)
        bt.CellType(name="unknown", ontology_id="unknown").save()
    # OrganismClade
    if organisms_clade is not None:
        records = []
        for organism_clade in organisms_clade:
            names = bt.Organism.public(organism=organism_clade).df().index
            source = bt.Source.filter(name="ensembl", organism=organism_clade).last()
            for name in names:
                try:
                    records.append(bt.Organism.from_source(name=name, source=source))
                except DoesNotExist:
                    print(f"Organism {name} not found in source {source}")
        nrecords = []
        prevrec = set()
        for rec in records:
            if rec is None:
                continue
            if not isinstance(rec, bt.Organism):
                rec = rec[0]
            if rec.uid not in prevrec:
                nrecords.append(rec)
                prevrec.add(rec.uid)
        ln.save(nrecords)
        bt.Organism(name="unknown", ontology_id="unknown").save()
    # Phenotype
    if sex is not None:
        names = bt.Phenotype.public().df().index if not sex else sex
        source = bt.Source.filter(name="pato").first()
        records = [
            bt.Phenotype.from_source(ontology_id=i, source=source) for i in names
        ]
        ln.save(records)
        bt.Phenotype(name="unknown", ontology_id="unknown").save()
    # ethnicity
    if ethnicities is not None:
        if len(ethnicities) == 0:
            bt.Ethnicity.import_source()
        else:
            names = bt.Ethnicity.public().df().index if not ethnicities else ethnicities
            records = bt.Ethnicity.from_values(names, field="ontology_id")
            ln.save(records)
        bt.Ethnicity(
            name="unknown", ontology_id="unknown"
        ).save()  # multi ethnic will have to get renamed
    # ExperimentalFactor
    if assays is not None:
        if len(assays) == 0:
            bt.ExperimentalFactor.import_source()
        else:
            names = bt.ExperimentalFactor.public().df().index if not assays else assays
            records = bt.ExperimentalFactor.from_values(names, field="ontology_id")
            ln.save(records)
        bt.ExperimentalFactor(name="unknown", ontology_id="unknown").save()
        # lookup = bt.ExperimentalFactor.lookup()
        # lookup.smart_seq_v4.parents.add(lookup.smart_like)
    # Tissue
    if tissues is not None:
        if len(tissues) == 0:
            bt.Tissue.import_source()
        else:
            names = bt.Tissue.public().df().index if not tissues else tissues
            records = bt.Tissue.from_values(names, field="ontology_id")
            ln.save(records)
        bt.Tissue(name="unknown", ontology_id="unknown").save()
    # DevelopmentalStage
    if dev_stages is not None:
        if len(dev_stages) == 0:
            bt.DevelopmentalStage.import_source()
            source = bt.Source.filter(organism="mouse", name="mmusdv").last()
            bt.DevelopmentalStage.import_source(source=source)
        else:
            names = (
                bt.DevelopmentalStage.public().df().index
                if not dev_stages
                else dev_stages
            )
            records = bt.DevelopmentalStage.from_values(names, field="ontology_id")
            ln.save(records)
        bt.DevelopmentalStage(name="unknown", ontology_id="unknown").save()

    # Disease
    if diseases is not None:
        if len(diseases) == 0:
            bt.Disease.import_source()
        else:
            names = bt.Disease.public().df().index if not diseases else diseases
            records = bt.Disease.from_values(names, field="ontology_id")
            ln.save(records)
        bt.Disease(name="normal", ontology_id="PATO:0000461").save()
        bt.Disease(name="unknown", ontology_id="unknown").save()
    # genes
    for organism in genes_from:
        # convert onto to name
        organism = bt.Organism.filter(ontology_id=organism).one().name
        names = bt.Gene.public(organism=organism).df()["ensembl_gene_id"]

        # Process names in blocks of 10,000 elements
        block_size = 10000
        for i in range(0, len(names), block_size):
            block = names[i : i + block_size]
            records = bt.Gene.from_values(
                block,
                field="ensembl_gene_id",
                organism=organism,
            )
            ln.save(records)

random_str

Generate a random string of letters and digits

Parameters:
  • stringLength (int, default: 6 ) –

    the amount of char. Defaults to 6.

  • stype (str, default: 'all' ) –

    one of lowercase, uppercase, all. Defaults to 'all'.

  • withdigits (bool, default: True ) –

    digits allowed in the string? Defaults to True.

  • Returns
  • str

    random string

Source code in scdataloader/utils.py
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
def random_str(stringLength=6, stype="all", withdigits=True):
    """
    Generate a random string of letters and digits

    Args:
        stringLength (int, optional): the amount of char. Defaults to 6.
        stype (str, optional): one of lowercase, uppercase, all. Defaults to 'all'.
        withdigits (bool, optional): digits allowed in the string? Defaults to True.

        Returns:
        str: random string
    """
    if stype == "lowercase":
        lettersAndDigits = string.ascii_lowercase
    elif stype == "uppercase":
        lettersAndDigits = string.ascii_uppercase
    else:
        lettersAndDigits = string.ascii_letters
    if withdigits:
        lettersAndDigits += string.digits
    return "".join(random.choice(lettersAndDigits) for i in range(stringLength))

translate

translate translates the ontology term id to the name

Parameters:
  • val ((str, dict, set, list, dict)) –

    the object to translate

  • t (flat, default: 'cell_type_ontology_term_id' ) –

    the type of ontology terms. one of cell_type_ontology_term_id, assay_ontology_term_id, tissue_ontology_term_id. Defaults to "cell_type_ontology_term_id".

Returns:
  • dict

    the mapping for the translation

Source code in scdataloader/utils.py
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
def translate(
    val: Union[str, list, set, Counter, dict], t: str = "cell_type_ontology_term_id"
):
    """
    translate translates the ontology term id to the name

    Args:
        val (str, dict, set, list, dict): the object to translate
        t (flat, optional): the type of ontology terms.
            one of cell_type_ontology_term_id, assay_ontology_term_id, tissue_ontology_term_id.
            Defaults to "cell_type_ontology_term_id".

    Returns:
        dict: the mapping for the translation
    """
    if t == "cell_type_ontology_term_id":
        obj = bt.CellType
    elif t == "assay_ontology_term_id":
        obj = bt.ExperimentalFactor
    elif t == "tissue_ontology_term_id":
        obj = bt.Tissue
    elif t in [
        "development_stage_ontology_term_id",
        "simplified_dev_stage",
        "age_group",
    ]:
        obj = bt.DevelopmentalStage
    elif t == "disease_ontology_term_id":
        obj = bt.Disease
    elif t == "self_reported_ethnicity_ontology_term_id":
        obj = bt.Ethnicity
    elif t == "organism_ontology_term_id":
        obj = bt.Organism
    else:
        return None
    if type(val) is str:
        return {val: obj.filter(ontology_id=val).one().name}
    elif type(val) is dict or type(val) is Counter:
        return {obj.filter(ontology_id=k).one().name: v for k, v in val.items()}
    elif type(val) is set:
        return {i: obj.filter(ontology_id=i).one().name for i in val}
    else:
        rl = {i: obj.filter(ontology_id=i).one().name for i in set(val)}
        return [rl.get(i, None) for i in val]

validate

validate checks if the adata object is valid for lamindb

Parameters:
  • adata (anndata) –

    the anndata object

  • lb (lamindb) –

    the lamindb instance

  • organism (str) –

    the organism

Raises:
  • ValueError

    if the adata object is not valid

  • ValueError

    if the anndata contains invalid ethnicity ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid organism ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid sex ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid disease ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid cell_type ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid development_stage ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid tissue ontology term id according to the lb instance

  • ValueError

    if the anndata contains invalid assay ontology term id according to the lb instance

Returns:
  • bool

    True if the adata object is valid

Source code in scdataloader/utils.py
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
212
213
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
def validate(adata: AnnData, organism: str, need_all=False):
    """
    validate checks if the adata object is valid for lamindb

    Args:
        adata (anndata): the anndata object
        lb (lamindb): the lamindb instance
        organism (str): the organism

    Raises:
        ValueError: if the adata object is not valid
        ValueError: if the anndata contains invalid ethnicity ontology term id according to the lb instance
        ValueError: if the anndata contains invalid organism ontology term id according to the lb instance
        ValueError: if the anndata contains invalid sex ontology term id according to the lb instance
        ValueError: if the anndata contains invalid disease ontology term id according to the lb instance
        ValueError: if the anndata contains invalid cell_type ontology term id according to the lb instance
        ValueError: if the anndata contains invalid development_stage ontology term id according to the lb instance
        ValueError: if the anndata contains invalid tissue ontology term id according to the lb instance
        ValueError: if the anndata contains invalid assay ontology term id according to the lb instance

    Returns:
        bool: True if the adata object is valid
    """
    organism = bt.Organism.filter(ontology_id=organism).one().name

    if adata.var.index.duplicated().any():
        raise ValueError("Duplicate gene names found in adata.var.index")
    if adata.obs.index.duplicated().any():
        raise ValueError("Duplicate cell names found in adata.obs.index")
    for val in [
        "self_reported_ethnicity_ontology_term_id",
        "organism_ontology_term_id",
        "disease_ontology_term_id",
        "cell_type_ontology_term_id",
        "development_stage_ontology_term_id",
        "tissue_ontology_term_id",
        "assay_ontology_term_id",
    ]:
        if val not in adata.obs.columns and need_all:
            raise ValueError(
                f"Column '{val}' is missing in the provided anndata object."
            )

    if not bt.Ethnicity.validate(
        adata.obs["self_reported_ethnicity_ontology_term_id"],
        field="ontology_id",
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid ethnicity ontology term id found")
    if not bt.Organism.validate(
        adata.obs["organism_ontology_term_id"], field="ontology_id"
    ).all():
        raise ValueError("Invalid organism ontology term id found")
    if not bt.Phenotype.validate(
        adata.obs["sex_ontology_term_id"], field="ontology_id"
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid sex ontology term id found")
    if not bt.Disease.validate(
        adata.obs["disease_ontology_term_id"], field="ontology_id"
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid disease ontology term id found")
    if not bt.CellType.validate(
        adata.obs["cell_type_ontology_term_id"], field="ontology_id"
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid cell type ontology term id found")
    if not bt.DevelopmentalStage.validate(
        adata.obs["development_stage_ontology_term_id"],
        field="ontology_id",
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid dev stage ontology term id found")
    if not bt.Tissue.validate(
        adata.obs["tissue_ontology_term_id"], field="ontology_id"
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid tissue ontology term id found")
    if not bt.ExperimentalFactor.validate(
        adata.obs["assay_ontology_term_id"], field="ontology_id"
    ).all() and not set(adata.obs["self_reported_ethnicity_ontology_term_id"]) == set(
        ["unknown"]
    ):
        raise ValueError("Invalid assay ontology term id found")
    if not bt.Gene.validate(
        adata.var.index, field="ensembl_gene_id", organism=organism
    ).all():
        if not bt.Gene.validate(
            adata.var.index, field="stable_id", organism=organism
        ).all():
            raise ValueError("Invalid gene ensembl id found")
    return True