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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
## apetools.R (2018-06-13)
## APE Tools
## Copyright 2017-2018 Emmanuel Paradis
## This file is part of the R-package `ape'.
## See the file ../COPYING for licensing issues.
.file.extensions <-
list(clustal = "aln", fasta = c("fas", "fasta", "fa"),
fastq = c("fq", "fastq"), newick = c("nwk", "newick", "tre", "tree"),
nexus = c("nex", "nexus"), phylip = "phy")
Xplorefiles <- function(from = "HOME", recursive = TRUE, ignore.case = TRUE)
{
if (from == "HOME") from <- Sys.getenv("HOME")
FILES <- list.files(path = from, recursive = recursive, full.names = TRUE)
ext <- if (exists(".file.extensions", envir = .PlotPhyloEnv))
get(".file.extensions", envir = .PlotPhyloEnv)
else .file.extensions
res <- vector("list", length(ext))
names(res) <- names(ext)
for (i in seq_along(res)) {
e <- paste0("\\.", ext[[i]], "$")
if (length(e) > 1) e <- paste(e, collapse = "|")
x <- grep(e, FILES, ignore.case = ignore.case, value = TRUE)
res[[i]] <- data.frame(File = x, Size = file.size(x),
stringsAsFactors = FALSE)
}
res
}
editFileExtensions <- function()
{
foo <- function(x) {
n <- length(x)
if (n < m) x[(n + 1):m] <- NA
x
}
res <- if (exists(".file.extensions", envir = .PlotPhyloEnv))
get(".file.extensions", envir = .PlotPhyloEnv)
else .file.extensions
m <- max(lengths(res, FALSE))
res <- lapply(res, foo)
res <- as.data.frame(res, stringsAsFactors = FALSE)
res <- edit(res)
res <- lapply(res, function(x) x[!is.na(x)])
assign(".file.extensions", res, envir = .PlotPhyloEnv)
}
bydir <- function(x)
{
nofile <- which(sapply(x, nrow) == 0)
if (length(nofile)) x <- x[-nofile]
if (!length(x)) {
cat("No file\n")
return(invisible(NULL))
}
for (i in seq_along(x)) x[[i]]$Type <- names(x)[i]
x <- do.call(rbind, x)
x <- x[order(x$File), ]
SPLIT <- strsplit(x$File, "/")
LL <- lengths(SPLIT)
foo <- function(i, PATH) {
K <- grep(paste0("^", PATH, "/"), x$File)
sel <- intersect(K, which(LL == i + 1L))
if (length(sel)) {
y <- x[sel, ]
y$File <- gsub(".*/", "", y$File)
cat("\n", PATH, "/\n", sep = "")
print(y, row.names = FALSE)
}
if (length(sel) < length(K)) {
d <- setdiff(K, sel)
subdir <- unlist(lapply(SPLIT[d], "[", i + 1L))
for (z in unique(subdir))
foo(i + 1L, paste(PATH, z, sep = "/"))
}
}
top <- unlist(lapply(SPLIT, "[", 1L))
for (z in unique(top)) foo(1L, z)
}
Xplor <- function(from = "HOME")
{
ext <- if (exists(".file.extensions", envir = .PlotPhyloEnv))
get(".file.extensions", envir = .PlotPhyloEnv)
else .file.extensions
OUT <- paste0(tempfile(), ".html")
mycat <- function(...) cat(..., sep = "", file = OUT, append = TRUE)
FILES <- Xplorefiles(from = from)
filetypes <- names(FILES)
## nb of files of each type:
NR <- sapply(FILES, nrow)
## HTML header
mycat('<html><title>Files Sorted by Type</title></head>')
## build the TOC
mycat('<h2>File types searched:</h2>')
mycat('<TABLE border=0>')
mycat('<tr><th align="center"> Type </th><th align="center"> Number of files </th><th align="center"> Extensions* </th></tr>')
for (type in filetypes) {
mycat('<tr><td><a href=#', type, '>', type, '</a></td><td align="center">', NR[type],
'</td><td align="center">', paste(paste0(".", ext[[type]]), collapse = " "), '</td></tr>')
}
mycat('</TABLE>')
mycat('<br>*Case-independent<br>To change the files extensions, type in R: <font face = "courier">editFileExtensions()</font><br>')
if (all(NR == 0)) {
browseURL(OUT)
return(invisible(NULL))
}
OUTBYDIR <- paste0(tempfile(), ".html")
sink(OUTBYDIR)
cat('<html><title>Files Sorted by Directory</title></head>')
.bydir.html(FILES)
cat('</html>')
sink(NULL)
mycat('<br><a target="blank" href=', OUTBYDIR, '><h2>Files sorted by directory (in new tab)</h2></a><br>')
for (type in filetypes) {
nr <- NR[type]
mycat('<a name=', type, '></a><h1>', toupper(type), '</h1>')
if (nr == 0) {
mycat('no file of this type')
next
}
DF <- FILES[[type]]
mycat('<TABLE border=1>')
mycat('<tr><th> File name </th><th align="center"> Size (KB) </th></tr>')
for (i in 1:nr)
mycat('<tr><td><a href="file://', DF[i, 1], '">', DF[i, 1],
'</a></td><td align="right">', round(DF[i, 2]/1000, 1), '</td></tr>')
mycat('</TABLE>')
}
mycat('</html>')
browseURL(OUT)
}
.bydir.html <- function(x)
{
nofile <- which(sapply(x, nrow) == 0)
if (length(nofile)) x <- x[-nofile]
if (!length(x)) return(NULL)
for (i in seq_along(x)) x[[i]]$Type <- names(x)[i]
x <- do.call(rbind, x)
x <- x[order(x$File), ]
SPLIT <- strsplit(x$File, "/")
LL <- lengths(SPLIT)
foo <- function(i, PATH) {
K <- grep(paste0("^", PATH, "/"), x$File)
sel <- intersect(K, which(LL == i + 1L))
if (length(sel)) {
y <- x[sel, ]
y$File <- gsub(".*/", "", y$File)
cat('<h2><a href="file://', PATH, '">', PATH, '/</a></h2>', sep = "")
cat('<TABLE border=1>')
cat('<tr><th> File </th><th align="center"> Size (KB) </th><th align="center"> Type </th></tr>')
for (i in 1:nrow(y))
cat('<tr><td><a href="file://', PATH, '/', y[i, 1], '">', y[i, 1],
'</a></td><td align="right">', round(y[i, 2]/1000, 1),
'</td><td align="right">', y[i, 3], '</td></tr>', sep = "")
cat('</TABLE><br>')
}
if (length(sel) < length(K)) {
d <- setdiff(K, sel)
subdir <- unlist(lapply(SPLIT[d], "[", i + 1L))
for (z in unique(subdir))
foo(i + 1L, paste(PATH, z, sep = "/"))
}
}
top <- unlist(lapply(SPLIT, "[", 1L))
for (z in unique(top)) foo(1L, z)
}
|