---
# System prepended metadata

title: ASR OF TROPHIC HABITS IN CALLIPHORIDAE

---

# ASR OF TROPHIC HABITS IN CALLIPHORIDAE

## BUILDING THE DATASET

after having the tree, you need a "traits file" with species sorted just like the tree; we do this in R

```
# this is how the traits file looked initially:

Auchmeromyia_bequaerti	P
Auchmeromyia_luteola	P
Cordylobia_anthropophaga	P
Pachychoeromyia_praegrandis	P

# one column for the species and one for the trait (you can use only ONE letter for the trait)
# R code to sort the species:

tree <- read.nexus(file = "TREE5_zumpt.nex")
data_zumpt <- read.table("phenotype_zumpt.txt")

# is the tree tip order the same as the V1 order?
phylo_species <- tree$tip.label
data_zumpt_sorted <- data_zumpt[match(phylo_species, data_zumpt$V1),]
sum(tree$tip.label == data_zumpt_sorted$V1) == nrow(data_zumpt_sorted)

# prints TRUE, so ready to go:

#write table
write.table(data_zumpt_sorted, "data_zumpt_sorted.txt", quote=F, row.names=F, col.names=F, sep='\t')
```

We also need the help of R to make a "control file" with will be used to pass the analyses commands to bayestraits; first the packages I will be using:

```
library(here)
library(posterior)
library(tidybayes)
library(coda)
library(btw) # remotes::install_github('rgriff23/btw')
library(tidytree)
library(ape)
library(tidyverse)
library(patchwork)
library(tidyr)
library(dplyr)
library(readr)
library(phytools)
library(EBImage)
library(ggtree)
```

the next steps are necessary to tell bayestraits to reconstruct ALL internal nodes in the tree (it will not be done by default):

```
# convert tree to a dataframe
tree_data <- as_tibble(tree)

# create an empty column for tip labels
d_nodes <- tibble(nodes = dplyr::filter(tree_data, is.na(label)) %>% pull(node),
                  label = NA)

for(i in 1:nrow(d_nodes)){
  temp_node = d_nodes$nodes[i]
  temp_offspring <- offspring(tree_data, temp_node, tiponly = TRUE)
  d_nodes$label[i] <- temp_offspring$label %>% paste0(collapse = ' ')
}

d_nodes <- mutate(d_nodes, first_command = paste('AddTag', paste('T', nodes, sep = ''), label, sep = ' '),
                  second_command = paste('AddNode', nodes, paste('T', nodes, sep = ''), sep = ' '))

to_save <- select(d_nodes, first_command, second_command) %>%
  pivot_longer(everything(), names_to = 'blah', values_to = 'command') %>%
  pull(command)

write.table(to_save, "tags_zumpt", quote=FALSE, col.names = FALSE, row.names = FALSE)

```

Now we create the control file using these info generated by the previous R script; this is how the control file looks like (the big block starting with AddTag was the one built in R):

