sorry, I'm still not fully on board. in your example data, you have two tables for data, one with P1 and P2 in the first two rows and another table with P3 and P4 in the first two rows. For your analysis, do you want to _combine all four parents_ or do you want to run the same analysis that we gave you above, but all that's changing is the actual _name_ of the parents, i.e., instead of asking that P1 and P2 be homozygous, you start by asking that P3 and P4 are homozygous and so on. If you were to combine them, the initial test would change. If you simply want to re-run the same analysis, I recommend you make a separate table for every pair of parents and put the code I gave before into a function:
fab_analysis <- function(file){
# I saved your table above in a file called "test.txt"
test <- read.table(file, header = TRUE, stringsAsFactors = FALSE)
# making separate object just for parents
parents <- as.data.frame(t(test[1:2,-1]))
names(parents) = c("P1","P2")
# replacing "unused" with NA because NA is a native identifier of missing data
parents$P1 <- gsub("Unused", NA, parents$P1)
parents$P2 <- gsub("Unused", NA, parents$P2)
# now, I'm adding separate columns for the individual alleles of P1 and P2
# gsub has the syntax: gsub(pattern to be replaced, replacement, string to operate on)
parents$P1.all1 <- gsub("\\/.","", parents$P1) # this replaces the / and the letter after it with nothing
parents$P1.all2 <- gsub(".\\/","", parents$P1) # this replaces the / and the letter BEFORE it with nothing
parents$P2.all1 <- gsub("\\/.","", parents$P2)
parents$P2.all2 <- gsub(".\\/","", parents$P2)
offspring <- test[-c(1:2),] # removing the lines corresponding to the parents
offspring <- reshape2::melt(offspring, id.vars = "Ind", variable.name = "type") # changing the format using function melt of the library reshape2
offspring$all.1 <- gsub("\\/.","", offspring$value)
offspring$all.2 <- gsub(".\\/","", offspring$value)
# identify offspring where both parents are homozygous, but not the same
relevant_offspring <- subset(parents, ((P1.all1 == P1.all2) & (P2.all1 == P2.all2)) & P1 != P2 ) %>% rownames
offspring <- subset(offspring, type %in% relevant_offspring)
off_par <- merge(offspring , parents, by.x = "type", by.y = "row.names")
off_par$code <- with(off_par, ifelse(value == P2, "1", # homozygous match
ifelse( all.1 == all.2, "0", # if it's not a match, but still homozygous, we put 0
ifelse( (all.1 == P1.all1 | all.1 == P2.all1) & (all.2 == P1.all1 | all.2 == P2.all1) , "H", NA )
)
)
)
return(off_par[c("type", "Ind","code")] %>% reshape2::dcast(., Ind~type, value.var = "code"))
}
This function expects exactly the type of table you posted in your very first post. It does not care, however, what the names of the parents or the children are, i.e., whether there's P1 and P2 or P4 and P5 or NewParent1 and NewParent2, it won't matter as long as the _parents are always in the first two rows_. It will also always compare the genotypes of the children to whatever parent is given in the second row of the table.
For the example, I randomly renamed both children and parents and the results did not change.
> fab_analysis("~/Downloads/test.txt")
Ind M10 M4 M5
1 1 H 0 1
2 2 1 0 H
3 3 1 0 H
4 4 1 H 1
5 5 H H H
Tell me if your first output file is what you like and then I can extend my answer.
Step 2:
C.
Dear Cristian Thanks lot for your help i got this error while executing first part
but i solved like this
Now i succefully got M1 i.e single column but i am getting this error while running 2nd part saying unexpected [ and { symbols. these are the error
once again thanks lot for your help Regards
I edited my answer in my first comment. Check it out and tell me where you got. There were mistakes in my first code. For example, when you fetch a column:
the i-th value of that column:
There are brackets missing in your blue code.
Why are columns M2 and M3 discarded? P1 and P2 are different for both.
Sorry, can you explain the problem more clearly? Do you want to output columns M2 and M3 in the first step? Also, I still haven't sorted out the heterozygote bit. I am on it.
How do you define 'between them' when the parents are not homozygous or the data not available, M3 and M2 respectively.
What does this mean: ' i want to check whether P1 and P2 match for M1 or not' ? Do you only want to export columns where both parents are homozygous?
Dear Cristian Thanks lot for your help in solving this problem if anyone of the parent either P1 or P2 are not homozygous (i.e. like A/T etc) or the data not available i do not want them in output and i will consider them as monomorphic that is the reason M2 and M3 are not in output and I will not consider in my further calculations.
is it possible to give H (heterozygous status) for segregating population for example individual 1 and 5 in M1 column? because i will count them and use this in next calculations.
i run your code and it is generating individual files to each marker like M1. csv, M2.csv etc is it possible to get them all in one file? and i am getting following error Warning messages: 1: In if (dfOutput[i, ] == dfOutput[2, ]) { : the condition has length > 1 and only the first element will be used like this i am getting 5 erors i think one per marker once again i would like to say big thanks for you help Regards
The code above is now exporting the 3 columns into a single file.
It's also marking the heterozygotes. I think table 2 is as you wish.