我正在尝试编写一些代码,该代码将使用包含某些样品名称的.csv文件作为输入,并输出包含样品名称和96孔板或384孔板格式(A1,B1,C1的data.frame)。 ..)。对于那些不知道的人,一个96孔板有8个按字母顺序标记的行(A,B,C,D,E,F,G,H)和12个以数字标记的列(1:12),而一个384孔板则具有16行字母标记的行(A:P)和24个数字标记的列(1:24)。我正在尝试编写一些代码,这些代码将生成这两种格式中的任何一种(可以有两种不同的功能来执行此操作),以允许将样品标记为DOWN(A1,B1,C1,D1,E1,F1,G1,H1 ,A2 ...)或ACROSS(A1,A2,A3,A4,A5 ...)。

到目前为止,我已经弄清楚了如何轻松获得行名

rowLetter <- rep(LETTERS[1:8], length.out = variable)
#variable will be based on how many samples I have

我只是无法弄清楚如何使数字列名称正确应用...我已经尝试过:
colNumber <- rep(1:12, times = variable)

但这不是那么简单。如果您要“向下”,则必须在col编号增加1之前填充所有8行,如果要“ACROSS”,则必须在行字母增加1之前填充所有12列。

编辑:

这是一个笨拙的版本。它需要您拥有的样本数量,尚无法使用的“板格式”和方向,并将返回带有孔和板号的data.frame。接下来,我将要进行以下操作:a)固定板的格式,使其能够正常工作,并且b)使此功能能够获取样品名称或ID或其他名称的列表,并返回样品名称,孔位和板号!
plateLayout <- function(numOfSamples, plateFormat = 96, direction = "DOWN"){
  #This assumes that each well will be filled in order. I may need to change this, but     lets get it working first.

  #Calculate the number of plates required
  platesRequired <- ceiling(numOfSamples/plateFormat)
  rowLetter <- character(0)
  colNumber <- numeric(0)
  plateNumber <- numeric(0)

  #The following will work if the samples are going DOWN
  if(direction == "DOWN"){
    for(k in 1:platesRequired){
     rowLetter <- c(rowLetter, rep(LETTERS[1:8], length.out = 96))
      for(i in 1:12){
       colNumber <- c(colNumber, rep(i, times = 8))
      }
     plateNumber <- c(plateNumber, rep(k, times = 96))
    }
  plateLayout <- paste0(rowLetter, colNumber)
  plateLayout <- data.frame(plateLayout, plateNumber)
  plateLayout <- plateLayout[1:numOfSamples,]
  return(plateLayout)
  }

  #The following will work if the samples are going ACROSS
  if(direction == "ACROSS"){
    for(k in 1:platesRequired){
      colNumber <- c(colNumber, rep(1:12, times = 8))
      for(i in 1:8){
        rowLetter <- c(rowLetter, rep(LETTERS[i], times = 12))
        }
      plateNumber <- c(plateNumber, rep(k, times = 96))
      }
    plateLayout <- paste0(rowLetter, colNumber)
    plateLayout <- data.frame(plateLayout, plateNumber)
    plateLayout <- plateLayout[1:numOfSamples,]
    return(plateLayout)
  }
}

是否有人对还有什么其他想法感到高兴?我将使用此函数来生成.csv或.txt文件,以用作不同乐器的样品名称导入,因此我在“出色功能”方面会受到一定的限制,但我认为使用ggplot会很酷制作一个显示板和样品名称的图形?

最佳答案

您不需要for循环。这是一个开始:

#some sample ids
ids <- c(LETTERS, letters)
#plate size:
n <- 96
nrow <- 8
samples <- character(n)
samples[seq_along(ids)] <- ids

samples <- matrix(samples, nrow=nrow)
colnames(samples) <- seq_len(n/nrow)
rownames(samples) <- LETTERS[seq_len(nrow)]

