File: write.cross.R

package info (click to toggle)
qtl 1.08-56-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,552 kB
  • ctags: 242
  • sloc: ansic: 8,903; makefile: 1
file content (401 lines) | stat: -rw-r--r-- 12,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
######################################################################
#
# write.cross.R
#
# copyright (c) 2001-7, Karl W Broman and Hao Wu
# last modified Sep, 2007
# first written Feb, 2001
# Licensed under the GNU General Public License version 2 (June, 1991)
#
# Part of the R/qtl package
# Contains: write.cross, write.cross.mm, write.cross.csv,
#           write.cross.gary, fixX4write
#           [See qtlcart_io.R for write.cross.qtlcart]
#
######################################################################


######################################################################
#
# write.cross: Wrapper for the other write.cross functions
#
######################################################################

write.cross <-
function(cross, format=c("csv", "csvr", "csvs", "csvsr", "mm", "qtlcart", "gary"),
         filestem="data", chr, digits=5)
{
  if(!any(class(cross) == "cross"))
    stop("Input should have class \"cross\".")

  format <- match.arg(format)
  if(!missing(chr)) cross <- subset(cross,chr=chr)

  # revise X data
  chrtype <- sapply(cross$geno,class)
  crosstype <- class(cross)[1]
  if((crosstype=="bc" || crosstype=="f2") &&
     any(chrtype=="X")) {
    sexpgm <- getsex(cross)
    sex <- sexpgm$sex
    pgm <- sexpgm$pgm
    for(i in which(chrtype=="X")) 
      cross$geno[[i]]$data <- fixX4write(cross$geno[[i]]$data,sex,pgm,crosstype)
  }

  if(format=="csv") write.cross.csv(cross,filestem,digits,FALSE,FALSE)
  else if(format=="csvr") write.cross.csv(cross,filestem,digits,TRUE,FALSE)
  else if(format=="csvs") write.cross.csv(cross,filestem,digits,FALSE,TRUE)
  else if(format=="csvsr") write.cross.csv(cross,filestem,digits,TRUE,TRUE)
  else if(format=="mm") write.cross.mm(cross,filestem,digits)
  else if(format=="qtlcart") write.cross.qtlcart(cross, filestem)
  else write.cross.gary(cross, digits)
}




######################################################################
#
# write.cross.mm: Write data for an experimental cross in Mapmaker
#                 format
#
#           creates two files: "raw" file with geno & pheno data
#                              "prep" file with map information
#
######################################################################

write.cross.mm <-
function(cross, filestem="data", digits=5)
{
  n.ind <- nind(cross)
  tot.mar <- totmar(cross)
  n.phe <- nphe(cross)
  n.chr <- nchr(cross)
  n.mar <- nmar(cross)
  
  type <- class(cross)[1]
  if(type=="riself" || type=="risib") type <- "bc"
  if(type != "f2" && type != "bc")
    stop("write.cross.mm only works for intercross, backcross and RI data.")

  # write genotype and phenotype data
  file <- paste(filestem, ".raw", sep="")
  
  # write experiment type
  if(type == "f2") 
    write("data type f2 intercross", file, append=FALSE)
  else 
    write("data type f2 backcross", file, append=FALSE)

  # write numbers of progeny, markers and phenotypes
  write(paste(n.ind, tot.mar, n.phe), file, append=TRUE)

  # max length of marker name
  mlmn <- max(nchar(unlist(lapply(cross$geno,function(a) colnames(a$data)))))+1

  # write marker data
  for(i in 1:n.chr) {
    for(j in 1:n.mar[i]) {
      mn <- paste("*", colnames(cross$geno[[i]]$data)[j], sep="")
      if(nchar(mn) < mlmn)
        mn <- paste(mn,paste(rep(" ", mlmn-nchar(mn)),collapse=""),sep="")
      g <- cross$geno[[i]]$data[,j]
      
      x <- rep("", n.ind)
      x[is.na(g)] <- "-"
      x[!is.na(g) & g==1] <- "A"
      x[!is.na(g) & g==2] <- "H"
      if(type == "f2") {
        x[!is.na(g) & g==3] <- "B"
        x[!is.na(g) & g==4] <- "D"
        x[!is.na(g) & g==5] <- "C"
      }

      if(n.ind < 60)
        write(paste(mn, paste(x,collapse="")), file, append=TRUE)
      else {
        lo <- seq(1,n.ind-1,by=60)
        hi <- c(lo[-1]-1,n.ind)
        for(k in seq(along=lo)) {
          if(k==1) write(paste(mn,paste(x[lo[k]:hi[k]],collapse="")),file,append=TRUE)
          else write(paste(paste(rep(" ", mlmn),collapse=""),
                           paste(x[lo[k]:hi[k]],collapse="")),file,append=TRUE)
        }
      }

    }
  } # end writing marker data

  # max length of phenotype name
  mlpn <- max(nchar(colnames(cross$pheno)))+1

  # write phenotypes
  for(i in 1:n.phe) {
    pn <- paste("*",colnames(cross$pheno)[i],sep="")
    if(nchar(pn) < mlpn)
      pn <- paste(pn, paste(rep(" ", mlpn-nchar(pn)),collapse=""),sep="")

    if(!is.factor(cross$pheno[,i]))
      x <- as.character(round(cross$pheno[,i],digits))
    else
      x <- as.character(cross$pheno[,i])
    x[is.na(x)] <- "-"

    if(n.ind < 10)
      write(paste(pn, paste(x,collapse="")), file, append=TRUE)
    else {
      lo <- seq(1,n.ind-1,by=10)
      hi <- c(lo[-1]-1,n.ind)
      for(k in seq(along=lo)) {
        if(k==1) write(paste(pn,paste(x[lo[k]:hi[k]],collapse=" ")),file,append=TRUE)
        else write(paste(paste(rep(" ", mlpn),collapse=""),
                         paste(x[lo[k]:hi[k]],collapse=" ")),file,append=TRUE)
      }
    }
  }
    

  # make "prep" file with map information
  file <- paste(filestem, ".prep", sep="")

  for(i in 1:n.chr) {
    cname <- paste("chr", names(cross$geno)[i], sep="")
    line <- paste("make chromosome", cname)
    if(i==1) write(line, file, append=FALSE)
    else write(line, file, append=TRUE)

    mn <- names(cross$geno[[i]]$map)
#    dis <- round(diff(cross$geno[[i]]$map),2)
#    dis <- paste("=", dis, sep="")
#    write(paste(paste("sequence", mn[1]), paste(dis,mn[-1],collapse=" ")),
#          file, append=TRUE)
    write(paste(paste("sequence", mn[1]), paste(mn[-1],collapse=" ")),
          file, append=TRUE)

    write(paste("anchor", cname), file, append=TRUE)
    write(paste("framework", cname), file, append=TRUE)
  } 

} 

