Clonotyping API¶
Module for clonotype aggregation and the long-format views downstream analyses consume (selection-route heatmaps, per-method ranking, sample overlap, etc.).
clonotype ¶
Clonotype aggregation for TCRsift.
Groups cells by TCR CDR3 sequences to identify clonal populations.
aggregate_clonotypes ¶
aggregate_clonotypes(adata: AnnData, group_by: str = 'CDR3ab', min_umi: int = 2, handle_doublets: str = 'flag', attribution=None, doublet_warn_rate: float = 0.1, verbose: bool = True, show_progress: bool = True) -> pd.DataFrame
Aggregate cells into clonotypes based on CDR3 sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData with VDJ and phenotype information |
required |
group_by
|
str
|
How to group clones: "CDR3ab" (alpha+beta) or "CDR3b_only" (beta only) |
'CDR3ab'
|
min_umi
|
int
|
Minimum UMI count for a chain to be considered |
2
|
handle_doublets
|
str
|
How to handle cells with multiple chains: "flag", "remove", "keep-primary" |
'flag'
|
verbose
|
bool
|
Print detailed progress information |
True
|
show_progress
|
bool
|
Show progress bar |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with one row per unique clonotype |
Source code in tcrsift/clonotype.py
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 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
build_clone_sample_long ¶
Build a long-format (clone, sample) DataFrame from adata.obs.
One row per (CDR3ab, sample) pair where the clone has at least one
cell in that sample. Includes donor and method columns when
patient_id and enrichment_method are populated; same for
timepoint and apc_type. UMI sums (n_alpha_umis /
n_beta_umis) are emitted when the per-chain UMI columns are
present.
Both the per-clone cell count (numerator) and the frequency
denominator are restricted to complete-clone cells (both CDR3s
present and both chains >=2 UMI), mirroring the convention used by
max_frequency in aggregate_clonotypes so the two are directly
comparable. Consequently per-sample frequencies sum to 1.0 and
single-chain clones do not appear in the table (#175).
Implements #20 chunk 1 — surfaces the per-(clone, sample) view that
users were previously reconstructing by parsing the semicolon-
delimited samples string on clonotypes.csv.
Source code in tcrsift/clonotype.py
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 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 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 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 | |
build_clone_method_long ¶
build_clone_method_long(long_df: DataFrame, *, method_col: str = 'method', clone_col: str = 'CDR3ab', cells_col: str = 'cells', freq_col: str = 'frequency', sample_col: str = 'sample') -> pd.DataFrame
Per-(clone, method) cell-count + frequency table (#81).
Aggregates long_df (one row per (clone, sample)) into one row
per (clone, method) using the same denominator the clone_sample
table uses. This is the authoritative input for per-method picks,
selection-route heatmaps, per-method tier-eligibility queries.
Columns emitted:
CDR3ab,methodcells_in_method— sum of cells across samples within the methodmax_freq_in_method— max per-sample frequency observed in the methodn_samples_in_method— count of samples in which the clone appears
Downstream code was rolling this aggregation by hand in multiple
places, drifting on cells=max vs cells=sum. This function
is the single source of truth (see #81).
Source code in tcrsift/clonotype.py
compute_sample_overlap_matrices ¶
compute_sample_overlap_matrices(long_df: DataFrame, *, clone_col: str = 'CDR3ab', sample_col: str = 'sample', cells_col: str = 'cells', restrict_clones: set | None = None) -> dict[str, pd.DataFrame]
Sample × sample overlap matrices (#82).
Returns a dict with two square DataFrames indexed by sample:
"jaccard"— clone-set Jaccard. |A ∩ B| / |A ∪ B| over the sets of clones observed in each sample."cell_weighted_jaccard"— cell-weighted Jaccard. Σ min(cells_A[c], cells_B[c]) / Σ max(cells_A[c], cells_B[c]) over clones in A ∪ B. Captures abundance similarity, not just presence/absence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
long_df
|
DataFrame
|
Output of :func: |
required |
clone_col
|
str
|
Column names. |
'CDR3ab'
|
sample_col
|
str
|
Column names. |
'CDR3ab'
|
cells_col
|
str
|
Column names. |
'CDR3ab'
|
restrict_clones
|
set | None
|
When set, restrict the matrices to clones in this set. Useful for "selected clones only" / "tier1 only" variants. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, DataFrame]
|
|
Source code in tcrsift/clonotype.py
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 | |
build_per_method_rankings ¶
build_per_method_rankings(filtered: DataFrame, long_df: DataFrame, top_n: int = 100) -> dict[tuple[str, str], pd.DataFrame]
Build per-(donor, method) ranked clone tables.
For each populated (donor, method) pair in long_df, returns up
to top_n clones (those that survived filtering) ranked by their
max within-(donor, method) frequency descending. Each row is annotated
with the clone's tier (when filter_mode='fdr') and a derived
sharing label ("private" / "public" based on n_donors) so
output CSVs are self-describing regardless of which filter mode
produced filtered.
Returns {} when the long table doesn't carry a method axis —
designs that don't supply enrichment_method get no per-method
output. Implements #20 chunk 2.
Source code in tcrsift/clonotype.py
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 | |
build_method_overlap_matrices ¶
build_method_overlap_matrices(filtered: DataFrame, long_df: DataFrame, similarity: str = 'jaccard') -> dict[str, pd.DataFrame]
Per-donor method × method overlap matrices over filter-passing clones.
For each donor that has at least two distinct methods in long_df,
builds a symmetric (n_methods × n_methods) DataFrame whose off-diagonal
cells are pairwise overlap of selected-clone sets and whose diagonal is
the per-method count.
similarity ∈ {"jaccard", "dice", "count"}:
- jaccard: |A ∩ B| / |A ∪ B| — defaults to 0 when both empty.
Diagonal = 1.0.
- dice: 2|A ∩ B| / (|A| + |B|) — defaults to 0 when both
empty. Diagonal = 1.0.
- count: raw intersection counts. Diagonal = per-method clone
count for clarity.
Returns {} when long_df lacks the method axis or filtered
is empty. Implements #27 chunk 3.
Source code in tcrsift/clonotype.py
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 | |
build_method_recovery_table ¶
build_method_recovery_table(filtered: DataFrame, long_df: DataFrame, tier: str = 'tier1') -> pd.DataFrame
Per-(donor, method) recovery of tier-level filtered clones.
Returns a long DataFrame [donor, method, recovered, total, fraction]
where total is the number of clones in filtered carrying
tier for that donor (or all of filtered when tier=='*'),
and recovered is how many of those clones appear in long_df for
that (donor, method) bucket.
Implements #27 chunk 4 — backs the method-recovery report panel.
Source code in tcrsift/clonotype.py
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 | |
get_clonotype_summary ¶
Get summary statistics for clonotypes.
Returns:
| Type | Description |
|---|---|
dict
|
Summary statistics |
Source code in tcrsift/clonotype.py
export_clonotypes_airr ¶
Export clonotypes in AIRR format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame |
required |
output_path
|
str
|
Output file path (.tsv) |
required |
Source code in tcrsift/clonotype.py
calculate_clone_frequencies ¶
Calculate detailed frequency information for each clone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame from aggregate_clonotypes |
required |
adata
|
AnnData
|
Original AnnData with cell-level data |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Clonotypes with additional frequency columns |