core

Helper Functions for the analysis

source

filter_variants

 filter_variants ()

source

count_variants

 count_variants (vcf_file)

Count the number of variants in a VCF file using subprocess.


source

read_vcf

 read_vcf (vcf_file)

Reads a VCF file, automatically detecting the header and using correct column names.


source

expand_multiallelic_variants

 expand_multiallelic_variants (df_vcf)

Extracts allele counts (RO, AO, DP) for each sample from the VCF DataFrame and expands multi-allelic variants into separate rows.


source

find_index

 find_index (format_value, field)

Finds the index of a specific field (e.g., RO, AO, DP) in the FORMAT column.


source

compute_frequencies

 compute_frequencies (df_counts)

Computes allele frequency (AF = AO / DP) for each sample in the dataset.

/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/fastcore/docscrape.py:230: UserWarning: Unknown section Parameters:
  else: warn(msg)
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/fastcore/docscrape.py:230: UserWarning: Unknown section Returns:
  else: warn(msg)

source

add_ann_info_to_df

 add_ann_info_to_df (df, info_column='INFO')

Extract the first ANN annotation from the INFO field and add as separate columns to a DataFrame.


source

extract_first_ann

 extract_first_ann (info_field)

Extract the first ANN annotation from a VCF INFO field.

# Test with the example
info_field = 'AB=0;ABP=0;AC=1;AF=0.142857;AN=7;AO=248;CIGAR=1X;DP=664;DPB=664;DPRA=0;EPP=13.1322;EPPR=3.03118;GTI=0;LEN=1;MEANALT=1;MQM=60;MQMR=60;NS=7;NUMALT=1;ODDS=61.2192;PAIRED=0.991935;PAIREDR=0.995192;PAO=0;PQA=0;PQR=0;PRO=0;QA=8364;QR=13932;RO=416;RPL=137;RPP=8.92931;RPPR=9.04449;RPR=111;RUN=1;SAF=122;SAP=3.15039;SAR=126;SRF=210;SRP=3.09382;SRR=206;TYPE=snp;technology.illumina=1;ANN=G|synonymous_variant|LOW|cgd1_340|cgd1_340|transcript|cgd1_340-RA|protein_coding|1/1|c.357A>G|p.Ser119Ser|570/1483|357/1194|119/397||,G|upstream_gene_variant|MODIFIER|cgd1_320|cgd1_320|transcript|cgd1_320-RA|protein_coding||c.-4699T>C|||||4675|,G|upstream_gene_variant|MODIFIER|cgd1_330|cgd1_330|transcript|cgd1_330-RA|protein_coding||c.-1174T>C|||||1095|,G|upstream_gene_variant|MODIFIER|cgd1_350|cgd1_350|transcript|cgd1_350-RA|protein_coding||c.-1490A>G|||||1323|,G|downstream_gene_variant|MODIFIER|cgd1_360|cgd1_360|transcript|cgd1_360-RA|protein_coding||c.*4665T>C|||||4665|'
result = extract_first_ann(info_field)
print(result)
# Should output: {'allele': 'G', 'type': 'synonymous_variant', 'impact': 'LOW', 'gene_id': 'cgd1_340'}
{'allele': 'G', 'type': 'synonymous_variant', 'impact': 'LOW', 'gene_id': 'cgd1_340'}
REF ALT AF_M7 AF_M5 AF_M4 AF_M6 AF_C3 AF_C2 AF_C1 INFO variant_type impact gene_id allele
#CHROM POS
CM000429 76625 A G 0.269231 0.350000 0.404762 0.314607 0.566265 0.303571 0.437500 AB=0;ABP=0;AC=1;AF=0.142857;AN=7;AO=248;CIGAR=... synonymous_variant LOW cgd1_340 G
82019 A T 0.908163 0.705882 0.779070 0.833333 0.628571 0.617284 0.690141 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=410;CIGAR=1X;DP=5... synonymous_variant LOW cgd1_360 T
82192 G A 0.894231 0.701493 0.666667 0.745098 0.471698 0.619565 0.602410 AB=0;ABP=0;AC=6;AF=0.857143;AN=7;AO=398;CIGAR=... missense_variant MODERATE cgd1_360 A
106518 C G 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=530;CIGAR=1X;DP=5... missense_variant MODERATE cgd1_493 G
116718 C A 1.000000 1.000000 1.000000 0.989796 1.000000 1.000000 1.000000 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=525;CIGAR=1X;DP=5... upstream_gene_variant MODIFIER cgd1_520 A
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
CM000436 1198389 T A 1.000000 0.981818 1.000000 1.000000 0.987654 1.000000 1.000000 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=531;CIGAR=1X;DP=5... missense_variant MODERATE cgd8_4800 A
1198626 A C 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.987013 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=626;CIGAR=1X;DP=6... 3_prime_UTR_variant MODIFIER cgd8_4800 C
1198636 G A 1.000000 1.000000 0.988889 1.000000 1.000000 1.000000 1.000000 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=650;CIGAR=1X;DP=6... 3_prime_UTR_variant MODIFIER cgd8_4800 A
1198801 C G 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=447;CIGAR=1X;DP=4... 3_prime_UTR_variant MODIFIER cgd8_4800 G
1210605 C T 0.827586 0.718310 0.711111 0.781818 0.673684 0.735294 0.747573 AB=0;ABP=0;AC=7;AF=1;AN=7;AO=464;CIGAR=1X;DP=6... synonymous_variant LOW cgd8_4840 T

515 rows × 14 columns


source

clean_axes

 clean_axes (ax, offset=10)

Customizes a matplotlib axes by removing top and right spines, and creating a broken axis effect where x and y axes don’t touch.


source

mod_hist_legend

 mod_hist_legend (ax, title=False)

*Creates a cleaner legend for histogram plots by using line elements instead of patches.

Motivation: - Default histogram legends show rectangle patches which can be visually distracting - This function creates a more elegant legend with simple lines matching histogram edge colors - Positions the legend outside the plot to avoid overlapping with data*


source

make_circos_plot

 make_circos_plot (data)

source

elbow_point

 elbow_point (values)

Find the elbow point in a curve using the maximum curvature method.

Type Details
values list The y-values of the curve.
Returns int The index of the elbow point.

source

kmeans_cluster_analysis

 kmeans_cluster_analysis (df, cluster_sizes, random_state=42,
                          features=None, figsize=(12, 6),
                          standardize=False, fill_na=False)

Perform K-means clustering analysis on a pandas DataFrame and visualize the results with both normalized inertia and silhouette scores on the same plot.

Type Default Details
df pandas.DataFrame The input data to cluster.
cluster_sizes list List of cluster sizes (k values) to evaluate.
random_state int 42 Random seed for reproducibility (default: 42).
features NoneType None List of column names to use for clustering. If None, all columns are used.
figsize tuple (12, 6) Figure size for the output plot (default: (12, 6)).
standardize bool False Whether to standardize the features (default: False).
fill_na bool False Whether to fill missing values with column means (default: False).
Returns tuple (figure, inertia_values, silhouette_values) - The matplotlib figure object,
the list of inertia values, and the list of silhouette scores.