-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_tiplabels.R
46 lines (35 loc) · 1.78 KB
/
change_tiplabels.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env Rscript
###########################################################################
# Project: Orania Phylogeny MT
# Script: change_tiplabels.R
# --- Action: Change tiplabels from TAG notation to individual names
# --- Input: tree with tiplabels in TAG notation
# --- Output: tree with tiplabels in individual name notation
# Author: Maya Schroedl ([email protected])
###########################################################################
#rm(list=ls())
# Packages ---------------------------------------------------------------
if (!require('ape')) install.packages('ape'); library('ape')
if (!require('phylotools')) install.packages('phylotools'); library('phylotools')
# Function -----------------------------------------------------------------
change_tip_labs=function(input_tree,output_tree,tag_indiv_df){
tree=read.tree(input_tree) #read the newick tree
lab_tree=sub.taxa.label(tree, read.table(tag_indiv_df,h=F,stringsAsFactors = F)) #replace tiplables
#output
write.tree(lab_tree, output_tree)
#When astral output with "q1, q2, & q3", the [q1, q2, q3] needs to be written as '[...]'
system(paste0('sed -i -e ', '"s/\\[/\'\\[/g" ', output_tree))
system(paste0('sed -i -e ', '"s/\\]/\\]\'/g" ', output_tree))
# and - needs to be replaced by ;
system(paste0('sed -i -e ', '"s/-q/;q/g" ', output_tree))
# Because this produces NaN as branchlengths for the tiplabels in astral trees, we want to remove the ":NaN"
system(paste('sed -i -e "s/:NaN//g"', output_tree))
}
# Arguments ---------------------------------------------------------------
args = commandArgs(trailingOnly=TRUE)
if(length(args) != 0){
input_tree = args[1]
output_tree = args[2]
tag_indiv_df = read.table(args[3],sep="\t",h=T)
change_tip_labs(input_tree,output_tree,tag_indiv_df)
}