error while saving neurons as swc format

30 views
Skip to first unread message

Jingpeng Wu

unread,
Oct 22, 2018, 5:37:08 PM10/22/18
to nat-user
Dear Greg,

I am trying to save neurons as swc format. It saved two neurons successfully, but encountered some errors while saving the third one.
This is the code I am running:

```
library(nat)
library(nat.nblast)
write.neurons(kcs20, "/tmp/", format="swc", Force=TRUE)
```

I got the following errors:
```
Error in basename(attr(n, "file")): a character vector argument expected
Traceback:

1. write.neurons(kcs20, "/tmp/", format = "swc", Force = TRUE)
2. write.neuron(n, dir = thisdir, file = files[nn], format = format, 
 .     Force = Force, ...)
3. basename(attr(n, "file"))
```

do you have any idea what is the problem?

Greg Jefferis

unread,
Oct 22, 2018, 6:36:33 PM10/22/18
to nat-user
Dear Jingpeng,

nat stores the name of the original input file as a field in the neuron object when you use read.neurons and friends. This field is then used to construct a default output name when you come to write out the neuron. See the documentation of write.neuron for details.

In this case one or more of the neurons in your input object, kcs20, does not contain that field. I can’t really say why, but perhaps you carried out some manipulation on the neuron. Alternatively, perhaps you are not writing out the objects that you mean. kcs20 is a sample neuronlist object that is provided with the nat package – it contains dotprops rather than neuron objects. These can be written as swc files, but those files may not be very useful since they will consist of lots of isolated segments. You can see the input file for those objects like so:

sapply(kcs20, attr, 'file')

indeed the third neuron in that sample data does not have an associated filename.

Assuming that you are writing out the object that you intend, you can sidestep the issue like so:

write.neurons(kcs20, dir="/tmp/", files = paste0(names(kcs20), '.swc'))

This makes a name for every output file explicitly. You can omit the format argument since this is implicit in the swc suffix of the file names passed into the files argument.

Hope this is clear. Best wishes,

Greg.

Jingpeng Wu

unread,
Oct 23, 2018, 10:05:29 AM10/23/18
to nat-user
thanks for the help, your solution works!
Reply all
Reply to author
Forward
0 new messages