######################################################################
#
# write.cross.csv: Write data for an experimental cross in
#                  comma-delimited format (the same format as is read
#                  by read.cross.csv)
#
######################################################################

write.cross.csv <-
function(cross, filestem="data", digits=5, rotate=FALSE, split=FALSE)
{
  type <- class(cross)[1]
  if(type != "f2" && type != "bc" && type != "riself" && type != "risib")
    stop("write.cross.csv only works for intercross, backcross and RI data.")

  if(!split) 
    file <- paste(filestem, ".csv", sep="")
  else {
    genfile <- paste(filestem, "_gen.csv", sep="")
    phefile <- paste(filestem, "_phe.csv", sep="")
  }
  
  if(split) { # split files; need individual IDs
    id <- getid(cross)
    if(is.null(id)) {
      cross$pheno$id <- 1:nind(cross)
      id <- getid(cross)
    }
    id.col <- which(colnames(cross$pheno)==attr(id,"phenam"))
  }

  n.ind <- nind(cross)
  tot.mar <- totmar(cross)
  n.phe <- nphe(cross)
  n.chr <- nchr(cross)
  n.mar <- nmar(cross)
  
  geno <- matrix(ncol=tot.mar,nrow=n.ind)

  # allele codes to use
  if("alleles" %in% names(attributes(cross))) {
    alle <- attr(cross, "alleles")
    alleles <- c(paste(alle[1],alle[1],sep=""),
                 paste(alle[1],alle[2],sep=""),
                 paste(alle[2],alle[2],sep=""),
                 paste("not ", alle[2],alle[2],sep=""),
                 paste("not ", alle[1],alle[1],sep=""))
  }
  else alleles <- c("A","H","B","D","C")

  firstmar <- 1
  for(i in 1:n.chr) {
    # replace allele numbers with 
    geno[,firstmar:(firstmar+n.mar[i]-1)] <-
      alleles[match(cross$geno[[i]]$data,1:5)]
    firstmar <- firstmar + n.mar[i]
  }
  if(any(is.na(geno))) geno[is.na(geno)] <- "-"
  pheno <- matrix(as.character(round(unlist(cross$pheno),digits)),nrow=n.ind)
  # factors: should be character by the levels rather than something like "1", "2", etc.
  for(i in 1:nphe(cross)) 
    if(is.factor(cross$pheno[,i])) pheno[,i] <- as.character(cross$pheno[,i])
  
  if(any(is.na(pheno))) pheno[is.na(pheno)] <- "-"
  thedata <- cbind(pheno,geno)
  colnames(thedata) <- c(colnames(cross$pheno),
                      unlist(lapply(cross$geno, function(a) colnames(a$data))))
  chr <- rep(names(cross$geno),n.mar)
  pos <- unlist(lapply(cross$geno,function(a) a$map))
  chr <- c(rep("",n.phe),chr)
  pos <- c(rep("",n.phe),as.character(round(pos,digits)))

  # put it all together
  thenames <- colnames(thedata)
  thedata <- matrix(as.character(thedata), ncol=ncol(thedata))
  thedata <- rbind(thenames, chr, pos, thedata)

  if(!split) {
    if(!rotate)
      write.table(thedata, file, quote=FALSE, sep=",",
                  row.names=FALSE, col.names=FALSE)
    else
      write.table(t(thedata), file, quote=FALSE, sep=",",
                  row.names=FALSE, col.names=FALSE)
  }
  else { # split files: one for phenotypes and one for genotypes
    n.phe <- nphe(cross)
    phe <- thedata[-(2:3),1:n.phe]
    gen <- cbind(thedata[,id.col], thedata[,-(1:n.phe)])

    if(!rotate) {
      write.table(gen, genfile, quote=FALSE, sep=",",
                  row.names=FALSE, col.names=FALSE)
      write.table(phe, phefile, quote=FALSE, sep=",",
                  row.names=FALSE, col.names=FALSE)
    }
    else {
      write.table(t(gen), genfile, quote=FALSE, sep=",",
                  row.names=FALSE, col.names=FALSE)
      write.table(t(phe), phefile, quote=FALSE, sep=",",
                  row.names=FALSE, col.names=FALSE)
    }

  }


}


