forked from jxw773/CytoConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyto_graph.R
291 lines (224 loc) · 9.55 KB
/
cyto_graph.R
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
##setting up blank plot
cyto_graph<-function(cyto_list,ref_list="GRCh38"){
##if it is string, set it equal to one of the stuff
if(length(ref_list)<=1)
{
if(ref_list =="GRCh38")
{
ref_list <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_GRCh38.txt", header = FALSE)
), as.character)
}else if(ref_list =="hg19"){
ref_list <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_hg19.txt", header = FALSE)
), as.character)
}else if(ref_list =="hg18"){
ref_list <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_hg18.txt", header = FALSE)
), as.character)
}else if(ref_list=="hg17"){
ref_list <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_hg17.txt", header = FALSE)
), as.character)
}else if(is.null(ref_list))
{
##default is grch38
ref_list <-
sapply(as.data.frame(
read.delim("Builds/cytoBand_GRCh38.txt", header = FALSE)
), as.character)
}else{
return("ref_list incorrectly specified")
}
ref_list <-as.data.frame(ref_list[sapply(unique(ref_list[,1]),function(x){grep(x,ref_list[,1])[length(grep(paste(x,"$",sep=""),ref_list[,1]))]}),][,c(1,3)])
ref_list<-apply(ref_list,2,as.character)
}else{
ref_list<-apply(ref_list,2,as.character)
}
##cyto_list<-cyto_list[order(cyto_list[,1],order(cyto_list[,2],cyto_list[,3])),]
if(nrow(cyto_list) >= 1)
{
double_loss<-cyto_list[which(cyto_list[,5]=="Loss"),]
double_loss[,1]<-as.character(double_loss[,1])
if(nrow(double_loss)>1)
{
loss_overlap<-data.frame()
chr_list<-unique(double_loss[,2])
name_list<-as.character(unique(double_loss[,1]))
for(i in 1:length(chr_list))
{
for(k in 1:length(name_list))
{
chr_table<-double_loss[intersect(which(double_loss[,2]==chr_list[i]) , which(double_loss[,1]==name_list[k] )),]
##don't do this, check for complete overlap first, if so , skip any that are in completely
##chr_table_ctr<-cbind("test",unique.data.frame(chr_table[,2:4]),"Loss")
if(nrow(chr_table) >=2)
{
chr_table <- chr_table[order(chr_table[,2],chr_table[,3]),]
for(d in 1:(nrow(chr_table)-1))
{
overlap=F
if(!is.na(chr_table[d,1])){
for(j in (d+1):(nrow(chr_table))){
if(!is.na(chr_table[j,1])){
first <- as.numeric(chr_table[d,3:4])
sec <- as.numeric(chr_table[j,3:4])
if(first %overlaps% sec)
{
overlap=T
##if(first[2] > sec[1]){
## chr_table[d,3] <- sec[1]
## }
##if(first[1] > sec[2]){
## chr_table[d,2] <- sec[2]
## }
if(first[2] > sec[2]){
chr_table[d,4] <- sec[2]
}
if(first[1] < sec[1]){
chr_table[d,3] <- sec[1]
}
chr_table[j,] <- rep(NA,6)
}
}
}
}
if(overlap)
{
loss_overlap<-rbind(loss_overlap,chr_table[d,])
}
}
}
}
}
if(is.vector(loss_overlap)&& !is.na(loss_overlap[1]))
{
loss_overlap[5]<-"Double"
cyto_list<-rbind(cyto_list,loss_overlap)
}else if(nrow(loss_overlap)>0)
{
loss_overlap <- loss_overlap[which(!is.na(loss_overlap[,1])),]
loss_overlap[,5]<-"Double"
cyto_list<-rbind(cyto_list,loss_overlap)
}
}
##if not assume ref list was inputted
}
sorted_reflist<-ref_list[c(order(as.numeric(gsub("chr","",ref_list[1:22,1]))),23:24),]
coords<-as.numeric(sorted_reflist[,2])
##length_coords<-coordss[2:length(coordss)]-coordss[1:(length(coordss)-1)]
cum_length_coords=cumsum(coords/(sum(coords)))
start_cum_length=c(0,cum_length_coords[1:length(cum_length_coords)-1])
##setting up lables
##x coord starting point
xbegin=0
##default parameters
xcoord_master=0
#determine how big graph should be
##if(length(uniq_coord_name)<=2)
##{
## y_above=0.90
## y_below=0.70
##}
##else if(length(uniq_coord_name)>10)
##{
y_above=0.90
y_below=0.15
##}else{
## y_above=0.90
## y_below=0.40
##}
uniq_coord_name<-vector()
rect_maker<-data.frame()
matched_coord_names<-vector()
y_coordlist<-matrix(ncol=2,nrow=0)
if(!is.null(cyto_list) && nrow(cyto_list) > 0)
{
##plotting data values
coord_name<-cyto_list[,1]
uniq_coord_name<-unique(coord_name)
##y coordinates (by name)
if(length(uniq_coord_name)>1)
{
##if(nrow(cyto_list)>1)
## {
## cyto_list<-cyto_list[match(cyto_list[,1],uniq_coord_name),]
##}
##coord_name<-coord_name[match(coord_name,uniq_coord_name)]
##matched_coord_names<-cyto_list
matched_coord_names<-lapply(uniq_coord_name,function(x){y=which(as.character(x)==as.character(coord_name));cbind(as.vector(y),rep(which(x==uniq_coord_name),length(y)))})
}else{
y=which(uniq_coord_name==coord_name)
matched_coord_names<-cbind(as.vector(y),rep(1,length(y)))
}
## if(is.list(matched_coord_names))
##{
## temp_coord_matrix<-data.frame()
## for(i in 1:length(matched_coord_names))
## {
## rbind(temp_coord_matrix,matched_coord_names[[i]])
## }
## }
##
if(is.list(matched_coord_names))
{
for(i in 1:length(matched_coord_names))
{
y_coordlist<-rbind(y_coordlist,matched_coord_names[[i]])
}
}else{
y_coordlist<-matched_coord_names
}
}
xcoord_master=max(nchar(as.character(uniq_coord_name)))*0.005+xbegin
##calculate x coords and y coords according to y_coord_list by resorting cytolist
if(length(y_coordlist)>0 && nrow(cyto_list)>1)
{
cyto_list<-cyto_list[order(cyto_list[,1],y_coordlist[,1]),]
}
if(is.vector(cyto_list)){
coords_listed<-cyto_list[2:5]
temp_coords<-gsub("chr","",coords_listed[1])
temp_coords[grep("Y",temp_coords)]<-24
temp_coords[grep("X",temp_coords)]<-23
coordlist<-as.numeric(temp_coords);
##adjust chrom name for x and y for input data
coords_listed[grep("X",coords_listed[,1])]<-23
coords_listed[grep("Y",coords_listed[,1])]<-24
y_area_coord=cbind((y_coordlist[,2]-1)*-(y_above-y_below)/length(uniq_coord_name)+y_above,y_above-y_coordlist[,2]*(y_above-y_below)/length(uniq_coord_name))
##calculating where rectangle should start from how long the sample is
##x coordinates
xstart<-(start_cum_length[coordlist]+(as.numeric(coords_listed[2]))/(sum(coords)))*(1-xcoord_master)+xcoord_master
xend<-(start_cum_length[coordlist]+(as.numeric(coords_listed[3]))/(sum(coords)))*(1-xcoord_master)+xcoord_master
##all info for coordinates
rect_maker<-as.data.frame(cbind(xstart,xend,y_area_coord,coords_listed[4]))
rect_maker[1:4]<-apply(rect_maker[,1:4],2,function(x){as.numeric(as.character(x))})
rect_maker[5]<-as.character(rect_maker[,5])
}else{
coords_listed<-cyto_list[,2:5]
temp_coords<-gsub("chr","",coords_listed[,1])
temp_coords[grep("Y",temp_coords)]<-24
temp_coords[grep("X",temp_coords)]<-23
coordlist<-as.numeric(temp_coords);
##adjust chrom name for x and y for input data
coords_listed[grep("X",coords_listed[,1]),1]<-23
coords_listed[grep("Y",coords_listed[,1]),1]<-24
y_area_coord=cbind((y_coordlist[,2]-1)*-(y_above-y_below)/length(uniq_coord_name)+y_above,y_above-y_coordlist[,2]*(y_above-y_below)/length(uniq_coord_name))
##calculating where rectangle should start from how long the sample is
##x coordinates
xstart<-(start_cum_length[coordlist]+(as.numeric(coords_listed[,2]))/(sum(coords)))*(1-xcoord_master)+xcoord_master
xend<-(start_cum_length[coordlist]+(as.numeric(coords_listed[,3]))/(sum(coords)))*(1-xcoord_master)+xcoord_master
##all info for coordinates
rect_maker<-as.data.frame(cbind(xstart,xend,y_area_coord,coords_listed[,4]))
rect_maker[,1:4]<-apply(rect_maker[,1:4],2,function(x){as.numeric(as.character(x))})
rect_maker[,5]<-as.character(rect_maker[,5])
}
sorted_reflist<-as.data.frame(sorted_reflist)
sorted_reflist[,1]<-as.character(sorted_reflist[,1])
sorted_reflist[,2]<-as.numeric(as.character(sorted_reflist[,2]))
return(list(rect_maker,xbegin,xcoord_master,y_above,y_below,sorted_reflist,cum_length_coords,start_cum_length,uniq_coord_name))
}