```
1 # tells the program to run in multistate mode
2 # run in MCMC mode (1 would be ML)
RJHP exp 0 2 # set a reverse jump hyperprior with an exponential distribution seeded from 0 to 2
Iterations 10000000 # number of iterations
Sample 10000 # frequency of sampling
Burnin 1000000 # set burnin
Stones 100 10000 # this will be used to run a BayesFactor test
LogFile LOG_zumpt_exp02 # name of output
AddTag T55 Pachychoeromyia_praegrandis Auchmeromyia_luteola Auchmeromyia_bequaerti Cordylobia_anthropophaga Aldrichina_grahami Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi Calliphora_nigribasis Calliphora_vicina Calliphora_croceipalpis Sarconesia_chlorogaster Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens Lucilia_lucigerens Lucilia_cluvia Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis Protophormia_terraenovae Phormia_regina Protocalliphora_sialia Protocalliphora_azurea Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani Compsomyiops_verena Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis Hemilucilia_semidiaphana Hemilucilia_segmentaria
AddNode 55 T55
AddTag T56 Pachychoeromyia_praegrandis Auchmeromyia_luteola Auchmeromyia_bequaerti Cordylobia_anthropophaga
AddNode 56 T56
AddTag T57 Pachychoeromyia_praegrandis Auchmeromyia_luteola Auchmeromyia_bequaerti
AddNode 57 T57
AddTag T58 Auchmeromyia_luteola Auchmeromyia_bequaerti
AddNode 58 T58
AddTag T59 Aldrichina_grahami Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi Calliphora_nigribasis Calliphora_vicina Calliphora_croceipalpis Sarconesia_chlorogaster Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens Lucilia_lucigerens Lucilia_cluvia Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis Protophormia_terraenovae Phormia_regina Protocalliphora_sialia Protocalliphora_azurea Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani Compsomyiops_verena Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis Hemilucilia_semidiaphana Hemilucilia_segmentaria
AddNode 59 T59
AddTag T60 Aldrichina_grahami Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi Calliphora_nigribasis Calliphora_vicina Calliphora_croceipalpis Sarconesia_chlorogaster Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens Lucilia_lucigerens Lucilia_cluvia Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis
AddNode 60 T60
AddTag T61 Aldrichina_grahami Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi Calliphora_nigribasis Calliphora_vicina Calliphora_croceipalpis
AddNode 61 T61
AddTag T62 Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi Calliphora_nigribasis Calliphora_vicina Calliphora_croceipalpis
AddNode 62 T62
AddTag T63 Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi Calliphora_nigribasis
AddNode 63 T63
AddTag T64 Calliphora_vomitoria Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi
AddNode 64 T64
AddTag T65 Calliphora_nigribarbis Calliphora_lata Calliphora_lopesi
AddNode 65 T65
AddTag T66 Calliphora_nigribarbis Calliphora_lata
AddNode 66 T66
AddTag T67 Calliphora_vicina Calliphora_croceipalpis
AddNode 67 T67
AddTag T68 Sarconesia_chlorogaster Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens Lucilia_lucigerens Lucilia_cluvia Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis
AddNode 68 T68
AddTag T69 Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens Lucilia_lucigerens Lucilia_cluvia Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis
AddNode 69 T69
AddTag T70 Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens
AddNode 70 T70
AddTag T71 Lucilia_silvarum Lucilia_richardsi Lucilia_sericata Lucilia_cuprina
AddNode 71 T71
AddTag T72 Lucilia_silvarum Lucilia_richardsi Lucilia_sericata
AddNode 72 T72
AddTag T73 Lucilia_silvarum Lucilia_richardsi
AddNode 73 T73
AddTag T74 Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens
AddNode 74 T74
AddTag T75 Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis Lucilia_caesar
AddNode 75 T75
AddTag T76 Lucilia_porphyrina Lucilia_ampullacea Lucilia_papuensis
AddNode 76 T76
AddTag T77 Lucilia_porphyrina Lucilia_ampullacea
AddNode 77 T77
AddTag T78 Hemipyrellia_pulchra Hemipyrellia_fernandica Hemipyrellia_ligurriens
AddNode 78 T78
AddTag T79 Hemipyrellia_pulchra Hemipyrellia_fernandica
AddNode 79 T79
AddTag T80 Lucilia_lucigerens Lucilia_cluvia Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis
AddNode 80 T80
AddTag T81 Lucilia_lucigerens Lucilia_cluvia
AddNode 81 T81
AddTag T82 Lucilia_mexicana Lucilia_eximia Lucilia_caeruleiviridis
AddNode 82 T82
AddTag T83 Lucilia_mexicana Lucilia_eximia
AddNode 83 T83
AddTag T84 Protophormia_terraenovae Phormia_regina Protocalliphora_sialia Protocalliphora_azurea Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani Compsomyiops_verena Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis Hemilucilia_semidiaphana Hemilucilia_segmentaria
AddNode 84 T84
AddTag T85 Protophormia_terraenovae Phormia_regina Protocalliphora_sialia Protocalliphora_azurea Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani
AddNode 85 T85
AddTag T86 Protophormia_terraenovae Phormia_regina
AddNode 86 T86
AddTag T87 Protocalliphora_sialia Protocalliphora_azurea Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani
AddNode 87 T87
AddTag T88 Protocalliphora_sialia Protocalliphora_azurea
AddNode 88 T88
AddTag T89 Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani
AddNode 89 T89
AddTag T90 Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis Chrysomya_putoria Chrysomya_chloropyga
AddNode 90 T90
AddTag T91 Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps Chrysomya_marginalis
AddNode 91 T91
AddTag T92 Chrysomya_villeneuvi Chrysomya_rufifacies Chrysomya_albiceps
AddNode 92 T92
AddTag T93 Chrysomya_rufifacies Chrysomya_albiceps
AddNode 93 T93
AddTag T94 Chrysomya_putoria Chrysomya_chloropyga
AddNode 94 T94
AddTag T95 Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana Chrysomya_chani
AddNode 95 T95
AddTag T96 Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala Chrysomya_bezziana
AddNode 96 T96
AddTag T97 Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes Chrysomya_megacephala
AddNode 97 T97
AddTag T98 Chrysomya_thanomthini Chrysomya_pinguis Chrysomya_nigripes
AddNode 98 T98
AddTag T99 Chrysomya_pinguis Chrysomya_nigripes
AddNode 99 T99
AddTag T100 Compsomyiops_verena Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis Hemilucilia_semidiaphana Hemilucilia_segmentaria
AddNode 100 T100
AddTag T101 Compsomyiops_verena Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis
AddNode 101 T101
AddTag T102 Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis
AddNode 102 T102
AddTag T103 Cochliomyia_minima Cochliomyia_macellaria Cochliomyia_hominivorax
AddNode 103 T103
AddTag T104 Cochliomyia_minima Cochliomyia_macellaria
AddNode 104 T104
AddTag T105 Paralucilia_pseudolyrcea Paralucilia_fulvinota Paralucilia_paraensis
AddNode 105 T105
AddTag T106 Paralucilia_pseudolyrcea Paralucilia_fulvinota
AddNode 106 T106
AddTag T107 Hemilucilia_semidiaphana Hemilucilia_segmentaria
AddNode 107 T107
Run
```

To run bayestraits:

```
./BayesTraitsV4 TREE5_zumpt.nex data_zumpt_sorted.txt < control_zumpt_covarion.txt 
```

Now we take the output and remove all unecessary data keeping only the reconstructed states and calculate the average. Easy to do in excel. Then parse an awk to get the following format:

```
awk -v RS='\t' '{printf $0 "\t"} NR%3 == 0 {print ""}' input.txt > output
```

The final file should look like:

```
0.409900477777777	0.173865247777778	0.416234034444443	
0.252614996666667	0.500092979999999	0.247291871111112	
0.197729334444445	0.612524225555555	0.189746361111111	
0.155221598888889	0.694552817777778	0.150225552222222	
```
where each row is a node in the phylogeny and each column is the probability of one of the 3 states

Lastly we add a new column with the corresponding node number. Nodes will always start with n+1 where n is the number of taxa in the phylogeny. So in this case, where we have 54 taxa, node numbers start with 55; the output will look like:

```
X0	X1	X2	node
0.409900477777777	0.173865247777778	0.416234034444443	55
0.252614996666667	0.500092979999999	0.247291871111112	56
0.197729334444445	0.612524225555555	0.189746361111111	57
0.155221598888889	0.694552817777778	0.150225552222222	58
0.421222236666667	0.107296277777778	0.471481452222222	59
```
