I have a code in R that works. But I want to re-do it in python. I use R to use apply function in order to calculate minor allele frequency. Can someone tell me how such a code would look in python? I am using pandas to read the data in python.
JavaScript
x
9
1
##R-code
2
###Reading file
3
var_freq <- read_delim("./cichlid_subset.frq", delim = "t",
4
col_names = c("chr", "pos", "nalleles", "nchr", "a1", "a2"), skip = 1)
5
6
# find minor allele frequency
7
var_freq$maf <- var_freq %>% select(a1, a2) %>% apply(1, function(z) min(z))
8
9
I have read the file using pandas but I am struggling with the second part.
JavaScript
1
10
10
1
###Python code
2
###Reading file
3
var_freq = pd.read_csv("./cichlid_subset.frq",sep='t',header=None)
4
column_indices = [0,1,2,3,4,5]
5
new_names = ["chr", "pos", "nalleles", "nchr", "a1", "a2"]
6
old_names = df_snv_gnomad.columns[column_indices]
7
8
###Finding minor allele frequency
9
10
Insights will be appreciated.
Advertisement
Answer
Use:
JavaScript
1
7
1
# Read file
2
colnames = ["chr", "pos", "nalleles", "nchr", "a1", "a2"]
3
var_freq = pd.read_csv('./cichlid_subset.frq', sep='t', header=None, skiprows=1, names=colnames)
4
5
# Get MAF
6
var_freq['maf'] = var_freq[['a1','a2']].min(axis=1)
7