How to read metadata into R in QIIME2 plugin

plugin-development
metadata

(John Chase) #1

I am developing a QIIME2 plugin that uses R. The R script that is ultimately called needs to access the metadata file.

With many of the formats there is an option to provide a format that will return a string to a temporary directory containing the object (or it at least appears that way). With something like this I can then just read the file in R

example:

    inputs={'table': 'phylogeny': Phylogeny[Unrooted])

Works well because in the function that is called I can pass as the object type in the parameters to the function as:

phylogeny: NewickFormat

Now this can be passed directly to an R command as a filepath

tree <- read.tree(tree.path)

How do I do this same thing with metadata? If for example I define the metadata type as:

metadata: Metadata

Now I have metadata object in python that I need to be able to get into R somehow. I could write this out to tsv and read it back in:

metadata.to_dataframe().to_csv(filepath)

Then in R I can read the table:

read.csv(filepath)

But this really seems like a round-a-about way to do it.

Alternatively I could call str on the metadata object and then pass that to R:

read.csv(text='a\tb\tc\n1\t2\t3\n4\t5\t6')

But I have no idea what the delimiter is, it appears to be different numbers of spaces.

Any suggestions?

Thank you for your help!


(Matthew Ryan Dillon) #2

(Matthew Ryan Dillon) #3

Hey there @John_Chase!

Excellent!

You can use the built-in .save() method on your Metadata object to save it somewhere that your R script can read in from.

Yeah, it is a bit of a pain, but it pretty much how that newick example above works, too. The advantage of calling Metadata.save is that you get all of the metadata validation and formatting in a consistent manner, versus calling .to_dataframe().to_csv().

Interesting idea! Personally I would go with saving to disk first, seems more predictable and easy to keep track of, but, that is just me!

Hope that helps — keep us posted! :t_rex: :qiime2:


(Matthew Ryan Dillon) #4

(John Chase) #5

Hey,

Thanks Matt, I appreciate your feedback! It looks like I wasn’t too far off on the “best” way to approach this. I will end up using the .save() method here, as you suggested.

I’ll keep you updated on how this all comes together, hopefully it will be useful for others as well!