Discussion:
[R] Help with a function [along columns]
Kate Ignatius
2014-10-13 20:46:12 UTC
Permalink
Hi all,

I need help with a function. I'm trying to write a function to apply
to varying number of columns in a lot of files - hence the function...
but I'm getting stuck. Here it is:

gt<- function(x) {
alleles <- sapply(x, function(.) strsplit(as.character(.), "/"))
gt <- apply(x, function(.) ifelse(x[1] == vcf[3] & x[2] == vcf[3], 'RR',
ifelse(x[1] == vcf[4] & x[2] == vcf[4], 'AA',
ifelse(x[1] == vcf[3] & x[2] == vcf[4], 'RA',
ifelse(x[1] == vcf[4] & x[2] == vcf[3], 'RA', '')))))
}

I have different sized family genetic files and at the end of the day
I want to see whether the alleles of each person in the family match
the ref and/or the alt and if so, give AA, RA or RR.

Like so:

REF ALT Sample_1 GT_1 Sample_2 GT_2
A G A/A RR A/G RA
T G G/G AA T/T RR
A T T/T AA A/A RR
G A G/A RA G/G RR
G A G/G RR G/A RA
T C C/C AA C/C AA
T C C/C AA C/C AA
C T C/T RA T/T AA
G A A/A AA A/A AA
T G T/G RA G/G AA


Is there an easy way to do this?

Thanks!
Kate Ignatius
2014-10-14 00:54:51 UTC
Permalink
Just an update to this:

gtal <- function(d) {
alleles <- sapply(d, function(.) strsplit(as.character(.), "/"))
gt <- unlist(lapply(alleles, function(x)
ifelse(identical(x[[1]], vcf[,3]) & identical(x[[2]], vcf[,3]), 'RR',
ifelse(identical(x[[1]], vcf[,4]) & identical(x[[2]], vcf[,4]), 'AA',
ifelse(identical(x[[1]], vcf[,3]) & identical(x[[2]], vcf[,4]), 'RA',
ifelse(identical(x[[1]], vcf[,4]) & identical(x[[2]],
vcf[,3]), 'RA', ''))))))
}

I've got something working but I'm having trouble with the gt part...
I'm getting the error: object of type 'closure' is not subsettable.
The vcf is my original file that I want to match with so not sure
whether this a problem.
Post by Kate Ignatius
Hi all,
I need help with a function. I'm trying to write a function to apply
to varying number of columns in a lot of files - hence the function...
gt<- function(x) {
alleles <- sapply(x, function(.) strsplit(as.character(.), "/"))
gt <- apply(x, function(.) ifelse(x[1] == vcf[3] & x[2] == vcf[3], 'RR',
ifelse(x[1] == vcf[4] & x[2] == vcf[4], 'AA',
ifelse(x[1] == vcf[3] & x[2] == vcf[4], 'RA',
ifelse(x[1] == vcf[4] & x[2] == vcf[3], 'RA', '')))))
}
I have different sized family genetic files and at the end of the day
I want to see whether the alleles of each person in the family match
the ref and/or the alt and if so, give AA, RA or RR.
REF ALT Sample_1 GT_1 Sample_2 GT_2
A G A/A RR A/G RA
T G G/G AA T/T RR
A T T/T AA A/A RR
G A G/A RA G/G RR
G A G/G RR G/A RA
T C C/C AA C/C AA
T C C/C AA C/C AA
C T C/T RA T/T AA
G A A/A AA A/A AA
T G T/G RA G/G AA
Is there an easy way to do this?
Thanks!
Jim Lemon
2014-10-14 08:48:01 UTC
Permalink
Post by Kate Ignatius
gtal <- function(d) {
alleles <- sapply(d, function(.) strsplit(as.character(.), "/"))
gt <- unlist(lapply(alleles, function(x)
ifelse(identical(x[[1]], vcf[,3]) & identical(x[[2]], vcf[,3]),
'RR', ifelse(identical(x[[1]], vcf[,4]) & identical(x[[2]], vcf[,4]), 'AA',
ifelse(identical(x[[1]], vcf[,3]) & identical(x[[2]], vcf[,4]), 'RA',
ifelse(identical(x[[1]], vcf[,4]) & identical(x[[2]],
vcf[,3]), 'RA', ''))))))
}
I've got something working but I'm having trouble with the gt part...
I'm getting the error: object of type 'closure' is not subsettable.
The vcf is my original file that I want to match with so not sure
whether this a problem.
Hi Kate,
Unless you have passed "vcf" to your function, it is unlikely to recognize
it. As you are working with genome data, I suspect that there is a
function named "vcf" somewhere in the parent environment and you
can't subset a function.

Jim

Loading...