I have a matrix that looks something like this:
chr | pos | het |
---|---|---|
chrI | 27 | 2 |
chrI | 55 | 0 |
chrI | 27 | 0 |
chrI | 55 | 1 |
It's essentially a VCF file that I have converted into a TSV. I want to be able to take all the values in the het
column and apply this function:
het_df['het'] = het_df['het'].transform(lambda x: -1*(x-1))
That line is from a python script and it works really well in transforming that column. However, as I am making plots from this data I want to be able to do this in R. When I went to convert it to an R script I started with this function:
convert_df <- lapply(het_df[,3], FUN=function(x) formula=(-1*(x-1)))
As I saw it in this issue: https://stackoverflow.com/questions/7009333/how-to-use-lapply-with-a-formula
However, I keep getting error messages such as this one:
Error in x - 1 : non-numeric argument to binary operator
Which is odd because when I run het_df[,3]
it gives me the correct output:
[1] "2" "0" "1" "1" "1" "1" "1"
I think I'm just a little lost and I don't know what to do to run this function...everything worked fine in my python script (I was able to generate plots with matplotlib) but I like manipulating plots more in R than python so I wanted to be able to convert the script. If anyone is able to see what I'm clearly doing wrong please let me know! Thank you!
Here is the python code I've been using in case this helps (I'm transforming the last column so I always get a 1 or a 0 for heterozygosity/homosygosity and then using a rolling window to obtain the variability in the data series and plotting them):
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
# read in file
het_df = pd.read_csv("het_file.tsv", sep="\t", header=None).set_axis(['chr', 'pos', 'het'], axis=1, copy=False)
# getting rid of homozygous alt allele
# always get 1 for heterozygosity
het_df['het'] = het_df['het'].transform(lambda x: -1*(x-1))
windows = [25, 50, 100, 200] # 100 bp, get rid of a lot of noise
steps = [5]
for window in windows:
for step in steps:
het_ratio = {}
for chr_het_df in het_df.groupby('chr'):
# rolling over the window
# center = have the count in the center of the window
# 50bp window count in the front or back of window or count in the center
# ratio of heterozygosity
het_ratio[chr_het_df[0]] = chr_het_df[1].set_index('pos').rolling(window=window, step=step, center=True).mean()
fig, ax = plt.subplots(len(het_ratio), 1, sharex=True)
for index in range(len(het_ratio)):
chromosome = list(het_ratio.keys())[index]
sns.scatterplot(het_ratio[chromosome], ax=ax[index], s=4)
ax[index].set_ylim(bottom=0, top=1)
ax[index].legend([], [], frameon=False)
ax[index].set(xlabel="Chromosome position", ylabel=chromosome)
ax[index].set_yticks([0, 0.5, 1])
ax[index].set_yticklabels([0, 0.5, 1], size = 6)
fig.subplots_adjust(top=0.98, bottom=0.05, left=0.05, right=0.98)
fig.set_size_inches(20, 15)
plt.savefig("het_plot" + str(window) + "_step" + str(step) + ".png", dpi=150)
```