-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add projection of samples with missing variants in PCA space #428
Open
maxibor
wants to merge
4
commits into
cggh:master
Choose a base branch
from
maxibor:pca_project
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This would provide a solution for most use cases for ancient DNA data (where the PCA is usually computed on modern samples, and ancient ones are then projected), and for issue #143 |
As a quick demonstration of the effect of missing data on PCA, and the benefit of projecting: First generating some synthetic datafrom allel.stats.decomposition import pca
import numpy as np
import pandas as pd
from plotnine import *
def generate_mutated_synthetic_data(n_variants=1000, n_ref_samples=100, n_project_samples=2, mutation_rate=0.05, missing_rate=0.1):
# Generate random genotype data (0, 1, 2) without missing data
variant_matrix = np.random.randint(0, 3, size=(n_variants, n_ref_samples))
# Generate one source sample
source_sample_idx = np.random.randint(0, n_ref_samples, 1)
# Create mutated samples from the source sample by mutating some of the variants
num_mutations = int(n_variants * mutation_rate)
mutated_samples = []
for i in range(n_project_samples):
mutated_sample = variant_matrix[:, source_sample_idx].copy().ravel()
mutation_indices = np.random.choice(n_variants, num_mutations, replace=False)
mutated_sample[mutation_indices] = np.random.randint(0, 3, size=num_mutations)
# Add missing data to the mutated samples
num_missing = int(n_variants * missing_rate)
missing_indices = np.random.choice(n_variants, num_missing, replace=False)
mutated_sample[missing_indices] = -1
mutated_samples.append(mutated_sample.reshape(-1, 1))
mutated_samples = np.hstack(mutated_samples)
return variant_matrix, mutated_samples, source_sample_idx[0]
bg, mut, src_idx = generate_mutated_synthetic_data(n_variants=9000, n_ref_samples=100, n_project_samples = 3, missing_rate=0.01) PCA of only background samples without missing variantscoords_bg, model_bg = pca(bg, n_components=2)
df_bg= pd.DataFrame(coords_bg, columns=[f"PC{i+1}" for i in range(model_bg.n_components)])
df_bg['samples'] = ['background'] * bg.shape[1]
df_bg['samples'][src_idx] = 'source'
ggplot(df_bg, aes(x='PC1', y='PC2', color='samples')) + geom_point() + theme_classic() PCA of all samples, including ones with missing variantscoords_all, model_all = pca(np.hstack((bg, mut)), n_components=2)
df_all = pd.DataFrame(coords_all, columns=[f"PC{i+1}" for i in range(model_all.n_components)])
df_all['samples'] = ['background'] * bg.shape[1] + ['mutated'] * mut.shape[1]
df_all['samples'][src_idx] = 'source'
ggplot(df_all, aes(x='PC1', y='PC2', color='samples')) + geom_point() + theme_classic() Now projecting the samples with missing variants onto the background samplesproj = model_bg.project(mut)
df_proj = pd.DataFrame(proj, columns=[f"PC{i+1}" for i in range(model_bg.n_components)])
df_proj['samples'] = ['background'] * bg.shape[1] + ['mutated'] * mut.shape[1]
df_proj['samples'][src_idx] = 'source'
ggplot(df_proj, aes(x='PC1', y='PC2', color='samples')) + geom_point() + theme_classic() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey @alimanfoo ,
This PR implements the least-square projection first introduced by Nick Patterson in smartPCA as the "lsqproject" option.
and described here: https://github.com/chrchang/eigensoft/blob/master/POPGEN/lsqproject.pdf
API demo