######################################################################
#
# write.cross.gary: Write data for an experimental cross in
# Gary's format. There will be 6 output files, they are:
#    chrid.dat - chromosome ids
#    markerpos.txt - marker position
#    mnames.txt - marker names
#    geno.data - genotypes
#    pheno.data - phenotypes
#    pnames.txt - phenotype names
#
######################################################################

write.cross.gary <-
function(cross, digits)
{
  # local variables
  n.ind <- nind(cross)
  tot.mar <- totmar(cross)
  n.phe <- nphe(cross)
  n.chr <- nchr(cross)
  n.mar <- nmar(cross)

  # chromosome ids
  chrid <- NULL
  for(i in 1:n.chr) {
    # the name for this chromosome
    chrname <- names(cross$geno[i])
    # convert to number (why?)
#    if(chrname=="X") chrname <- 20
#    else chrname <- as.numeric(chrname)
    chrid <- c(chrid, rep(chrname, n.mar[i]))
  }
  write.table(chrid, file="chrid.dat", quote=FALSE, row.names=FALSE,
              col.names=FALSE)

  # marker position file
  markpos <- NULL
  for(i in 1:n.chr)
    markpos <- c(markpos, cross$geno[[i]]$map)
  write.table(markpos, file="markerpos.txt", quote=FALSE, sep="\t",
              row.names=TRUE, col.names=FALSE)

  # marker names
  mnames <- names(markpos)
  write.table(mnames, file="mnames.txt", quote=FALSE, row.name=FALSE,
              col.names=FALSE)

  # genotype
  geno <- NULL
  for(i in 1:n.chr)
    geno <- cbind(geno, cross$geno[[i]]$data)
  # note that gary's format codes genotype from 0
  # and 9 is for NA
  geno <- geno - 1 # note NA will still be NA
  write.table(geno, file="geno.dat", quote=FALSE, row.name=FALSE,
              col.name=FALSE, sep="\t", na="9")

  # phenotype
  pheno <- matrix(as.character(round(unlist(cross$pheno),digits)),nrow=n.ind)
  for(i in 1:nphe(cross)) 
    if(is.factor(cross$pheno[,i])) pheno[,i] <- as.character(cross$pheno[,i])

  write.table(pheno, file="pheno.dat", quote=FALSE, row.names=FALSE,
              col.names=FALSE, sep="\t", na="-999")
  # phenotype names
  write.table(names(cross$pheno), file="pnames.txt", quote=FALSE,
              row.names=FALSE, col.names=FALSE, sep="\t", na="-999")

}
                          

######################################################################
# fixX4write
######################################################################

fixX4write <-
function(geno,sex,pgm,crosstype)
{
  # males
  if(!is.null(sex) & any(sex==1)) { 
    temp <- geno[sex==1,,drop=FALSE]
    temp[temp==2] <- 3
    geno[sex==1,] <- temp
  }

  if(crosstype == "f2") {
    # females
    if(!is.null(pgm)) {
      if(!is.null(sex)) {
        if(any(pgm==1 & sex==0)) {
          temp <- geno[sex==0 & pgm==1,,drop=FALSE]
          temp[temp==1] <- 3
          geno[sex==0 & pgm==1,] <- temp
        }
      }
      else { # assume all females
        if(any(pgm==1)) {
          temp <- geno[pgm==1,,drop=FALSE]
          temp[temp==1] <- 3
          geno[pgm==1,] <- temp
        }
      }
    }

  }
      
  geno
}

# end of write.cross.R