ChatGPT是一款强大的语言模型,不仅可以回答问题和进行对话,还可以辅助编写代码。通过提供清晰的问题或指令,可以从ChatGPT获得代码段、算法示例以及编程建议。它支持多种编程语言和领域,为编程任务提供快速而可靠的帮助,无论是解决问题、学习新技术还是优化代码结构,ChatGPT都能助你一臂之力。
我们以绘制基因共表达网络图为例展示chatGPT在写代码方面的作用。首先提出问题“绘制共表达网络图”,描述数据格式并指定用R语言实现这一过程,chatGPT给出的方法是使用igraph
包根据随机数据生成无向图对象并绘图。
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
| install.packages("igraph") library(igraph)
set.seed(123) num_genes <- 100 genes <- paste0("Gene", 1:num_genes) edges <- data.frame( gene1 = sample(genes, num_genes, replace = TRUE), gene2 = sample(genes, num_genes, replace = TRUE), weight = runif(num_genes) )
graph <- graph.data.frame(edges, directed = FALSE)
plot(graph, layout = layout_with_fr(graph), vertex.label = NA, edge.color = "gray", edge.width = edges$weight * 3, main = "Gene Co-expression Network" )
|
这个图中的边的宽度根据权重大小变化,但是其变化程度并不明显,因此向chatGPT进一步提出增加边宽度变化程度。
点击展开代码块
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
| install.packages("igraph") library(igraph)
set.seed(123) num_genes <- 100 genes <- paste0("Gene", 1:num_genes) edges <- data.frame( gene1 = sample(genes, num_genes, replace = TRUE), gene2 = sample(genes, num_genes, replace = TRUE), weight = runif(num_genes, min = 0.1, max = 1) )
graph <- graph.data.frame(edges, directed = FALSE)
max_weight <- max(edges$weight) edge_widths <- edges$weight / max_weight * 5
plot(graph, layout = layout_with_fr(graph), vertex.label = NA, edge.color = "gray", edge.width = edge_widths, main = "Gene Co-expression Network" )
|
在新的代码中由原来的edges$weight * 3
变成了edges$weight / max_weight * 5
,对权重的范围进行了调整,并计算了边的宽度,使得权重大的边显示得更加明显。进一步我想将权重最大的三个基因挑选出来,标注Gene ID,并用不同颜色突出表示。
点击展开代码块
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
| install.packages("igraph") library(igraph)
set.seed(123) num_genes <- 100 genes <- paste0("Gene", 1:num_genes) edges <- data.frame( gene1 = sample(genes, num_genes, replace = TRUE), gene2 = sample(genes, num_genes, replace = TRUE), weight = runif(num_genes, min = 0.1, max = 1) )
graph <- graph.data.frame(edges, directed = FALSE)
gene_weights <- tapply(edges$weight, edges$gene1, sum) top_genes <- names(sort(gene_weights, decreasing = TRUE)[1:3])
max_weight <- max(edges$weight) edge_widths <- edges$weight / max_weight * 5
vertex_colors <- ifelse(V(graph)$name %in% top_genes, "red", "blue")
plot(graph, layout = layout_with_fr(graph), vertex.label = NA, vertex.color = vertex_colors, edge.color = "gray", edge.width = edge_widths, main = "Gene Co-expression Network" )
|
新的结果中使用tapply
函数计算gene1列基因的weight之和随后排序选出前三个基因作为高权重基因。使用vertex.color
参数,在图中将其用红色表示出来。但是vertex.label
参数仍然未设置,我们要求的Gene ID并没有标注,因此我们将这一问题反馈给chatGPT。
点击展开代码块
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
| install.packages("igraph") library(igraph)
set.seed(123) num_genes <- 100 genes <- paste0("Gene", 1:num_genes) edges <- data.frame( gene1 = sample(genes, num_genes, replace = TRUE), gene2 = sample(genes, num_genes, replace = TRUE), weight = runif(num_genes, min = 0.1, max = 1) )
graph <- graph.data.frame(edges, directed = FALSE)
gene_weights <- tapply(edges$weight, edges$gene1, sum) top_genes <- names(sort(gene_weights, decreasing = TRUE)[1:3])
max_weight <- max(edges$weight) edge_widths <- edges$weight / max_weight * 5
vertex_colors <- ifelse(V(graph)$name %in% top_genes, "red", "blue") vertex_labels <- ifelse(V(graph)$name %in% top_genes, V(graph)$name, NA)
plot(graph, layout = layout_with_fr(graph), vertex.label = vertex_labels, vertex.color = vertex_colors, edge.color = "gray", edge.width = edge_widths, main = "Gene Co-expression Network" )
|
最终我们得到一个基因共表达网络图,其中重要基因使用不同颜色突出显示,并标注出Gene ID。我们向chatGPT提出要求,可以实现快速编写代码,通过对chatGPT提出新的要求可以进一步对结果进行修改。但是chatGPT有时不能给出完全符合预期的结果,需要我们对其进行引导,而且给出的方案也并不一定完全正确,上述代码中挑选高权重基因的方法没有考虑gene2列。因此我们需要向chatGPT提出清晰明确的要求并不断引导,从而正确实现我们的目的,提高工作效率。