select_top_genes
TopGeneSelector
Class for selecting top n genes by k-mer coverage from transcripts.
This class contains methods for obtaining the top n genes in a transcriptome according to their k-mer coverage. k-mer coverage is used here to roughly measure the extent to which genes were expressed in the RNA-seq libraries from which the transcriptomes were derived.
Since k-mer coverages are defined for individual transcripts rather than genes, this class defines the k-mer coverage of a gene as the maximum k-mer coverage among the gene's isoforms.
Note that this class always selects exactly n genes. It does NOT find the top n distinct gene k-mer coverages and then select the genes that have those coverages. Instead, it behaves as though it sorts the genes by k-mer coverage in descending order, and then selects the first n elements of that sorted collection. Among other things, this means that tie" may be broken arbitrarily; it is possible that the last element selected has the same k-mer coverage as another element that was not selected.
Efficiently obtaining both the IDs of the top n genes and the sequences of all transcript isoforms associated with those genes requires iterating over the sequences in the provided transcriptome twice---once for each of those two steps. Since sometimes it may only be necessary to obtain the IDs, and the sequences themselves can be ignored, this class contains two methods---one to obtain just the gene IDs and one for the actual sequences.
How the transcripts should best be iterated twice depends on the application and the available hardware resources. A typically slower, but less memory intensive, way of iterating twice is to simply parse the input FASTA file twice. In principle, this can be done for a FASTA file stored on disk without opening the file twice; fseek could be used instead. However, BioPython's Bio.SeqIO.parse function does not work well with seeking, so it is usually easier to simply call parse twice, opening the file each time. For applications and environments where memory is less of a concern, it may be faster to store all of the transcript sequences in memory (e.g., in a list) and simply iterate over the in-memory collection twice. This class tries to accommodate both cases (and possibly others) by allowing the client code to pass a nullary Callable returning an iterator over transcripts. Since this makes the interface somewhat cumbersome for common use cases, the class provides classmethods that construct TopGeneSelector objects with such Callables automatically.
Attributes:
| Name | Type | Description |
|---|---|---|
transcripts |
Function returning transcript SeqRecord iterator. |
|
top |
int
|
Number of top genes to select. |
parse_transcript_id |
Function to parse FASTA IDs into TranscriptIDs. |
Source code in src/rna_clique/select_top_genes.py
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 | |
__init__(transcripts, top, parse_transcript_id=default_parser)
Construct a TopGeneSelector for given transcripts and top gene count.
The first argument, transcripts, must be a nullary function returning an iterator over the transcript Bio.SeqRecrod objects from which to select the top genes. For convenience, this method also provides from_ classmethods. Each can construct a TopGeneSelector object from a more common object, such as a Path to the FASTA file containing the transcripts or a list of Bio.SeqRecord objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transcripts
|
Callable[[], Iterator[SeqRecord]]
|
Function to get transcript SeqRecord iterator. |
required |
top
|
int
|
Number of top genes to select. |
required |
parse_transcript_id
|
Callable[[str], TranscriptID]
|
Function to parse FASTA IDs into TranscriptIDs. |
default_parser
|
Source code in src/rna_clique/select_top_genes.py
from_path(path, *args, **kwargs)
classmethod
Get a TopGeneSelector from a Path to the transcripts FASTA file.
from_sequences(seqs, *args, **kwargs)
classmethod
Get a TopGeneSelector for a Collection of Bio.SeqRecord objects.
get_top_gene_seqs()
Get the Bio.SeqRecord objects of the top genes by k-mer coverage.
Source code in src/rna_clique/select_top_genes.py
get_top_genes()
Get the gene IDs of the top genes by k-mer coverage.