Skip to content

Clonotype

A class to model the clonotype input data.

Attributes:

Name Type Description
id str

the label of the clonotype

alignment dict

the multiple sequence alignment for the BCR sequences

isotypes dict

the encoded isotypes of the sequenced B cells

forest list

a list of networkx.Digraphs containing the maximum parsimony forest for a the multiple sequence alignment

isotype_encoding list

an ordered list of the labels of the isotypes. This is important because naming conventions of isotype states, i.e., IgM, M, IghM, ighm, vary across datasets.

Source code in tribal/clonotype.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
@dataclass
class Clonotype:
    """A class to model the clonotype input data. 

    Attributes
    ----------
    id : str
        the label of the clonotype
    alignment : dict
        the multiple sequence alignment for the BCR sequences
    isotypes : dict
        the encoded isotypes of the sequenced B cells
    forest : list
        a list of networkx.Digraphs containing the maximum parsimony forest for a 
        the multiple sequence alignment
    isotype_encoding : list
        an ordered list of the labels of the isotypes. This is important because naming
        conventions of isotype states, i.e., IgM, M, IghM, ighm, vary across datasets.

    """
    id: str   #id label of the clonotype
    alignment: dict = None
    isotypes: dict = None
    forest: list = field(default_factory=list)
    isotype_encoding: list = None

    def generate_from_list(self, tree_list, root=None):
        """Populate the parsimony forst from a list of networkx digraphs.

        Parameters
        ----------
        tree_list : list
            a list of nx.DiGraphs containing the trees to populate the parsimony forest
        root : str, optional
            the root id of each B cell lineage tree

        """
        for i,t in enumerate(tree_list):
            if isinstance(t, BaseTree):
                t.set_id(i)
                self.add(t)
            else:
                self.add(BaseTree(t,root,i))

    def add(self, tree):
        """Add a tree to the parsimony forest.
        Parameters
        ----------
        tree : nx.DiGraph
            a tree to add to the parsimony forest
        """
        self.forest.append(tree)

    def __getitem__(self, key):
        """Slice a tree in the parsimony forest."""
        return self.forest[key]

    def size(self):
        """Return the size of the parsimony forest."""
        return len(self.forest)

    def get_forest(self):
        """Return the parsimony forest."""
        return self.forest

    def save(self, fname):
        """Pickle the clonotype.

        Parameters
        ----------
        fname : str
            the filename where the clonotype should be pickled.
        """
        with open(fname, 'wb') as file:
            pickle.dump(self, file)

__getitem__(key)

Slice a tree in the parsimony forest.

Source code in tribal/clonotype.py
60
61
62
def __getitem__(self, key):
    """Slice a tree in the parsimony forest."""
    return self.forest[key]

add(tree)

Add a tree to the parsimony forest.

Parameters:

Name Type Description Default
tree DiGraph

a tree to add to the parsimony forest

required
Source code in tribal/clonotype.py
51
52
53
54
55
56
57
58
def add(self, tree):
    """Add a tree to the parsimony forest.
    Parameters
    ----------
    tree : nx.DiGraph
        a tree to add to the parsimony forest
    """
    self.forest.append(tree)

generate_from_list(tree_list, root=None)

Populate the parsimony forst from a list of networkx digraphs.

Parameters:

Name Type Description Default
tree_list list

a list of nx.DiGraphs containing the trees to populate the parsimony forest

required
root str

the root id of each B cell lineage tree

None
Source code in tribal/clonotype.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def generate_from_list(self, tree_list, root=None):
    """Populate the parsimony forst from a list of networkx digraphs.

    Parameters
    ----------
    tree_list : list
        a list of nx.DiGraphs containing the trees to populate the parsimony forest
    root : str, optional
        the root id of each B cell lineage tree

    """
    for i,t in enumerate(tree_list):
        if isinstance(t, BaseTree):
            t.set_id(i)
            self.add(t)
        else:
            self.add(BaseTree(t,root,i))

get_forest()

Return the parsimony forest.

Source code in tribal/clonotype.py
68
69
70
def get_forest(self):
    """Return the parsimony forest."""
    return self.forest

save(fname)

Pickle the clonotype.

Parameters:

Name Type Description Default
fname str

the filename where the clonotype should be pickled.

required
Source code in tribal/clonotype.py
72
73
74
75
76
77
78
79
80
81
def save(self, fname):
    """Pickle the clonotype.

    Parameters
    ----------
    fname : str
        the filename where the clonotype should be pickled.
    """
    with open(fname, 'wb') as file:
        pickle.dump(self, file)

size()

Return the size of the parsimony forest.

Source code in tribal/clonotype.py
64
65
66
def size(self):
    """Return the size of the parsimony forest."""
    return len(self.forest)