gpt4 book ai didi

r - 在 R 中生成 96 或 384 孔板布局

转载 作者:行者123 更新时间:2023-12-02 07:32:01 40 4
gpt4 key购买 nike

我正在尝试编写一些代码,将包含一些样本名称的 .csv 文件作为输入,并将输出包含样本名称和 96 孔板或 384 孔板格式(A1、B1 , C1...)。对于那些不知道的人,96 孔板有八个按字母顺序标记的行(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...) 或交叉 (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) 

但事情并没有那么简单。如果您要“向下”,则必须在列号增加 1 之前填写所有 8 行;如果您要“交叉”,则必须在行字母增加 1 之前填写所有 12 列。

编辑:

这是一个笨重的版本。它需要您拥有的样本数量、尚未起作用的“板格式”和方向,并将返回包含孔和板号的数据框。接下来,我将 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 会很酷制作显示板和 sample 名称的图形?

最佳答案

您不需要 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/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com