Exporting chart data from command line

I wish to create textual output of the number of read assignments to each taxonomic group, in this case Phyla. In the GUI I do this by opening the rma6 file, collapsing on rank “Phylum”, opening a barchart, and then, in the File menu, choosing Export Data.

This produces a file with the following contents:

#Series: noMap.500.fasta-vs-nt.1e-5
Firmicutes 69.0
Ascomycota 18.0
Arthropoda 4.0
Streptophyta 212.0

To script this, I copied the commands executed by Megan in the Message Window into a file (cmd_file) and run it from the command line like so:

~/MEGAN6/MEGAN -g -c cmd_file

cmd_file contents:

open file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-nt.1e-5.rma6’;
update;
collapse rank=‘Phylum’;
select rank=‘Phylum’;
show chart drawer=BarChart data=‘Taxonomy’;
export what=chartData file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-n-chart’;

This produces the following output and error.

Opening startup files
Open startup files:
Loading ncbi.map
Loading ncbi.map: 2,031,704
Loading ncbi.tre
Loading ncbi.tre: 2,031,708
updating viewer
Induced tree has 2,031,708 of 2,031,708 nodes
Command: open file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-nt.1e-5.rma6’;
Executing: open file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-nt.1e-5.rma6’;
Info: Opened file ‘/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-nt.1e-5.rma6’ with 500 reads
Executing: update;
updating viewer…
Induced tree has 121 of 2,031,708 nodes
Induced tree has 121 of 2,031,708 nodes
Command: update;
Executing: update;
updating viewer…
Induced tree has 121 of 2,031,708 nodes
Induced tree has 121 of 2,031,708 nodes
Command: collapse rank=‘Phylum’;
Executing: collapse rank=‘Phylum’;
Induced tree has 121 of 2,031,708 nodes
Induced tree has 121 of 2,031,708 nodes
Command: select rank=‘Phylum’;
Executing: select rank=‘Phylum’;
Command: show chart drawer=BarChart data=‘Taxonomy’;
Executing: show chart drawer=BarChart data=‘Taxonomy’;
Command: export what=chartData file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-n-chart’;
Executing: export what=chartData file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-n-chart’;
Failed to parse command: export what = chartData file = /project/MA_contamination/work/data/julies/noMap.500.fasta-vs-n-chart
Similar commands:
etc

What am I doing wrong? Am I incorrect in trying to use Message Window information to create scripts. I see no mention of “chartData” in the online megan manual.

Hi Rosema1,

Using the Message Window is usually the best way to identify commands for your script. Your script looks good up until the show chart drawer=BarChart data=‘Taxonomy’; command.

MEGAN has a separate menu for exporting the underlying data used for plotting. You can access it from the main phylogenetic window by selecting some nodes, and then going to File --> Export --> Text (CSV) Format...

From here, there’s a dropdown menu that allows you select various statistics, such as taxonName_to_count. Please try the following script and let me know if this is what you’re looking for:

open file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-nt.1e-5.rma6’;
update;
collapse rank=‘Phylum’;
select rank=‘Phylum’;
export what=CSV format=taxonName_to_count separator=tab counts=assigned file=’/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-n-chart’;

Best,
Ted

There is one line missing in your script:

After opening the chart viewer, you have to change the “command context”. Each window in MEGAN UE has its own command parser. When running the GUI, the context is always set to the window that last had focus. In the command line version, you have to specifically set this. So, after opening the chart window, add this:

set context=TaxaChart;

In the current release of MEGAN, UE when using the command enter dialog, the context is mentioned at the bottom of the dialog, but unfortunately, not using the name that the “set context” command expects. In the next release, you will see the correct name of the context, e.g. “context=TaxaChart”.

open file=‘/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-nt.1e-5.rma6’;
collapse rank=Phylum;
select rank=Phylum;
show chart drawer=BarChart data=Taxonomy;
set context=TaxaChart;
export what=chartData file=‘/project/MA_contamination/work/data/julies/noMap.500.fasta-vs-n-chart’;
quit;

I have just uploaded a new release in which the context is clearly stated at the bottom of the command input dialog.

Hi Daniel,
Is there a way to export all a taxonomic information via command line version of MEGAN or Windows version that gives me a .tsv or .csv file similar to DIAMOND? Diamond produces a perfect .tsv table from all my blastx hits against the NCBI database, but did not provide order, subfamily, family, genus or species, so I ended up meganizing my blastx via Diamond into a .daa file and uploaded it to MEGAN, but I still can’t figure out how to generate the same table as Diamond with all the taxonomic information from the NCBI database. Can MEGAN do this?

qseqid sseqid pident length mismatch evalue bitscore staxids sscinames sskingdoms skingdoms sphylums stitle
tig00000086 YP_001426684.1 100 40 0 4.50E-19 84.3 322019 Acanthocystis turfacea chlorella virus 1 Viruses Bamfordvirae Nucleocytoviricota YP_001426684.1 ubiquitin family protein [Acanthocystis turfacea chlorella virus 1]