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

Convert taxon structure to newick format #17

Open
MichWeb75 opened this issue Mar 4, 2020 · 2 comments
Open

Convert taxon structure to newick format #17

MichWeb75 opened this issue Mar 4, 2020 · 2 comments

Comments

@MichWeb75
Copy link

@MichWeb75 MichWeb75 commented Mar 4, 2020

Do you know a simple way to convert the result of a taxonomic search into newick format to plot it with ggtree or ITOL ?
I found a function here https://alakazam.readthedocs.io/en/stable/topics/graphToPhylo/, but maybe you know a more elegant way ?

@sherrillmix

This comment has been minimized.

Copy link
Owner

@sherrillmix sherrillmix commented Mar 5, 2020

That might be interesting to incorporate. I guess as a first pass, maybe a function like:

makeNewick<-function(taxa,naSub='_'){
  if(!is.null(naSub))taxa[is.na(taxa)]<-naSub
  if(ncol(taxa)==0)return('')
  bases<-unique(taxa[,1])
  innerTree<-sapply(bases,function(ii)makeNewick(taxa[taxa[,1]==ii,-1,drop=FALSE]))
  out<-sprintf('(%s)',paste(sprintf('%s%s',innerTree,bases),collapse=','))
  return(out)
}

where taxa would be the output from getTaxonomy e.g.

taxaId<-getId(c('Homo sapiens','Bos taurus','Homo','Alces alces'),'accessionTaxa.sql')
taxa<-getTaxonomy(taxaId,'accessionTaxa.sql')
message(makeNewick(taxa))

Producing an output like:

(((((((Homo sapiens,_)Homo)Hominidae)Primates,(((Bos taurus)Bos)Bovidae,((Alces alces)Alces)Cervidae)_)Mammalia)Chordata)Eukaryota)

Is that what you're looking for? I guess I could incorporate that into the package if useful.

@MichWeb75

This comment has been minimized.

Copy link
Author

@MichWeb75 MichWeb75 commented Mar 6, 2020

That is exactly what I was looking for and very smart implementation without any need for additional network structure. This function would be useful to load the graph into other packages such ape and ggtree.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.