Sanshine
Sanshine

Reputation: 615

Reshaping data to CSV

I`m working on a script to create csv files for an analysis. When running the script it gives 1 csv file for the OPA 5701 and 1 for the 6561. This is the only difference between the 2 parts of the script.

##Samplesheet for GS0005701
rows<-unique(samples$Sample_Name)
samplesheet<-rows
opa.panels<-sort(unique(samples$Pool_ID))
for ( i in 1:length(opa.panels)){
  samps<-samples[samples$Pool_ID == opa.panels[i],]
  idx<-match(samps$Sample_Name,rows)
  samplesheet<-cbind(samplesheet,samps$Sentrix_ID[idx],samps$Sentrix_Position[idx])
}
colnames(samplesheet)[2:(length(opa.panels)*2+1)]<-c("SentrixBarcode_A","SentrixPosition_A","SentrixBarcode_B","SentrixPosition_B","SentrixBarcode_C","SentrixPosition_C","SentrixBarcode_D","SentrixPosition_D")[1:(length(opa.panels)*2)]
colnames(samplesheet)[1]<-"Sample_Name"
idx<-match(rows,samples$Sample_Name)
samplesheet<-cbind(samplesheet,samples[idx,c("Sample_Group","NorTum","Sample")])
ss_header<-c("[Header]","Investigator Name,Sander","Project Name,HNPCC_NA_MYH","Experiment Name,OPA1+2+3+4","Date,5062012","[Manifests]")
for (i in 1:length(opa.panels)) ss_header<-c(ss_header,paste(LETTERS[i],opa.panels[i],sep=","))
ss_header<-c(ss_header,"[Data]")
writeLines(ss_header,"Samplesheet5701.csv")
write.table(samplesheet,file="Samplesheet5701.csv",sep=",",row.names=FALSE,quote=FALSE,append=TRUE,na="")

##Samplesheet for GS0006561-OPA
rows2<-unique(samples2$Sample_Name)
samplesheet2<-rows2
opa.panels2<-sort(unique(samples2$Pool_ID))
for ( j in 1:length(opa.panels2)){
  samps2<-samples2[samples2$Pool_ID == opa.panels2[j],]
  idx2<-match(samps2$Sample_Name,rows2)
  samplesheet2<-cbind(samplesheet2,samps2$Sentrix_ID[idx2],samps2$Sentrix_Position[idx2])
}
colnames(samplesheet2)[2:(length(opa.panels)*2+1)]<-c("SentrixBarcode_A","SentrixPosition_A","SentrixBarcode_B","SentrixPosition_B","SentrixBarcode_C","SentrixPosition_C","SentrixBarcode_D","SentrixPosition_D")[1:(length(opa.panels)*2)]
colnames(samplesheet2)[1]<-"Sample_Name"
idx2<-match(rows2,samples2$Sample_Name)
samplesheet2<-cbind(samplesheet2,samples2[idx2,c("Sample_Group","NorTum","Sample")])
ss_header<-c("[Header]","Investigator Name,Sander","Project Name,HNPCC_NA_MYH","Experiment Name,OPA1+2+3+4","Date,5062012","[Manifests]")
for (j in 1:length(opa.panels2)) ss_header<-c(ss_header,paste(LETTERS[j],opa.panels2[j],sep=","))
ss_header<-c(ss_header,"[Data]")
writeLines(ss_header,"samplesheet6561.csv")
write.table(samplesheet2,file="Samplesheet6561.csv",sep=",",row.names=FALSE,quote=FALSE,append=TRUE,na="")

The ## Samplesheet GS0005701 part creates an data.frame. While the ##Samplesheet GS0006561 creates an matrix. With the same code and the same input data.

The input data looks like this:

Sample Data

For copy paste:

    Sample  Sample_Name Sample_Group    NorTum  Sentrix_ID  Sentrix_Position    Pool_ID Folderdate
1   00-04193    00-04193N   HNPCC_UV    N   1495421 R007_C012   GS0006564-OPA   Exp060410
2   00-04193    00-04193N   HNPCC_UV    N   1495447 R007_C012   GS0006562-OPA   Exp060410
3   00-04193    00-04193N   HNPCC_UV    N   1495447 R007_C006   GS0006561-OPA   Exp060410
4   00-04193    00-04193N   HNPCC_UV    N   1495421 R007_C006   GS0006563-OPA   Exp060410
5   00-04193    00-04193N   HNPCC_UV    N   1460498 R007_C005   GS0006561-OPA   Exp060516
6   00-04193    00-04193N   HNPCC_UV    N   1460498 R007_C012   GS0006564-OPA   Exp060516

I know this is a difficult to answer question, but i`m hoping someone can give me a hint about how its possible that 1 code creates an data.frame and the other an matrix.

Very much thanks in advance!

Upvotes: 3

Views: 157

Answers (1)

Sanshine
Sanshine

Reputation: 615

The correct answer to this question was a swap in the index part.

idx<-match(samps$Sample_Name,rows)

was changed to:

idx<-match(rows,samps$Sample_Code) 

So that the length of rows is the same as the length of Sample_Code.

Upvotes: 1

Related Questions