labels.R 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # * Author: Bangyou Zheng ([email protected])
  2. # * Author: Bangyou Zheng ([email protected])
  3. # * Created: 04:21 PM Monday, 28 April 2014
  4. # * Copyright: AS IS
  5. # *
  6. #' Create labels with barcode
  7. #'
  8. #' @param designs A data.frame of design file, which has to include several columns
  9. #' @param file A output pdf file
  10. #' @param measure_date Date of measurement
  11. #' @param site_idx Index of site
  12. #' @param sample_num Number of sample in each plot
  13. #' @param product Product ID of lables
  14. #' @export
  15. createBarcode <- function(
  16. designs,
  17. measure_date,
  18. site_idx,
  19. measure_traits,
  20. sample_num = 1,
  21. measure_traits_map = NULL) {
  22. # Convert into small case to avoid problems
  23. names(designs) <- tolower(names(designs))
  24. library(readr)
  25. library(dplyr)
  26. library(magrittr)
  27. names(measure_traits_map) <- tolower(names(measure_traits_map))
  28. measure_trait_idx <- measure_traits_map$measurementindex[
  29. match(measure_traits, measure_traits_map$name)]
  30. bc_plot <- designs %>%
  31. mutate(Barcode = paste0(
  32. format(measure_date, '%y%m%d'),
  33. site_idx,
  34. formatC(row, width = 2, flag = '0'),
  35. formatC(column, width = 2, flag = '0')
  36. ))
  37. bc <- expand.grid(
  38. Barcode = bc_plot$Barcode,
  39. measure_trait = measure_trait_idx,
  40. sample = seq(length = sample_num),
  41. stringsAsFactors = FALSE
  42. ) %>%
  43. left_join(bc_plot, by = 'Barcode') %>%
  44. left_join(measure_traits_map,
  45. by = c('measure_trait' = 'measurementindex')) %>%
  46. mutate(
  47. Barcode = paste0(
  48. Barcode,
  49. formatC(measure_trait, width = 2, flag = '0'),
  50. sample)) %>%
  51. rename(MeasureTrait = name) %>%
  52. mutate(MeasureDate = measure_date,
  53. Researcher = researcher) %>%
  54. arrange(column, row)
  55. bc
  56. }
  57. #' Generate labels for avery products
  58. #'
  59. #' @param labels A character vector will be used to genearte labels
  60. #' @param file the file name to export
  61. #' @param product The product ID of Avery
  62. #' @param researcher Name of researcher
  63. #' @param researcher Name of researcher
  64. #' @export
  65. averyLabel <- function(
  66. labels, file,
  67. product = 'L7163')
  68. {
  69. names(labels) <- tolower(names(labels))
  70. # Total number of labels
  71. num_label <- nrow(labels)
  72. # Check the labels
  73. if (num_label == 0)
  74. {
  75. stop('NO label specified')
  76. }
  77. # Read teh AveryDB
  78. averydb <- read.csv('data/dbavery.csv')
  79. # Check the product ID
  80. if (!(product %in% averydb$ID))
  81. {
  82. stop(paste0('Product ID ', product, ' don\'t support.'))
  83. }
  84. # Get the design
  85. a_design <- averydb[averydb$ID == product,]
  86. # Define the page size (A4)
  87. page_height <- 297
  88. page_width <- 210
  89. # Calculate label width and height
  90. a_design$LabelHeight <- (page_height - a_design$TopBorder -
  91. a_design$BottomBorder -
  92. a_design$MarginRow * (a_design$LabelRows - 1)) /
  93. a_design$LabelRows
  94. a_design$LabelWidth <- (page_width - a_design$LeftBorder -
  95. a_design$RightBorder -
  96. a_design$MarginCol * (a_design$LabelCols - 1)) /
  97. a_design$LabelCols
  98. # load the library
  99. library(grid)
  100. # page_border <- 0.25 * 25.4
  101. # Printed labels
  102. num_printed <- 1
  103. # Create all labels
  104. # Calculate total pages
  105. pages <- ceiling(num_label / (a_design$LabelRows * a_design$LabelCols))
  106. pdf(file, width = page_width / 25.4, height = page_height / 25.4)
  107. op <- par(mar = rep(0, 4))
  108. for (k in seq(length = pages))
  109. {
  110. grid.newpage()
  111. grid.rect()
  112. for (i in seq(length = a_design$LabelRows))
  113. {
  114. for (j in seq(length = a_design$LabelCols))
  115. {
  116. # Check if all labels is printed
  117. if (num_printed > num_label)
  118. {
  119. break
  120. return(NULL)
  121. }
  122. # Calculate the viewport
  123. vp_x <- unit(
  124. # Left border
  125. a_design$LeftBorder +
  126. # Page border
  127. # page_border +
  128. # Size for labels in the left
  129. a_design$LabelWidth * (j - 1) +
  130. # Middle of this label
  131. a_design$LabelWidth / 2 +
  132. # Margins between labels in the left
  133. a_design$MarginCol * (j - 1),
  134. 'mm')
  135. vp_y <- unit(
  136. # Print from the top
  137. page_height - (
  138. # Top border
  139. a_design$TopBorder +
  140. # Page border
  141. # Size for labels in the top
  142. a_design$LabelHeight * (i - 1) +
  143. # Middle of this label
  144. a_design$LabelHeight / 2 +
  145. # Margins between labels in the top
  146. a_design$MarginRow * (i - 1)),
  147. 'mm')
  148. # Create viewport
  149. vp <- viewport(vp_x, vp_y,
  150. unit(a_design$LabelWidth, 'mm'),
  151. unit(a_design$LabelHeight, 'mm'))
  152. pushViewport(vp)
  153. grid.rect()
  154. # keep a 3 mm border
  155. # canvas
  156. canvas_width <- a_design$LabelWidth - 6
  157. canvas_height <- a_design$LabelHeight - 6
  158. vp <- viewport(
  159. 0.5, 0.5,
  160. unit(canvas_width, 'mm'),
  161. unit(canvas_height, 'mm'))
  162. pushViewport(vp)
  163. #reate label
  164. do.call(eval(parse(text = paste0('label_', product))),
  165. args = list(labels = labels, num_printed = num_printed,
  166. canvas_width = canvas_width, canvas_height = canvas_height))
  167. popViewport()
  168. popViewport()
  169. num_printed <- num_printed + 1
  170. }
  171. }
  172. }
  173. par(op)
  174. dev.off()
  175. }
  176. label_L7651 <- function(labels, num_printed, canvas_width, canvas_height) {
  177. bc_img <- zint_barcode(
  178. labels$barcode[num_printed],
  179. height = 100,
  180. border = 0,
  181. is_show_text = TRUE)
  182. vp_img <- viewport(
  183. x = unit(0.5, "npc"),
  184. y = unit(0.6, "npc"),
  185. width = unit(1, "npc"),
  186. height = unit(0.8, "npc")
  187. )
  188. pushViewport(vp_img)
  189. #grid.rect()
  190. grid.raster(bc_img[[1]],
  191. width = unit(1, 'npc'),
  192. height = unit(1, 'npc'))
  193. popViewport()
  194. vp_txt <- viewport(
  195. x = unit(0.5, "npc"),
  196. y = unit(0.1, "npc"),
  197. width = unit(1, "npc"),
  198. height = unit(0.2, "npc")
  199. )
  200. pushViewport(vp_txt)
  201. grid.text(label = labels$label[num_printed],
  202. gp = gpar(cex = 0.3))
  203. popViewport()
  204. }
  205. label_L7163 <- function(labels, num_printed, canvas_width, canvas_height) {
  206. #grid.rect()
  207. # Barcode
  208. bc_img <- zint_barcode(
  209. labels$barcode[num_printed],
  210. height = 30,
  211. border = 0)
  212. vp_img <- viewport(
  213. unit(0.25 * canvas_width, 'mm'),
  214. unit(3 * 1 / 4 * canvas_height, 'mm'),
  215. unit(1 / 2 * canvas_width, 'mm'),
  216. unit(1 / 2 * canvas_height, 'mm'))
  217. pushViewport(vp_img)
  218. #grid.rect()
  219. grid.raster(bc_img[[1]])
  220. popViewport()
  221. # column
  222. vp_row <- viewport(
  223. unit(0.5 / 8 * canvas_width, 'mm'),
  224. unit((1 / 4) * canvas_height, 'mm'),
  225. unit(0.5 / 4 * canvas_width, 'mm'),
  226. unit(0.5 / 4 * canvas_width, 'mm'))
  227. pushViewport(vp_row)
  228. grid.rect()
  229. grid.text(paste0('Col\n', labels$column[num_printed]))
  230. popViewport()
  231. # row
  232. vp_col <- viewport(
  233. unit(3 * 0.5 / 8 * canvas_width, 'mm'),
  234. unit((1 / 4) * canvas_height, 'mm'),
  235. unit(0.5 / 4 * canvas_width, 'mm'),
  236. unit(0.5 / 4 * canvas_width, 'mm'))
  237. pushViewport(vp_col)
  238. grid.rect()
  239. grid.text(paste0('Row\n', labels$row[num_printed]))
  240. popViewport()
  241. # replicate
  242. vp_rep <- viewport(
  243. unit(5 * 0.5 / 8 * canvas_width, 'mm'),
  244. unit((1 / 4) * canvas_height, 'mm'),
  245. unit(0.5 / 4 * canvas_width, 'mm'),
  246. unit(0.5 / 4 * canvas_width, 'mm'))
  247. pushViewport(vp_rep)
  248. grid.rect()
  249. grid.text(paste0('Rep\n', labels$replicate[num_printed]))
  250. popViewport()
  251. # Sample
  252. vp_sam <- viewport(
  253. unit(7 * 0.5 / 8 * canvas_width, 'mm'),
  254. unit((1 / 4) * canvas_height, 'mm'),
  255. unit(0.5 / 4 * canvas_width, 'mm'),
  256. unit(0.5 / 4 * canvas_width, 'mm'))
  257. pushViewport(vp_sam)
  258. grid.rect()
  259. grid.text(paste0('Sam\n', labels$sample[num_printed]))
  260. popViewport()
  261. # Traits
  262. vp_traits <- viewport(
  263. unit(0.75 * canvas_width, 'mm'),
  264. 10 / 12,
  265. 0.5,
  266. 2 / 6)
  267. pushViewport(vp_traits)
  268. #grid.rect()
  269. grid.text(labels$measuretrait[num_printed],
  270. gp = gpar(cex = 1.1))
  271. popViewport()
  272. # TrialCode
  273. vp_trialcode <- viewport(
  274. unit(0.75 * canvas_width, 'mm'),
  275. 7 / 12,
  276. 0.5,
  277. 1 / 6)
  278. pushViewport(vp_trialcode)
  279. #grid.rect()
  280. grid.text(labels$trial[num_printed])
  281. popViewport()
  282. # Genotype
  283. vp_genotype <- viewport(
  284. unit(0.75 * canvas_width, 'mm'),
  285. 5 / 12,
  286. 0.5,
  287. 1 / 6)
  288. pushViewport(vp_genotype)
  289. #grid.rect()
  290. grid.text(labels$genotype[num_printed])
  291. popViewport()
  292. # Site
  293. vp_site <- viewport(
  294. unit(0.75 * canvas_width, 'mm'),
  295. 3 / 12,
  296. 0.5,
  297. 1 / 6)
  298. pushViewport(vp_site)
  299. #grid.rect()
  300. grid.text(labels$site[num_printed])
  301. popViewport()
  302. # Researcher and date
  303. vp_res_date <- viewport(
  304. unit(0.75 * canvas_width, 'mm'),
  305. 1 / 12,
  306. 0.5,
  307. 1 / 6)
  308. pushViewport(vp_res_date)
  309. #grid.rect()
  310. grid.text(paste0(
  311. 'by ',
  312. labels$researcher[num_printed],
  313. ' on ',
  314. labels$measuredate[num_printed]),
  315. x = 0.9,
  316. gp = gpar(cex = 0.5),
  317. hjust = 1)
  318. popViewport()
  319. }