aboutsummaryrefslogtreecommitdiff
path: root/wqflask/base/species.py
blob: 68b00c702aff2a2a69e5b4f668a909c5b5d35e91 (plain)
1
2
3
4
5
6
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
from dataclasses import dataclass
from typing import Optional, Union
from collections import OrderedDict

from wqflask.database import database_connection


class TheSpecies:
    """Data related to species."""

    def __init__(self, dataset=None, species_name=None) -> None:
        "Initialise the Species object"
        self.dataset = dataset
        self.name = self.species_name = species_name
        self.chromosomes = Chromosomes(species=species_name,
                                       dataset=dataset)


@dataclass
class IndChromosome:
    """Data related to IndChromosome"""
    name: str
    length: int

    @property
    def mb_length(self) -> Union[int, float]:
        """Chromosome length in mega-bases"""
        return self.length / 1000000


@dataclass
class Chromosomes:
    """Data related to a chromosome"""

    def __init__(self, dataset, species: Optional[str]) -> None:
        "initialise the Chromosome object"
        self.species = species
        if species is None:
            self.dataset = dataset

    def chromosomes(self, db_cursor) -> OrderedDict:
        """Lazily fetch the chromosomes"""
        chromosomes = OrderedDict()
        if self.species is not None:
            db_cursor.execute(
                "SELECT Chr_Length.Name, Chr_Length.OrderId, Length "
                "FROM Chr_Length, Species WHERE "
                "Chr_Length.SpeciesId = Species.SpeciesId AND "
                "Species.Name = %s "
                "ORDER BY OrderId", (self.species.capitalize(),))
        else:
            db_cursor.execute(
                "SELECT Chr_Length.Name, Chr_Length.OrderId, "
                "Length FROM Chr_Length, InbredSet WHERE "
                "Chr_Length.SpeciesId = InbredSet.SpeciesId AND "
                "InbredSet.Name = "
                "%s ORDER BY OrderId", (self.dataset.group.name,))
        for name, _, length in db_cursor.fetchall():
            chromosomes[name] = IndChromosome(
                name=name, length=length)
        return chromosomes