master<-read.csv(file="~/Documents/ants_june_to_august_2016/ants.ncbi.14july2016/14july2016_antcat_aligns_fullalign_einsi_nogappy_wadds_heavilymodified_wo_results/antwiki_august2016/AntWiki_Valid_Species_w_fossils_mymods.csv",stringsAsFactors=FALSE)
master.ext<-master[master$Status %in% "Extant",]
#table(master$Genus)
seqs<-read.csv(file="~/Desktop/14july2016_antcat_aligns_fullalign_einsi_nogappy_wadds_heavilymodified_correctedmacse/Hymenoptera/drop_bads_again/combined.updated.updated.pruned.wouts.badsremoved.badsremovedagain.nopheidrectispina.csv",stringsAsFactors=FALSE)
seqs<-seqs[seqs$Family %in% "Formicidae",]
seqs$Genus_Richness<-seqs$Genus
master.ext$Genus_Richness<-master.ext$Genus

#if only genus rep falls within another genus, the whole genus was merged into other, regardless of whether type
seqs$Genus_Richness[seqs$Genus %in% c("Adetomyrma", "Xymmer", "Myopopone", "Mystrium","Stigmatomma")]<-"Stigmatomma_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Adetomyrma", "Xymmer", "Myopopone", "Mystrium","Stigmatomma")]<-"Stigmatomma_sl"
#retain rest of genus with type of Labidus for now
seqs$Genus_Richness[seqs$Taxon %in% "Labidus_spininodis"]<-"Labidus_spininodis"
master.ext$Genus_Richness[master.ext$Taxon %in% "Labidus_spininodis"]<-"Labidus_spininodis"
#the only two Calomyrmex species sequenced (including the type, Calomyrmex laevissimus) are in Camponotus
#the only Echinopla species sequenced so far is in Camponotus (but is not type of Echinopla)
seqs$Genus_Richness[seqs$Genus %in% c("Calomyrmex","Echinopla","Camponotus")]<-"Camponotus_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Calomyrmex","Echinopla","Camponotus")]<-"Camponotus_sl"
#Colobopsis_vitrea occurs in Camponotus, but rest of Colobopsis falls outside
seqs$Genus_Richness[seqs$Taxon %in% "Colobopsis_vitrea"]<-"Camponotus_sl"
master.ext$Genus_Richness[master.ext$Taxon %in% "Colobopsis_vitrea"]<-"Camponotus_sl"
#the only Rossomyrmex species sequenced (not the type) falls within Cataglyphis
#seqs$Genus_Richness[seqs$Taxon %in% "Rossomyrmex_anatolicus"]<-"Cataglyphis_sl"
seqs$Genus_Richness[seqs$Genus %in% c("Cataglyphis","Rossomyrmex")]<-"Cataglyphis_sl"
#master.ext$Genus_Richness[master.ext$Taxon %in% "Rossomyrmex_anatolicus"]<-"Cataglyphis_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Cataglyphis","Rossomyrmex")]<-"Cataglyphis_sl"
#Prenolepis_emmae falls within Nylanderia, but remainder (including type) is outside
seqs$Genus_Richness[seqs$Taxon %in% "Prenolepis_emmae"]<-"Nylanderia"
master.ext$Genus_Richness[master.ext$Taxon %in% "Prenolepis_emmae"]<-"Nylanderia"
#Acanthoponera and Heteroponera are mixed together
seqs$Genus_Richness[seqs$Genus %in% c("Acanthoponera","Heteroponera")]<-"Acanthoponera_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Acanthoponera","Heteroponera")]<-"Acanthoponera_sl"
#multiple Attini genera group together; alternative to sort out Acromyrmex and Trachymyrmex.
seqs$Genus_Richness[seqs$Genus %in% c("Atta","Acromyrmex","Trachymyrmex","Mycetagroicus","Sericomyrmex")]<-"Atta_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Atta","Acromyrmex","Trachymyrmex","Mycetagroicus","Sericomyrmex")]<-"Atta_sl"
#Talaridris embedded within Eurhopalothrix
seqs$Genus_Richness[seqs$Genus %in% "Talaridris"]<-"Eurhopalothrix"
master.ext$Genus_Richness[master.ext$Genus %in% "Talaridris"]<-"Eurhopalothrix"
#Propodilobus embedded within Lordomyrma; ?also include Ancyridris???
seqs$Genus_Richness[seqs$Genus %in% "Propodilobus"]<-"Lordomyrma"
master.ext$Genus_Richness[master.ext$Genus %in% "Propodilobus"]<-"Lordomyrma"
#only Strongylognathus species sequenced is in Tetramorium and it is the type
seqs$Genus_Richness[seqs$Genus %in% c("Strongylognathus","Tetramorium")]<-"Tetramorium_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Strongylognathus","Tetramorium")]<-"Tetramorium_sl"
#Monomorium not monophyletic, so include a number of other genera...Monomorium comes out in 3 groups - difficult to bin species into these individual clades, but would allow preservation of smaller groups.
#seqs$Genus_Richness[seqs$Genus %in% c("Monomorium","Myrmicaria","Syllophopsis","Baracidris","Adelomyrmex","Cryptomyrmex","Tyrannomyrmex","Epelysidris","Solenopsis","Tropidomyrmex","Kempfidris","Austromorium","Oxyepoecus")]<-"Solenopsis_sl"
#added Anillomyrma, as I'd forgotten initially.
seqs$Genus_Richness[seqs$Genus %in% c("Monomorium","Myrmicaria","Syllophopsis","Baracidris","Adelomyrmex","Cryptomyrmex","Tyrannomyrmex","Epelysidris","Solenopsis","Tropidomyrmex","Kempfidris","Austromorium","Oxyepoecus","Anillomyrma")]<-"Solenopsis_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Monomorium","Myrmicaria","Syllophopsis","Baracidris","Adelomyrmex","Cryptomyrmex","Tyrannomyrmex","Epelysidris","Solenopsis","Tropidomyrmex","Kempfidris","Austromorium","Oxyepoecus","Anillomyrma")]<-"Solenopsis_sl"
#Rogeria comes out in two places and type is not sequenced...simplest solution is to merge with Solenopsis sl...actually, this conflicts with what done for dating (Monomorium is mrca of Monomorium species)...solution is to split Rogeria in two, since neither is the type and neither is in the Rogeria curvipubens (type) species group of Kugler (1994).  This is a roundabout way of assigning half the species to nevadensis and half to stigmatica.
seqs$Genus_Richness[seqs$Taxon %in% "Rogeria_nevadensis"]<-"Rogeria_nevadensis"
master.ext$Genus_Richness[master.ext$Taxon %in% "Rogeria_nevadensis"]<-"Rogeria_nevadensis"
master.ext$Genus_Richness[master.ext$Genus %in% "Rogeria"][1:19]<-"Rogeria_nevadensis"
seqs$Genus_Richness[seqs$Taxon %in% "Rogeria_stigmatica"]<-"Rogeria_stigmatica"
master.ext$Genus_Richness[master.ext$Taxon %in% "Rogeria_stigmatica"]<-"Rogeria_stigmatica"
master.ext$Genus_Richness[master.ext$Genus %in% "Rogeria"][c(20:26,28:33,35:40)]<-"Rogeria_stigmatica"
#Aphaenogaster not monophyletic
seqs$Genus_Richness[seqs$Genus %in% c("Aphaenogaster","Messor","Oxyopomyrmex","Goniomma")]<-"Aphaenogaster_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Aphaenogaster","Messor","Oxyopomyrmex","Goniomma")]<-"Aphaenogaster_sl"
#Streblognathsu embedded in Mesoponera...Streblognathus is older name
seqs$Genus_Richness[seqs$Genus %in% c("Streblognathus","Mesoponera")]<-"Streblognathus_sl"
master.ext$Genus_Richness[master.ext$Genus %in% c("Streblognathus","Mesoponera")]<-"Streblognathus_sl"

