<commit_before># Import and Principal Component Analysis (PCA) of SNPs

In this tutorial, we describe how to use Asaph to perform principal component analysis (PCA) of SNP data.  This is a necessary preliminary step before moving on to localizing and genotyping inversions.  We assume that you already followed the instructions in the tutorial for [installing Asaph](installing-asaph.md).

## Data Preparation
Asaph assumes that the input VCF only contains biallelic SNPs.  Further, inversion detection works best when samples are all drawn from a single population and SNPs are all from a single chromosome (or chromosome arm).  This avoids confounding factors.  You can use [VCFTools](https://vcftools.github.io/) to filter the SNPs and samples accordingly.

## Importing Data
To create an Asaph project, we first need to import the data.  Feature hashing (the default setting) is used to construct a very small feature matrix.  A minimal command for importing biallelic SNPs from a VCF file would look like so:

```bash
$ asaph_import --workdir <workdir> \
	--vcf <path/to/vcf>
```

If your inversion is not detected in the later stages, you can adjust the minimum inversion size (as a fraction of SNPs) to change the number of dimensions.  The default is 10%.

```bash
$ asaph_import --workdir <workdir> \
	--vcf <path/to/vcf> \
	--min-inversion-fraction 0.05
```

If you only wish to use a subset of the samples in the VCF file, you can provide a populations file using the `--selected-samples` flag.  The populations file contains one group per line, with the first column indicating the population name, and the sample names separated by commas like so:

```
Population 1,Sample1,Sample2
Population 2,Sample3,Sample4
```

The sample ids have to match the ids in the VCF file.  If a sample name from the VCF file is not present in the populations file, that sample is ignored.

The work directory will be created and contain the resulting Asaph data structures such as a feature matrix.

## Principal Component Analysis (PCA)
In the next step, we perform PCA on the feature matrix. The `asaph_pca` script facilitates this:

```bash
$ asaph_pca --workdir <workdir> \
    train \
    --n-components 10
```

We will output the PCA coordinates for each sample.  To output the coordinates:

```bash
$ asaph_pca --workdir <workdir> \
	output-coordinates \
	--components 1 2 3 4 \<commit_msg>Remove reference to deprecated feature<commit_after># Import and Principal Component Analysis (PCA) of SNPs

In this tutorial, we describe how to use Asaph to perform principal component analysis (PCA) of SNP data.  This is a necessary preliminary step before moving on to localizing and genotyping inversions.  We assume that you already followed the instructions in the tutorial for [installing Asaph](installing-asaph.md).

## Data Preparation
Asaph assumes that the input VCF only contains biallelic SNPs.  Further, inversion detection works best when samples are all drawn from a single population and SNPs are all from a single chromosome (or chromosome arm).  This avoids confounding factors.  You can use [VCFTools](https://vcftools.github.io/) to filter the SNPs and samples accordingly.

## Importing Data
To create an Asaph project, we first need to import the data.  Feature hashing (the default setting) is used to construct a very small feature matrix.  A minimal command for importing biallelic SNPs from a VCF file would look like so:

```bash
$ asaph_import --workdir <workdir> \
	--vcf <path/to/vcf>
```

If your inversion is not detected in the later stages, you can adjust the minimum inversion size (as a fraction of SNPs) to change the number of dimensions.  The default is 10%.

```bash
$ asaph_import --workdir <workdir> \
	--vcf <path/to/vcf> \
	--min-inversion-fraction 0.05
```

The work directory will be created and contain the resulting Asaph data structures such as a feature matrix.

## Principal Component Analysis (PCA)
In the next step, we perform PCA on the feature matrix. The `asaph_pca` script facilitates this:

```bash
$ asaph_pca --workdir <workdir> \
    train \
    --n-components 10
```

We will output the PCA coordinates for each sample.  To output the coordinates:

```bash
$ asaph_pca --workdir <workdir> \
	output-coordinates \
	--components 1 2 3 4 \
{'fraction_non_alphanumeric': 0.06826654240447344, 'fraction_numerical': 0.0065237651444548, 'mean_word_length': 4.092526690391459, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5127945', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1128, 'n_words': 617}