knitr::opts_chunk$set(echo = TRUE)
In this homework we will write a function to read in
and summarize data files in the format of
the example file hmp.txt, which you can obtain from
canvas or GitHub. When string manipulation is
necessary, please use functions from stringr.
library(stringr)
- Determine the delimiter of the file
hmp.txtand use the appropriateread.function to read it into an R data frame namedhmp. Do not read the character strings in as factors. Save the dimensions ofhmpin a variablehmp.dim.
setwd("~/Desktop/课业文档/Statistics/STAT 341/SFUStat341/Homeworks")
hmp<-read.delim("hmp.txt",header=TRUE,sep="\t",stringsAsFactors = FALSE)
str(hmp)
hmp.dim<-dim(hmp)
- Extract the first 11 columns of
hmp, and save them as a data frame calledmarker.info. In the ninth column ofmarker.infoyou will see long character strings. Some of these contain the substringMITOCHONDRIA. Create a logical vectormitthat isTRUEfor strings that containMITOCHONDRIAandFALSEotherwise. Addmitto themarker.infodata frame.
marker.info<-data.frame(hmp[,1:11])
str(marker.info)
marker.info$mit<-regexpr("MITOCHONDRIA",marker.info[,9])>0
marker.info
-
Progamatically extract the remaining columns (that is, write code that uses
hmp.dimto calculate the indices of the columns to extract), transpose this subset and save as a matrixmarker.matrix. (Notice that transposing has coercedmarker.matrixto a matrix of characters and that the column names fromhmphave become the row names ofmarker.matrix.) Set the column names ofmarker.matrixto be the character strings in the first column ofmarker.info(strings that start withrs). Print the first six rows ofmarker.matrix. -
Background: The
NNentries inmarker.matrixare missing data. Other than missing data, the possible letters in a column ofmarker.matrixare given in theallelescolumn of the corresponding row ofmarker.info. For example, the possible letters in the first column ofmarker.matrixareAandT. The "minor" allele is the letter that is least common in the data, once missing data are excluded. Hint: Ifvvis a column ofmarker.matrix, thenstr_split_fixed(vv,pattern="",n=2)would be useful (str_split_fixedis fromstringr). Your task: Write a function calledMAsummary()that takes a column ofmarker.matrixand the correspondingallelesentry frommodel.infoas input and returns a list with elements (i)MA, the minor allele (the letter), (ii)MAF, the frequency of the minor allele (proportion of letters other thanNthat are theMA), and (iii)MAcount, a vector of minor allele counts for each entry of the input vector. Test your function on the first column ofmarker.matrixwithMAsummary(marker.matrix[,1],marker.info[1,"alleles"])Your function should find that theMAisA, theMAFis 0.0227, and the first six elements ofMAcountarec(NA,0,NA,NA,0,0). -
Create vectors
MAandMAFof lengthnrow(marker.info)and a matrixMAcountwith dimensiondim(marker.matrix). Use a for loop with indexito loop through the columns ofmarker.matrixand use the output ofMAsummary(marker.matrix[,i],marker.info[i,"alleles"])to fill in theith elements ofMAandMAF, and theith column ofMAcount. AddMAandMAFto themarker.infodata frame. CopyMAcounttomarker.matrix; that is, overwritemarker.matrixwithMAcount. -
Verify your code by (i) bundling
marker.infoandMAcountinto a list withmylist <- list(marker.info=marker.info,MAcount=MAcount), and (ii) usingidentical()to comparemylistto the output in the objecttestlistthat you can load from [http://people.stat.sfu.ca/~mcneney/Teaching/Stat341/Test/testhw2.rda]
setwd("~/Desktop/课业文档/Statistics/STAT 341/SFUStat341/Homeworks") hmp<-read.delim("hmp.txt",header=TRUE,sep="\t",stringsAsFactors = FALSE) str(hmp) hmp.dim<-dim(hmp)