Trouble annotating heatmap with pheatmap. Error in annotation_col[colnames(mat), , drop = F] : subscript out of bounds
0
0
Entering edit mode
12 months ago

Hi everyone,

I would like to ask if anyone could help me with Pheatmap. I am trying to annotate my heatmap, but nothing is working. I already checked if the dimensions and column names fit in the metadata and counts file (you can find all the tests in the code). Everything is ok with the files, but I still cannot annotate the heatmap. Every time it gives me an error: annotation_col[colnames(mat), drop = F]: subscript out of bounds. I would really appreciate if someone could help.

here's a code that I am using:

#Load your data into R as a dataframe and matrix:
metadata <- read.csv("matrix_metadata_cluster.csv", header= TRUE, sep = ";")
counts <- read.csv("heatmap_clusters_our_own.csv", header = TRUE,  row.names="Gene", sep = ",")

# Extract the cluster information and annotation from metadata
clusters <- metadata$Cluster
cluster_annotation <- metadata$Cluster_annotation

# Create a matrix for annotations with the same number of rows as counts_t
annotation_matrix <- cbind(clusters, cluster_annotation)
annotation_matrix_t <- t(annotation_matrix)

colnames(annotation_matrix_t) <- colnames(counts)

annotation_matrix_t <- as.matrix(annotation_matrix_t, rownames.force = TRUE)

# Set the color palette for the heatmap
color_palette <- colorRampPalette(brewer.pal(9, "Blues"))(100)


# Check correspondence between annotation_matrix_t and counts_1 columns
annotation_cols <- colnames(annotation_matrix_t)
counts_cols <- colnames(counts)

if (identical(annotation_cols, counts_cols)) {
  print("Column names match.")
} else {
  print("Column names do not match.")

  # Check for any mismatched column names
  mismatched_cols <- setdiff(annotation_cols, counts_cols)
  if (length(mismatched_cols) > 0) {
    print("Mismatched columns in annotation_matrix_t:")
    print(mismatched_cols)
  }

  mismatched_cols <- setdiff(counts_cols, annotation_cols)
  if (length(mismatched_cols) > 0) {
    print("Mismatched columns in counts:")
    print(mismatched_cols)
  }
}

[1] "Column names match."


#Check if colnames are the same

identical(colnames(counts), colnames(annotation_matrix_t))
[1] TRUE

# Check the dimensions
dim(counts)
[1]  127 8846
dim(annotation_matrix_t)
[1]    2 8846

# Compare column names
colnames(counts)
colnames(annotation_matrix_t)

# Check column order
if (identical(colnames(annotation_matrix_t), colnames(counts))) {
  print("Column order matches.")
} else {
  print("Column order does not match.")
}
[1] "Column order matches."

#Check column number
if (ncol(annotation_matrix_t) == ncol(counts)) {
  print("Number of columns matches.")
} else {
  print("Number of columns does not match.")
}
[1] "Number of columns matches."

# Create the heatmap using pheatmap


pheatmap(counts,
         annotation_col = annotation_matrix_t,
         clustering_distance_rows = "euclidean",
         clustering_distance_cols = "euclidean",
         clustering_method = "complete",
         scale = "row",
         color = color_palette)

Error in annotation_col[colnames(mat), , drop = F] : 
  subscript out of bounds
pheatmap • 1.3k views
ADD COMMENT
0
Entering edit mode

What is row.names="Gene" supposed to do here?

counts <- read.csv("heatmap_clusters_our_own.csv", header = TRUE,  row.names="Gene", sep = ",")
ADD REPLY
0
Entering edit mode

it is just to specify a column with row.names, I also tried row.names =1 and row.names = TRUE

ADD REPLY

Login before adding your answer.

Traffic: 2485 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6