#   1   2   3   4   5   6   7   8  9  10 11 12
# A "A" "I" "Q" "Y" "g" "o" "w" "" "" "" "" ""
# B "B" "J" "R" "Z" "h" "p" "x" "" "" "" "" ""
# C "C" "K" "S" "a" "i" "q" "y" "" "" "" "" ""
# D "D" "L" "T" "b" "j" "r" "z" "" "" "" "" ""
# E "E" "M" "U" "c" "k" "s" ""  "" "" "" "" ""
# F "F" "N" "V" "d" "l" "t" ""  "" "" "" "" ""
# G "G" "O" "W" "e" "m" "u" ""  "" "" "" "" ""
# H "H" "P" "X" "f" "n" "v" ""  "" "" "" "" ""

library(reshape2)
samples <- melt(samples)
samples$position <- paste0(samples$Var1, samples$Var2)

#    Var1 Var2 value position
# 1     A    1     A       A1
# 2     B    1     B       B1
# 3     C    1     C       C1
# 4     D    1     D       D1
# 5     E    1     E       E1
# 6     F    1     F       F1
# 7     G    1     G       G1
# 8     H    1     H       H1
# 9     A    2     I       A2
# 10    B    2     J       B2
# 11    C    2     K       C2
# 12    D    2     L       D2
# 13    E    2     M       E2
# 14    F    2     N       F2
# 15    G    2     O       G2
# 16    H    2     P       H2
# 17    A    3     Q       A3
# 18    B    3     R       B3
# 19    C    3     S       C3
# 20    D    3     T       D3
# 21    E    3     U       E3
# 22    F    3     V       F3
# 23    G    3     W       G3
# 24    H    3     X       H3
# 25    A    4     Y       A4
# 26    B    4     Z       B4
# 27    C    4     a       C4
# 28    D    4     b       D4
# 29    E    4     c       E4
# 30    F    4     d       F4
# 31    G    4     e       G4
# 32    H    4     f       H4
# 33    A    5     g       A5
# 34    B    5     h       B5
# 35    C    5     i       C5
# 36    D    5     j       D5
# 37    E    5     k       E5
# 38    F    5     l       F5
# 39    G    5     m       G5
# 40    H    5     n       H5
# 41    A    6     o       A6
# 42    B    6     p       B6
# 43    C    6     q       C6
# 44    D    6     r       D6
# 45    E    6     s       E6
# 46    F    6     t       F6
# 47    G    6     u       G6
# 48    H    6     v       H6
# 49    A    7     w       A7
# 50    B    7     x       B7
# 51    C    7     y       C7
# 52    D    7     z       D7
# 53    E    7             E7
# 54    F    7             F7
# 55    G    7             G7
# 56    H    7             H7
# 57    A    8             A8
# 58    B    8             B8
# 59    C    8             C8
# 60    D    8             D8
# 61    E    8             E8
# 62    F    8             F8
# 63    G    8             G8
# 64    H    8             H8
# 65    A    9             A9
# 66    B    9             B9
# 67    C    9             C9
# 68    D    9             D9
# 69    E    9             E9
# 70    F    9             F9
# 71    G    9             G9
# 72    H    9             H9
# 73    A   10            A10
# 74    B   10            B10
# 75    C   10            C10
# 76    D   10            D10
# 77    E   10            E10
# 78    F   10            F10
# 79    G   10            G10
# 80    H   10            H10
# 81    A   11            A11
# 82    B   11            B11
# 83    C   11            C11
# 84    D   11            D11
# 85    E   11            E11
# 86    F   11            F11
# 87    G   11            G11
# 88    H   11            H11
# 89    A   12            A12
# 90    B   12            B12
# 91    C   12            C12
# 92    D   12            D12
# 93    E   12            E12
# 94    F   12            F12
# 95    G   12            G12
# 96    H   12            H12

使用byrow参数沿另一个方向填充矩阵:
samples <- matrix(samples, nrow=nrow, byrow=TRUE)

要填充多个板,您可以使用基本相同的方法,但是使用数组而不是矩阵。

关于r - 在R中生成96或384孔板布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20843272/

10-12 19:54