Error: unexpected '}' in " }" in R codes of Seurat
3
0
Entering edit mode
4.9 years ago
vHelmholtz ▴ 40

Hello,

I am practicing R codes in the Seurat tutorials. When I type

for (i in 0:12) {
  g[i].marker <- FindConservedMarkers(immune.combined, ident.1 = i, 
  grouping.var = "stim", verbose = FALSE)
   }

It gives me an error as below.

Error: unexpected symbol in: "for (i in 0:12) {   g[i].marker"
  } Error: unexpected '}' in "  }"

How can I fix this error? Please help!

R Seurat • 3.1k views
ADD COMMENT
2
Entering edit mode
4.9 years ago
vHelmholtz ▴ 40

Hi Fatima,

Thank you for your advice. It gives me 'g_marker not found'. Could you give me any suggestion about this issue?

Thanks in advance!

> for (i in 0:12) {
    g_marker[i] <- FindConservedMarkers(immune.combined, ident.1 = i, 
    grouping.var = "stim", verbose = FALSE)
    }

Error: object 'g_marker' not found
ADD COMMENT
0
Entering edit mode

what if you add g_marker <- 0 before the for loop to initialize the array?

https://www.datacamp.com/community/tutorials/tutorial-on-loops-in-r

could you try this command to see what is the output? I assume it's of type string.

a <- FindConservedMarkers(immune.combined, ident.1 = 0, 
    grouping.var = "stim", verbose = FALSE)
class(a)

Also you can try with a simpler code to see if your loop and your array work as expected, for example these scripts works for me

Numeric array:

g_marker <- 0
for (i in 0:4) { g_marker[i] <- i }
g_marker
> g_marker
[1] 1 2 3 4

String array:

# initialize the array with ""
g_marker <- ""
for (i in 0:4) { g_marker[i] <- sprintf("Gene %d",i) }
g_marker
> g_marker
[1] "Gene 1" "Gene 2" "Gene 3" "Gene 4"
ADD REPLY
0
Entering edit mode

Hi Fatima,

Thank you for your kind advice. I tried the simpler code and found that it works as expected. However, when I type add g_marker <- 0 before the for loop,

g_marker <- 0
for (i in 0:12) {
  g_marker[i] <- FindConservedMarkers(immune.combined, ident.1 = i, 
  grouping.var = "stim", verbose = FALSE)
  }

it gave me errors as below.

There were 12 warnings (use warnings() to see them)    
> warnings()
Warning messages:
1: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
2: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
3: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
4: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
5: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
6: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
7: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
8: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
9: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
10: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
11: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
12: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
ADD REPLY
0
Entering edit mode

Could you just run

FindConservedMarkers(immune.combined, ident.1 = 0, 
    grouping.var = "stim", verbose = FALSE)

And see what the output is? It might be a complicated output and we need to see the format before deciding on how to save it in an array.

based on the link below the output is a data.frame. https://www.rdocumentation.org/packages/Seurat/versions/3.1.2/topics/FindConservedMarkers

For a simple string output (e.g Gene1) you could add g_marker <- "" before the loop instead of 0, if you post a sample output I might be able to help on how to save it in a suitable form. For example if the output is a big data.frame then you can write the result of each iteration in a new csv file or you can use a script like this:

for (i in 0:12) {
g <- paste("g_marker_", i, sep = "")
assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE))
#print(g_marker_i)
}

Simpler script:

g <- paste("g_marker", 1, sep = "")
assign(g, 10)
> g_marker1
10

Another simpler but irrelevant script:

g <- paste("g_marker", 1, sep = "")
# create a sample dataframe
set.seed(1)
mydf <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100))
assign(g, mydf)
print(g_marker1)

https://thomasleeper.com/Rcourse/Tutorials/savingdata.html

https://stackoverflow.com/questions/16566799/change-variable-name-in-for-loop-using-r

ADD REPLY
0
Entering edit mode

Hi Fatima,

Thank you so much for your comments. I add g_marker <- "" before the loop and also got an error as below, which is similar to the result of g_marker <- 0.

 > g_marker <- ""
 > for (i in 0:12) {
+   g_marker[i] <- FindConservedMarkers(immune.combined, ident.1 = i, 
    grouping.var = "stim", verbose = FALSE)
+   }
There were 12 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
2: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
3: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
4: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
5: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
6: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
7: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
8: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
9: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
10: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
11: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
12: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
ADD REPLY
0
Entering edit mode

Since the output is a data.frame you can do the following as mentioned in my previous comment.

for (i in 0:12) {
g <- paste("g_marker_", i, sep = "")
assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE))
print(g_marker_i)
}

You can read more about paste and assign here: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/paste https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/assign

You can also use this script to save each result in a file:

for (i in 0:12) {
filename <- paste("g_marker", i, ".csv", sep = "")
result <- FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE)
write.csv(result, file=filename)
}
# this command gives you the current directory
getwd()

Then you can find the following files in your current directory:

g_marker1.csv, g_marker2.csv,.....

Another simple approach can be just manually assigning values 1 to 12 to ident.1:

    g_marker_1 <- FindConservedMarkers(immune.combined, ident.1 = 1, 
            grouping.var = "stim", verbose = FALSE)
   .....
    g_marker_12 <- FindConservedMarkers(immune.combined, ident.1 = 12, 
            grouping.var = "stim", verbose = FALSE)
ADD REPLY
0
Entering edit mode

Hi Fatima,

Thank you for your advice. Your code works very well.

for (i in 0:12) {
  g <- paste("g_marker_", i, sep = "")
  assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
                                  grouping.var = "stim", verbose = FALSE))
  #print(g_marker_i)
}
ADD REPLY
0
Entering edit mode

do the following:

g_marker <-  list()
for (i in 0:12) {
   g_marker[[i]] <- FindConservedMarkers(immune.combined, ident.1 = i, 
   grouping.var = "stim", verbose = FALSE)
}
ADD REPLY
1
Entering edit mode
4.9 years ago
Fatima ▴ 1000

I think the problem is with g[i].marker maybe using g_marker[i] would solve the problem.

**update: since the output of FindConservedMarkers is a data.frame you can use a script similar to these ones:

for (i in 0:12) {
g <- paste("g_marker_", i, sep = "")
assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE))
print(g_marker_i)
}

for (i in 0:12) {
filename <- paste("g_marker", i, ".csv", sep = "")
result <- FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE)
write.csv(result, file=filename)
}

https://www.rdocumentation.org/packages/Seurat/versions/3.1.2/topics/FindConservedMarkers https://www.tutorialspoint.com/r/r_data_frames.htm

ADD COMMENT
1
Entering edit mode
4.9 years ago

The error is happening is because of g[i].marker. Are you sure there exists a variable/list as g.

Assuming it exists, g[i] would point to an appropriate element (only for a vector, if g is a list then g[[i]] has to be used). The "." following creates the error. Unlike other languages, R is based on Lisp where "." is used as a part of naming convention and is used in place of "_' for naming variables compared to other languages such as python, C. Further, compared to python where dot operator is used to access the object attributes and functions, it does not have special properties for most of the cases (barring formulas) in R.

ADD COMMENT

Login before adding your answer.

Traffic: 1927 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