#check names were all spelled right
stuff<-unique(seqs[seqs$Genus!=seqs$Genus_Richness,c("Genus","Genus_Richness")])


rich<-as.data.frame(table(master.ext$Genus_Richness))
colnames(rich)<-c("Genus","Richness")
rich$Sampled<-0
rich$SampledProp<-0


rich.seqs<-as.data.frame(table(seqs$Genus_Richness))
colnames(rich.seqs)<-c("Genus","Richness")

for(k in 1: nrow(rich.seqs)){
	rich$Sampled[rich$Genus %in% rich.seqs$Genus[k]]<-rich.seqs$Richness[k]
}

rich$SampledProp<-round(rich$Sampled/rich$Richness,2)
global.samp.frac<-round(sum(rich$Sampled)/sum(rich$Richness),2)
gen.samp.frac<-round(sum(rich$Sampled==0)/sum(!is.na(rich$Sampled)),2)

seqs$SampledProp<-NA
for(p in 1:nrow(rich)){
	seqs$SampledProp[seqs$Genus_Richness %in% rich$Genus[p]]<-rich$SampledProp[p]
}

for.bamm<-seqs[,c("Taxon","Genus_Richness","SampledProp")]



write.csv(master.ext,file="~/Documents/ants_june_to_august_2016/ants.ncbi.14july2016/14july2016_antcat_aligns_fullalign_einsi_nogappy_wadds_heavilymodified_wo_results/antwiki_august2016/AntWiki_Valid_Species_w_fossils_mymods_EXTANT_w_bammgen10nov2016.csv",row.names=FALSE)
write.csv(seqs,file="~/Desktop/14july2016_antcat_aligns_fullalign_einsi_nogappy_wadds_heavilymodified_correctedmacse/Hymenoptera/drop_bads_again/combined.updated.updated.pruned.wouts.badsremoved.badsremovedagain.nopheidrectispina_w_bammgen10nov2016.csv",row.names=FALSE)
write.csv(rich,file="~/Desktop/14july2016_antcat_aligns_fullalign_einsi_nogappy_wadds_heavilymodified_correctedmacse/Hymenoptera/drop_bads_again/combined.updated.updated.pruned.wouts.badsremoved.badsremovedagain.nopheidrectispina_w_bammgen_GENUSONLY10nov2016.csv",row.names=FALSE)
write.table(for.bamm,file="~/Desktop/14july2016_antcat_aligns_fullalign_einsi_nogappy_wadds_heavilymodified_correctedmacse/Hymenoptera/drop_bads_again/for.bamm.10nov2016.txt",row.names=FALSE,col.names=FALSE,quote=FALSE,sep="\t")

#then add in global.samp.frac to top of for.bamm.txt and make new file called for.bamm.w.global.txt
#round(sum(rich$Sampled)/sum(rich$Richness),2)

#actually...this should be the proportion of taxa not already accounted for - see earlier conversation w rabosky
round(1-(sum(rich$Richness[rich$Sampled==0])/sum(rich$Richness)),2)
