-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_cluster_selection_palms.R
142 lines (130 loc) · 5.1 KB
/
1_cluster_selection_palms.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Identify best clusters
# LIBS
library(ggplot2)
library(phylotaR)
source(file.path('tools', 'selection_tools.R'))
# VARS
wd <- 'palms'
# INPUT
all_cls <- read_phylota(wd)
# RECREATE PhyLoTa TABLE
# get phylogenetically inform. clusters
n_taxa <- get_cl_slot(all_cls, cid = all_cls@cids, slt_nm = 'ntx')
cltyps <- get_cl_slot(all_cls, all_cls@cids, slt_nm = 'typ')
#picls <- cltyps %in% c('subtree', 'direct') & n_taxa >= 4
picls <- n_taxa >= 4
picls <- all_cls@cids[picls]
# build table
prnts <- get_cl_slot(all_cls, cid = picls, slt_nm = 'prnt')
n_taxa <- get_cl_slot(all_cls, cid = picls, slt_nm = 'ntx')
n_sqs <- get_cl_slot(all_cls, cid = picls, slt_nm = 'nsqs')
n_gnra <- sapply(picls, function(x) {
length(unique(get_txids(all_cls, cid = x, rnk = 'genus')))
})
mxsqlngs <- sapply(picls, function(x) {
max(get_sq_slot(all_cls, cid = x, slt_nm = 'nncltds'))
})
mnsqlngs <- sapply(picls, function(x) {
min(get_sq_slot(all_cls, cid = x, slt_nm = 'nncltds'))
})
lngst <- sapply(picls, function(x) {
sids <- all_cls[[x]]@sids
lngths <- get_sq_slot(all_cls, sid = sids, slt_nm = 'nncltds')
sids[which.max(lngths)]
})
deflns <- get_sq_slot(all_cls, sid = lngst, slt_nm = 'dfln')
mads <- sapply(picls, calc_mad, phylota = all_cls)
phylota_table <- data.frame('Cluster.ID' = picls, 'Parent' = prnts,
'TaxIDs' = n_taxa, 'GIs' = n_sqs,
'Genera' = n_gnra, 'L.min' = mnsqlngs,
'L.max' = mxsqlngs, 'MAD' = mads,
'Defline.of.longest.sequence' = deflns)
ordr <- order(phylota_table[['TaxIDs']], decreasing = TRUE)
phylota_table <- phylota_table[ordr, ]
write.csv(x = phylota_table, file = file.path('results', 'palms_phylota.csv'),
row.names = FALSE)
# CLTYPE STATS
# drop clusters of 10
n_taxa <- get_cl_slot(all_cls, cid=all_cls@cids, slt_nm='ntx')
keep <- names(n_taxa)[n_taxa > 10]
all_cls <- drop_cls(all_cls, keep)
table(sapply(all_cls@cls@cls, function(x) x@typ))
# PLOT
# tribe treemap
tribe_txids <- unique(get_txids(phylota=all_cls,
txids=all_cls@txids,
rnk='tribe'))
tribe_txids <- tribe_txids[tribe_txids != '']
txnms <- get_tx_slot(phylota=all_cls, txid=tribe_txids,
slt_nm='scnm')
txnms <- sort(txnms, decreasing=TRUE)
p <- plot_phylota_treemap(phylota=all_cls, txids=tribe_txids,
txnms=txnms, area='nsq', fill='ncl')
png(file.path('results', 'palms_tx_treemap.png'), width=2000, height=2000)
print(p + theme(legend.position='none'))
dev.off()
saveRDS(p, file.path('figures', 'palms_tx_treemap.RData'))
# cluster treemap
p <- plot_phylota_treemap(phylota=all_cls, cids=all_cls@cids,
area='nsq', fill='ntx')
png(file.path('results', 'palms_cl_treemap.png'), width=2000, height=2000)
print(p + theme(legend.position='none'))
dev.off()
saveRDS(p, file.path('figures', 'palms_cl_treemap.RData'))
# REDUCE TO TRIBE
tribe_only <- drop_by_rank(all_cls, rnk='tribe', n=2,
choose_by= c("pambgs", "age",
"nncltds"),
greatest = c(FALSE, FALSE,
TRUE))
# SUMMARISE AND FILTER
# count n genera per cid
n_tribes <- sapply(tribe_only@cids, function(x) {
length(unique(get_txids(phylota=tribe_only, cid=x, rnk='tribe')))
})
smmry <- summary(tribe_only)
smmry[['N_taxa']] <- n_tribes
smmry <- smmry[smmry[['MAD']] > 0.75, ]
smmry <- smmry[order(smmry$N_taxa, decreasing=TRUE), ]
# SELECT
slctd_smmry <- smmry[1:10, ]
slctd_smmry$ID <- as.numeric(slctd_smmry$ID)
slctd <- drop_cls(tribe_only, as.character(slctd_smmry$ID))
write.csv(slctd_smmry, file.path('results', 'best_clusters_palms.csv'))
# PLOT
tribes <- unique(get_txids(phylota=all_cls,
txids=all_cls@txids, rnk='tribe'))
tribes <- tribes[tribes != '']
txnms <- get_tx_slot(phylota=slctd, txid=tribes, slt_nm='scnm')
ordd <- order(txnms, decreasing=TRUE)
p <- plot_phylota_pa(phylota=slctd, cids=slctd@cids,
txids=tribes[ordd], txnms=txnms[ordd])
png(file.path('results', 'palms_cl_pamap.png'), width=2000, height=2000)
print(p)
dev.off()
saveRDS(p, file.path('figures', 'palms_cl_pamap.RData'))
# OUTPUT
# write out top 10 clusters with most taxa
sqfls <- list.files(wd, pattern='sequences[0-9]+.fasta')
rmFls(file.path(wd, sqfls))
for(i in seq_along(slctd@cids)) {
cid <- slctd@cids[i]
sids <- slctd@cls[[cid]]@sids
txids <- get_txids(slctd, cid=cid, rnk='tribe')
scnms <- get_tx_slot(slctd, txids, 'scnm')
n <- sapply(seq_along(scnms), function(x)
sum(scnms[x] == scnms[x:length(scnms)]))
sq_nm <- paste0(scnms, '_', n)
infile <- file.path(wd, paste0('sequences', i, '.fasta'))
write_sqs(phylota=slctd, outfile=infile, sid=sids,
sq_nm=sq_nm)
}
# ALIGN
alfls <- list.files(wd, pattern='alignment[0-9]+.fasta')
rmFls(file.path(wd, alfls))
for(i in seq_along(slctd@cids)) {
inpt <- file.path(wd, paste0('sequences', i,
'.fasta'))
otpt <- file.path(wd, paste0('alignment', i,'.fasta'))
system(paste0('mafft --auto ', inpt, ' > ', otpt))
}