4 ##A bunch of r and r/qtl commands for reading input data,
5 ##running qtl analysis, and writing output data. Input data are feed to R
6 ##from and output data from R feed to a perl script called
7 ##../phenome/population_indls.
8 ##QTL mapping visualization is done by the perl script mentioned above.
9 ## r/qtl has functions for data visualization but the perl script
10 ## allows more control for publishing on the browser,
11 ##cross-referencing to other SGN datasets and CONSISTENT formatting/appearance
15 ## Isaak Y Tecle (iyt2@cornell.edu)
22 allargs
<-commandArgs()
24 infile
<- grep("infile_list",
25 allargs
, ignore
.case
=TRUE,
30 outfile
<-grep("outfile_list",
37 statfile
<-grep("stat",
46 statfiles
<-scan(statfile
,
51 ###### QTL mapping method ############
52 qtlmethodfile
<-grep("stat_qtl_method",
58 qtlmethod
<-scan(qtlmethodfile
,
63 if (qtlmethod
== "Maximum Likelihood")
68 if (qtlmethod
== "Haley-Knott Regression")
74 if (qtlmethod
== "Multiple Imputation")
79 if (qtlmethod
== "Marker Regression")
84 ###### QTL model ############
85 qtlmodelfile
<-grep("stat_qtl_model",
91 qtlmodel
<-scan(qtlmodelfile
,
95 if (qtlmodel
== "Single-QTL Scan")
97 qtlmodel
<-c("scanone")
99 if (qtlmodel
== "Two-QTL Scan")
101 qtlmodel
<-c("scantwo")
104 ###### permutation############
105 userpermufile
<-grep("stat_permu_test",
111 userpermuvalue
<-scan(userpermufile
,
117 if (userpermuvalue
== "None")
121 userpermuvalue
<-as
.numeric(userpermuvalue
)
124 #userpermuvalue<-c(100)
128 ######genome step size############
129 stepsizefile
<-grep("stat_step_size",
135 stepsize
<-scan(stepsizefile
,
141 if (qtlmethod
== 'mr')
146 (qtlmethod
!= 'mr' & stepsize
== "zero")
151 stepsize
<-as
.numeric(stepsize
)
155 ######genotype calculation method############
156 genoprobmethodfile
<-grep("stat_prob_method",
163 genoprobmethod
<-scan(genoprobmethodfile
,
170 ########No. of draws for sim.geno method###########
173 if (qtlmethod
== 'imp')
175 if (is
.null(grep("stat_no_draws", statfiles
))==FALSE)
177 drawsnofile
<-grep("stat_no_draws",
185 if (is
.null(drawsnofile
)==FALSE)
187 drawsno
<-scan(drawsnofile
,
192 drawsno
<-as
.numeric(drawsno
)
196 ########significance level for genotype
197 #######probablity calculation
198 genoproblevelfile
<-grep("stat_prob_level",
205 genoproblevel
<-scan(genoproblevelfile
,
212 if (qtlmethod
== 'mr')
214 if (is
.logical(genoproblevel
) == FALSE)
219 if (is
.logical(genoprobmethod
) ==FALSE)
221 genoprobmethod
<-c('Calculate')
225 genoproblevel
<-as
.numeric(genoproblevel
)
227 ########significance level for permutation test
228 permuproblevelfile
<-grep("stat_permu_level",
235 permuproblevel
<-scan(permuproblevelfile
,
241 permuproblevel
<-as
.numeric(permuproblevel
)
244 infile
<-scan(file
=infile
,
248 cvtermfile
<-grep("cvterm",
255 popidfile
<-grep("popid",
262 genodata
<-grep("genodata",
269 phenodata
<-grep("phenodata",
276 permufile
<-grep("permu",
283 crossfile
<-grep("cross",
290 popid
<-scan(popidfile
,
295 cross
<-scan(crossfile
,
305 popdata
<- read
.cross("csvs",
308 na
.strings
=c("NA", "-"),
309 genotypes
=c("1", "2", "3", "4", "5"),
312 popdata
<-jittermap(popdata
)
314 if (cross
== "bc" | cross
== "rilsib" | cross
== "rilself")
316 popdata
<- read
.cross("csvs",
319 na
.strings
=c("NA", "-"),
320 genotypes
=c("1", "2"),
323 popdata
<-jittermap(popdata
)
326 if (cross
== "rilself")
328 popdata
<-convert2riself(popdata
)
330 if (cross
== "rilsib")
332 popdata
<-convert2risib(popdata
)
335 #calculates the qtl genotype probablity at
336 #the specififed step size and probability level
338 if (genoprobmethod
== "Calculate")
340 popdata
<-calc
.genoprob(popdata
,
342 error
.prob
=genoproblevel
344 genotypetype
<-c('prob')
346 if (genoprobmethod
== "Simulate")
348 popdata
<-sim
.geno(popdata
,
351 error
.prob
=genoproblevel
,
354 genotypetype
<-c('draws')
359 cvterm
<-scan(file
=cvtermfile
,
363 cv
<-find
.pheno(popdata
,
365 )#returns the col no. of the cvterm
367 permuvalues
<-scan(file
=permufile
,
371 permuvalue1
<-permuvalues
[1]
372 permuvalue2
<-permuvalues
[2]
375 if (is
.logical(permuvalue1
) == FALSE)
377 if (qtlmodel
== "scanone")
379 if (userpermuvalue
== 0 )
381 popdataperm
<-scanone(popdata
,
388 if (userpermuvalue
!= 0)
390 popdataperm
<-scanone(popdata
,
393 n
.perm
= userpermuvalue
,
397 permu
<-summary(popdataperm
,
402 if (qtlmethod
!= "mr")
404 if (qtlmodel
== "scantwo")
406 if (userpermuvalue
== 0 )
408 popdataperm
<-scantwo(popdata
,
414 if (userpermuvalue
!= 0)
416 popdataperm
<-scantwo(popdata
,
419 n
.perm
= userpermuvalue
,
422 permu
<-summary(popdataperm
,
431 ##########set the LOD cut-off for singificant qtls ##############
434 if(is
.null(permu
) == FALSE)
436 LodThreshold
<-permu
[1,1]
438 ##########QTL EFFECTS ##############
448 chrlist
<-append(chrlist
,
453 chrdata
<-paste(cvterm
,
460 for (ch
in chrlist
) {
462 chrdata
<-paste(cvterm
,
476 chrdata
<-append(chrdata
,
487 lodconfidenceints
<-c()
494 filedata
<-paste(cvterm
,
499 filedata
<-paste(filedata
,
515 LodScore
<-position
[["lod"]]
516 QtlChr
<-levels(position
[["chr"]])
518 if ( is
.null(LodThreshold
)==FALSE )
520 if (LodScore
>=LodThreshold
)
522 QtlChrs
<-append(QtlChrs
,
525 QtlLods
<-append(QtlLods
,
528 QtlPositions
<-append(QtlPositions
,
537 peakmarker
<-find
.marker(popdata
,
542 lodpeakmarker
<-i
[peakmarker
, ]
544 lodconfidenceint
<-bayesint(i
,
550 if (is
.na(lodconfidenceint
[peakmarker
, ]))
552 lodconfidenceint
<-rbind(lodconfidenceint
,
564 peakmarkers
<-peakmarker
565 lodconfidenceints
<-lodconfidenceint
570 datasummary
<-rbind(datasummary
,
573 peakmarkers
<-rbind(peakmarkers
,
576 lodconfidenceints
<-rbind(lodconfidenceints
,
585 ##########QTL EFFECTS ##############
590 if (is
.null(LodThreshold
) == FALSE)
592 if ( max(QtlLods
) >= LodThreshold
)
594 QtlObj
<-makeqtl(popdata
,
600 QtlsNo
<-length(QtlPositions
)
627 QtlEffects
<-try(fitqtl(popdata
,
636 if(class(QtlEffects
) != 'try-error')
639 ResultModel
<-attr(QtlEffects
,
643 Effects
<-QtlEffects$ests$ests
644 QtlLodAnova
<-QtlEffects$lod
645 ResultFull
<-QtlEffects$result
.full
646 ResultDrop
<-QtlEffects$result
.drop
648 if (is
.numeric(Effects
))
650 Effects
<-round(Effects
,
655 if (is
.numeric(ResultFull
))
657 ResultFull
<-round(ResultFull
,
662 if (is
.numeric(ResultDrop
))
664 ResultDrop
<-round(ResultDrop
,
672 ##########creating vectors for the outfiles##############
674 outfiles
<-scan(file
=outfile
,
678 qtlfile
<-grep("qtl_summary",
685 peakmarkersfile
<-grep("peak_marker",
692 confidencelodfile
<-grep("confidence",
698 QtlEffectsFile
<-grep("qtl_effects",
704 VariationFile
<-grep("explained_variation",
711 ##### writing outputs to their respective files
713 write
.table(datasummary
,
721 write
.table(peakmarkers
,
722 file
=peakmarkersfile
,
729 write
.table(lodconfidenceints
,
730 file
=confidencelodfile
,
737 if (is
.null(ResultDrop
)==FALSE)
739 write
.table(ResultDrop
,
748 if(is
.null(ResultFull
)==FALSE)
750 write
.table(ResultFull
,