# onehot


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Example Data

``` python
df = pd.DataFrame(
    {
        "site_seq": [
            "AASTYGVAKR",
            "GGSTYGVAKR",
            "AASTFGVAKR",
            "PPSTYGVAKK",
            "VVSTYGIARR",
        ]
    }
)
df.shape
```

``` python
df.head()
```

## Encoding

------------------------------------------------------------------------

<a href="https://github.com/sky1ove/kprot/blob/main/kprot/onehot.py#L17"
target="_blank" style="float:right; font-size:smaller">source</a>

### onehot_encode

``` python

def onehot_encode(
    sequences:Sequence, # aligned protein sequence windows
    transform_colname:bool=True, # shift feature names around the center residue
    n:int=20, # center position used for transformed column labels
)->DataFrame:

```

*One-hot encode aligned protein sequence windows.*

``` python
onehot_encode(df["site_seq"]).head()
```

------------------------------------------------------------------------

<a href="https://github.com/sky1ove/kprot/blob/main/kprot/onehot.py#L32"
target="_blank" style="float:right; font-size:smaller">source</a>

### onehot_encode_df

``` python

def onehot_encode_df(
    df:DataFrame, # dataframe containing aligned sequences
    seq_col:str='site_seq', # column containing the sequence strings
    kwargs:object
)->DataFrame: # forwarded to onehot_encode

```

*One-hot encode a sequence column from a dataframe.*

``` python
onehot = onehot_encode_df(df, seq_col="site_seq")
onehot.head()
```

## Clustering

------------------------------------------------------------------------

<a href="https://github.com/sky1ove/kprot/blob/main/kprot/onehot.py#L41"
target="_blank" style="float:right; font-size:smaller">source</a>

### run_kmeans

``` python

def run_kmeans(
    onehot:DataFrame, # one-hot encoded sequence matrix
    n:int=2, # number of clusters to fit
    seed:int=42, # random seed for KMeans
)->object:

```

*Fit KMeans to one-hot encoded features and return the assigned labels.*

``` python
run_kmeans(onehot, n=2)
```

------------------------------------------------------------------------

<a href="https://github.com/sky1ove/kprot/blob/main/kprot/onehot.py#L51"
target="_blank" style="float:right; font-size:smaller">source</a>

### filter_range_columns

``` python

def filter_range_columns(
    df:DataFrame, # one-hot encoded dataframe with position-prefixed column names
    low:int=-10, # lower bound for retained positions
    high:int=10, # upper bound for retained positions
)->DataFrame:

```

*Filter one-hot columns to a position window around the center residue.*

``` python
onehot_10 = filter_range_columns(onehot, low=-10, high=10)
onehot_10.head()
```

## Elbow Method

------------------------------------------------------------------------

<a href="https://github.com/sky1ove/kprot/blob/main/kprot/onehot.py#L62"
target="_blank" style="float:right; font-size:smaller">source</a>

### get_clusters_elbow

``` python

def get_clusters_elbow(
    encoded_data:DataFrame, # one-hot encoded feature matrix
    max_cluster:int=400, # largest cluster count to evaluate
    interval:int=50, # step size between tested cluster counts
)->None:

```

*Plot the elbow curve for a range of KMeans cluster counts.*

``` python
get_clusters_elbow(onehot, max_cluster=5, interval=2)
```
