1+ plotHaplotypes <- function (filename )
2+ {
3+ # filename <- "outfile.rs7215219.ihs.out";
4+
5+ # filename.der <- paste(filename,".der.colormap",sep="")
6+ # filename.anc <- paste(filename,".anc.colormap",sep="")
7+ # data<-as.matrix(read.table(filename.der))
8+ rawData <- read.table(filename )
9+
10+
11+ print(" Plotting EHH decay..." )
12+ setEPS()
13+ postscript(file = paste(filename ," .ehh.eps" ,sep = " " ))
14+ plot(rawData $ V1 / 1000000 ,rawData $ V2 ,pch = " " ,xlab = " Distance from locus (Mb)" ,ylab = " EHH" ,ylim = c(0 ,1 ))
15+ lines(rawData $ V1 / 1000000 ,rawData $ V3 ,col = " red" )
16+ lines(rawData $ V1 / 1000000 ,rawData $ V4 ,col = " blue" )
17+ legend(" topright" ,legend = c(" Derived" ," Ancestral" ),col = c(" red" ," blue" ),lty = 1 )
18+ dev.off()
19+
20+ numHaps <- dim(data )[1 ]
21+ numSites <- dim(data )[2 ]
22+ numCols <- range(data )[2 ]- range(data )[1 ]
23+
24+ pos <- rawData $ V1
25+ hap <- seq(1 ,numHaps )
26+
27+ numSortCols <- min(numCols ,5 )
28+
29+ ordering <- as.data.frame(matrix (rep(0 ,numSortCols * numHaps ),nrow = numHaps ,ncol = numSortCols ))
30+
31+ print(" Sorting derived colors..." )
32+
33+ for (h in 1 : numSortCols )
34+ {
35+ for (i in 1 : numHaps )
36+ {
37+ for (j in 1 : numSites )
38+ {
39+ if ( data [i ,j ] == h - 1 )
40+ {
41+ ordering [i ,h ] <- ordering [i ,h ]+ 1
42+ }
43+ }
44+ }
45+ }
46+
47+ order.string <- " sorted.order<-order("
48+ for (h in 1 : numSortCols )
49+ {
50+ order.string <- paste(order.string ," ordering$V" ,h ," ," ,sep = " " )
51+ }
52+ substr(order.string ,nchar(order.string ),nchar(order.string )) <- " )"
53+ eval(parse(text = order.string ))
54+
55+ blank <- rep(" " ,length(pos ))
56+
57+
58+ data2 <- as.matrix(read.table(filename.anc ))
59+ numHaps2 <- dim(data2 )[1 ]
60+ numSites2 <- dim(data2 )[2 ]
61+ numCols2 <- range(data2 )[2 ]- range(data2 )[1 ]
62+
63+ pos2 <- rawData $ V1
64+ hap2 <- seq(1 ,numHaps2 )
65+
66+ numSortCols2 <- min(numCols2 ,5 )
67+
68+ ordering2 <- as.data.frame(matrix (rep(0 ,numSortCols2 * numHaps2 ),nrow = numHaps2 ,ncol = numSortCols2 ))
69+
70+ print(" Sorting ancestral colors..." )
71+
72+ for (h in 1 : numSortCols2 )
73+ {
74+ for (i in 1 : numHaps2 )
75+ {
76+ for (j in 1 : numSites2 )
77+ {
78+ if ( data2 [i ,j ] == h - 1 )
79+ {
80+ ordering2 [i ,h ] <- ordering2 [i ,h ]+ 1
81+ }
82+ }
83+ }
84+ }
85+
86+ order.string2 <- " sorted.order2<-order("
87+ for (h in 1 : numSortCols2 )
88+ {
89+ order.string2 <- paste(order.string2 ," ordering2$V" ,h ," ," ,sep = " " )
90+ }
91+ substr(order.string2 ,nchar(order.string2 ),nchar(order.string2 )) <- " )"
92+ eval(parse(text = order.string2 ))
93+
94+ blank2 <- rep(" " ,length(pos2 ))
95+ space <- rep(- 1 ,numSites )
96+
97+ padding <- as.integer((numHaps + numHaps2 ) * 0.05 )
98+ if (padding < 2 )
99+ {
100+ padding <- 2
101+ } else if (padding %% 2 == 1 )
102+ {
103+ padding <- padding - 1
104+ }
105+
106+ combined.data <- rbind(data [sorted.order ,],space );
107+
108+ for (i in 1 : padding )
109+ {
110+ combined.data <- rbind(combined.data ,space );
111+ }
112+
113+ combined.data <- rbind(combined.data ,data2 [sorted.order2 ,])
114+ combined.numHaps <- dim(combined.data )[1 ]
115+ combined.hap <- seq(1 ,combined.numHaps )
116+ combined.numCols <- max(numCols ,numCols2 )
117+
118+ col.der <- c(rgb(227 / 255 , 26 / 255 , 28 / 255 ),rgb(51 / 255 , 160 / 255 , 44 / 255 ),rgb(31 / 255 , 120 / 255 , 180 / 255 ),rgb(255 / 255 , 127 / 255 , 0 / 255 ),rgb(106 / 255 , 61 / 255 , 154 / 255 ),rgb(251 / 255 , 154 / 255 , 153 / 255 ),rgb(178 / 255 , 223 / 255 , 138 / 255 ),rgb(166 / 255 , 206 / 255 , 227 / 255 ),rgb(253 / 255 , 191 / 255 , 111 / 255 ),rgb(202 / 255 , 178 / 255 , 214 / 255 ))
119+ colors.der <- rep(" " ,combined.numCols )
120+
121+ col.anc <- c(rgb(31 / 255 , 120 / 255 , 180 / 255 ),rgb(255 / 255 , 127 / 255 , 0 / 255 ),rgb(106 / 255 , 61 / 255 , 154 / 255 ),rgb(227 / 255 , 26 / 255 , 28 / 255 ),rgb(51 / 255 , 160 / 255 , 44 / 255 ),rgb(166 / 255 , 206 / 255 , 227 / 255 ),rgb(253 / 255 , 191 / 255 , 111 / 255 ),rgb(202 / 255 , 178 / 255 , 214 / 255 ),rgb(251 / 255 , 154 / 255 , 153 / 255 ),rgb(178 / 255 , 223 / 255 , 138 / 255 ))
122+ colors.anc <- rep(" " ,combined.numCols )
123+
124+ index <- 1 ;
125+ for (i in 0 : (combined.numCols - 1 ))
126+ {
127+ colors.der [i + 1 ] <- col.der [(i %% 10 ) + 1 ]
128+ colors.anc [i + 1 ] <- col.anc [(i %% 10 ) + 1 ]
129+ }
130+
131+ pos <- pos / 1000000
132+
133+ print(" Plotting haplotype colors..." )
134+ setEPS()
135+ postscript(file = paste(filename ," .hapcolor.eps" ,sep = " " ))
136+
137+ image(pos ,combined.hap ,t(combined.data ),zlim = c(- 0.1 ,- 0.01 ),yaxt = " n" ,xaxt = " n" ,ylab = " " ,xlab = " Distance from locus (Mb)" ,ylim = c(1 - (padding / 2 ),combined.numHaps + (padding / 2 )))
138+ axis(3 ,at = pos ,lab = blank ,tck = (1 / (combined.numHaps ) * padding / 4 ))
139+ axis(3 )
140+ axis(1 ,at = pos ,lab = blank ,tck = (1 / (combined.numHaps ) * padding / 4 ))
141+ axis(1 )
142+ abline(h = numHaps + (padding / 2 )+ 1 )
143+
144+ image(pos ,combined.hap [1 : numHaps ],t(combined.data [1 : numHaps ,]),zlim = c(0 ,max(combined.data )),col = colors.der ,add = TRUE )# ,yaxt="n",xaxt="n",ylab="",ylim=c(1-(padding/10),combined.numHaps+(padding/10)),xlab="")
145+ mtext(" Derived" ,2 ,at = (numHaps / 2 + padding / 2 ))
146+ image(pos ,combined.hap [(numHaps + 1 ): (combined.numHaps )],t(combined.data [(numHaps + 1 ): (combined.numHaps ),]),zlim = c(0 ,max(combined.data )),col = colors.anc ,add = TRUE )# ,yaxt="n",xaxt="n",ylab="",ylim=c(-1,combined.numHaps+2),xlab="")
147+ mtext(" Ancestral" ,2 ,at = (numHaps + (padding + 1 ) + numHaps2 / 2 ) )
148+
149+ dev.off()
150+
151+ return
152+ }
153+
154+ plotHaplotypes(" /Users/amatur/code/selscan/src/out/outfile.ihs.out" )
0 commit comments