Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output file to csv #83

Open
Lithobius opened this issue Jan 24, 2019 · 2 comments
Open

Output file to csv #83

Lithobius opened this issue Jan 24, 2019 · 2 comments

Comments

@Lithobius
Copy link

Lithobius commented Jan 24, 2019

I wrote a quick Python script to convert the output of mPTP to a CSV file so that I could use the output with ggmap and with other things where I might want to use the output of mPTP to sort my data in lieu of other elements of my spreadsheets.

It would be nice if mPTP had a built-in option to export csv versions of the output alongside the txt file.

Here is the script I wrote, it might not be perfect but if anyone else needs it it is there:
https://github.com/Lithobius/lazylithobius/blob/master/mPTP2csv./mptp2csv.py

@stamatak
Copy link
Collaborator

stamatak commented Jan 25, 2019 via email

@boopsboops
Copy link

boopsboops commented Jan 24, 2022

If any use to anyone, here's also quick R function to read mPTP output as two-column tabular format:

# load libs
library("tidyverse")

# load func
read_mptp <- function(file,skiplines) {
    mptp.raw <- readr::read_delim(file,skip=skiplines,delim=",",col_names="individual",show_col_types=FALSE)
    mptp.tab <- mptp.raw %>% 
        dplyr::mutate(species=ifelse(grepl(":",individual),individual,NA)) %>%
        tidyr::fill(species,.direction="down") %>%
        dplyr::filter(!grepl(":",individual)) %>%
        dplyr::mutate(species=stringr::str_replace_all(species,":| ","")) %>%
        dplyr::relocate(species,.before=individual)
    return(mptp.tab)
}

# run func
my.mptp.table <- read_mptp(file="path/to/mptp-output.txt",skiplines=6)

# write out if required
readr::write_csv(my.mptp.table,file="path/to/mptp-output.csv")

Note: the skiplines option ignores the first n lines of the mPTP output file containing model parameters. Therefore make sure the skiplines option is correct for your mPTP output. With delimitations starting on line 7, set skiplines to 6 (skiplines=6). If the model parameters are required in the table, then the script should be easy to adapt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants