-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate_gel.R
143 lines (118 loc) · 6.44 KB
/
annotate_gel.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
#!/usr/bin/Rscript
require(imager)
require(pracma)
require(optparse)
## Collect arguments
option_list = list(
make_option(c("-f", "--file"), type="character", default=NULL,help="dataset file name", metavar="character"),
make_option(c("-o", "--out"), type="character", default=NULL,help="output file name [default= %default]", metavar="character"),
make_option(c("-n", "--n.lanes"), type="numeric", default=NULL,help="number of lanes in the gel", metavar="numeric"),
make_option(c("-l","--lanes.labels"), type="character", default=NULL,help="labels for the lanes, separated by commas, quoted", metavar="character"),
make_option(c("-N", "--n.bottom.lanes"), type="numeric", default=NULL,help="number of lanes in the gel", metavar="numeric"),
make_option(c("-L","--bottom.lane.labels"), type="character", default=NULL,help="labels for the lanes, separated by commas, quoted", metavar="character"),
make_option(c("--height.mm"), type="numeric", default=80, help="height of output in mm", metavar="numeric"),
make_option(c("--text.pt"), type="numeric", default=8, help="text size in pt", metavar="numeric"),
make_option(c("--text.color"), type="character", default="red", help="text color", metavar="character"),
make_option(c("--labels.position"), type="character", default="top",help="top or bottom", metavar="numeric"),
make_option(c("--labels.angle"), type="numeric", default=90, help="degrees", metavar="numeric"),
make_option(c("--labels.adj.x"), type="numeric", default=NULL, help="labels position adjustment in writting direction", metavar="numeric"),
make_option(c("--labels.adj.y"), type="numeric", default=NULL, help="labels position adjustment in direction perpendicular to writting", metavar="numeric"),
make_option(c("--labels.col"), type="character", default=NULL, help="color for labels (default text.color)", metavar="numeric"),
make_option(c("--labels.pt"), type="numeric", default=NULL,help="labes size (default text.pt)", metavar="numeric"),
make_option(c("--name.position"), type="character", default="bottomright",help="bottomleft/bottomright/topright/topleft", metavar="numeric"),
make_option(c("--name.offset"), type="numeric", default=10,help="offset for name position", metavar="numeric"),
make_option(c("--name.col"), type="character", default=NULL,help="color for name (default text.color)", metavar="numeric"),
make_option(c("--name.pt"), type="numeric", default=NULL,help="name for labels (default text.color)", metavar="numeric")
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
if(is.null(opt$file)){stop('missing argument: file')}
if(is.null(opt$n.lanes)){stop('missing argument: n.lanes')}
if(is.null(opt$lanes.labels)){stop('missing argument: lanes.labels')}
file <- opt$file
out <- if(is.null(opt$out)){sub(".Tif", "_annotated.png", file)}else{opt$out}
n.lanes <- opt$n.lanes
lanes.labels <- unlist(strsplit(opt$lanes.labels, split = ",", fixed=T))
height.mm <- opt$height.mm
text.pt <- opt$text.pt
text.color <- opt$text.color
labels.position <- opt$labels.position
labels.angle <- opt$labels.angle
labels.col <- if(is.null(opt$labels.col)){text.color}else{opt$labels.col}
labels.pt <- if(is.null(opt$labels.pt)){text.pt}else{opt$labels.pt}
name.position <- opt$name.position
name.offset <- opt$name.offset
name.col <- if(is.null(opt$name.col)){text.color}else{opt$name.col}
name.pt <- if(is.null(opt$name.pt)){text.pt}else{opt$name.pt}
# Check inputs
if(labels.angle!=0 & labels.angle!=90 & is.null(opt$labels.adj.x)){
stop('You must specify labels.adj.x if labels angle is not 0 or 90')
}
if(labels.angle!=0 & labels.angle!=90 & is.null(opt$labels.adj.y)){
stop('You must specify labels.adj.y if labels angle is not 0 or 90')
}
if(length(lanes.labels)!=n.lanes){stop('length(length.labels) must be n.lanes')}
if(!is.null(opt$n.bottom.lanes)){
n.bottom.lanes <- opt$n.bottom.lanes
bottom.lane.labels <- unlist(strsplit(opt$bottom.lane.labels, split = ",", fixed=T))
if(length(bottom.lane.labels)!=n.bottom.lanes){stop('length(bottom.lane.labels) must be n.bottom.lanes', length(bottom.lane.labels)," ",n.bottom.lanes)}
}
####################################
## Load file
img.input <- load.image(file)
## Cut manually
coord <- grabRect(img.input, output = "coord")
img <- img.input
img <- imsub(img, x < coord["x1"])
img <- imsub(img, y < coord["y1"])
img <- imsub(img, x>coord["x0"])
img <- imsub(img, y>coord["y0"])
## Detect lanes
coord_lanes <- grabLine(img, output = "coord")
y_lanes <- min(coord_lanes[c("y0","y1")])
x_lanes <- seq(coord_lanes["x0"], coord_lanes["x1"], length.out = n.lanes)
if(!is.null(opt$n.bottom.lanes)){
coord_bottom_lanes <- grabLine(img, output = "coord")
y_bottom_lanes <- min(coord_bottom_lanes[c("y0","y1")])
x_bottom_lanes <- seq(coord_bottom_lanes["x0"], coord_bottom_lanes["x1"], length.out = n.bottom.lanes)
}
#Invert colors
img <- max(img)-img
##### Plot
if(labels.angle==0){
adj.x <- 0.5
adj.y <- 1
}
if(labels.angle==90 & labels.position=="top"){
adj.x <- 0
adj.y <- 0.5
}
if(labels.angle==90 & labels.position=="bottom"){
adj.x <- 1
adj.y <- 0.5
}
if(!is.null(opt$labels.adj.x)){
adj.x <- opt$labels.adj.x
}
if(!is.null(opt$labels.adj.y)){
adj.y <- opt$labels.adj.y
}
png(out, width = nrow(img)/ncol(img)*height.mm, height = height.mm, units = "mm", res = 500)
par(mar=c(0,0,0,0), ps=labels.pt)
# Plot image
plot(img, axes = F)
# Print labels
text(x=x_lanes,y=rep(y_lanes, n.lanes), labels = lanes.labels,
col=labels.col, srt=labels.angle,adj = c(adj.x,adj.y))
if(!is.null(opt$n.bottom.lanes)){
text(x=x_bottom_lanes,y=rep(y_bottom_lanes, n.bottom.lanes),
labels = bottom.lane.labels, col=labels.col, srt=labels.angle,adj = c(adj.x,adj.y))
}
# Print name of the file
if(name.position=="bottomright"){x.name=dim(img)[1]-name.offset; y.name=dim(img)[2]-name.offset; adj.name = c(0,0) }
if(name.position=="topright"){ x.name=dim(img)[1]-name.offset; y.name=0+name.offset; adj.name = c(1,0) }
if(name.position=="topleft"){ x.name=name.offset; y.name=0+name.offset; adj.name = c(1,1) }
if(name.position=="bottomleft"){ x.name=name.offset; y.name=dim(img)[2]-name.offset; adj.name = c(0,1) }
par(ps=name.pt)
text(x=x.name, y=y.name, labels = file, col=name.col, srt=90, adj=adj.name)
dev.off()