forked from shpakoo/YAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileStats.R
executable file
·151 lines (107 loc) · 3.19 KB
/
FileStats.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
########################################################################################
## This file is a part of YAP package of scripts. https://github.com/shpakoo/YAP
## Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Copyright (c) 2011-2013 Sebastian Szpakowski
########################################################################################
library(reshape2)
# list directories
files = dir("./", pattern=glob2rx("*.fasta") , recursive=TRUE)
files = append(files, dir("./", pattern=glob2rx("*.mate1") , recursive=TRUE))
files = append(files, dir("./", pattern=glob2rx("*.mate2") , recursive=TRUE))
x = data.frame(file = files, stringsAsFactors=FALSE)
x$step = unlist(lapply(x$file, function(x)
{
tmp = unlist(strsplit(x, "/"))
tmp = unlist(strsplit(tmp[1],"_"))
tmp = tmp[-1]
tmp = tmp[-length(tmp)]
tmp = paste(tmp, sep="_", collapse="_")
tmp
} ))
x$filename = unlist(lapply(x$file, function(x)
{
tmp = unlist(strsplit(x, "/"))
tmp = tmp[length(tmp)]
return(tmp)
} ))
x$filetype = unlist(lapply(x$filename, function(x)
{
if (regexpr( "fasta.mate1",x)>-1)
{
return("fasta (mate1)")
}
else if (regexpr( "fasta.mate2",x)>-1)
{
return("fasta (mate2)")
}
else if (regexpr( "contig",x)>-1)
{
return("fasta (contigs)")
}
else if (regexpr("singletons",x )>-1)
{
return("fasta (singletons)")
}
else if (regexpr("assembled",x)>-1)
{
return("fasta (assembled)")
}
else if (regexpr("fasta",x)>-1)
{
return("fasta")
}
else if (regexpr("mate1",x)>-1)
{
return("fastq (mate1)")
}
else if (regexpr("mate2",x)>-1)
{
return("fastq (mate2)")
}
else
{
tmp = unlist(strsplit(x, "\\."))
tmp = tmp[length(tmp)]
tmp
}
} ))
x$id = unlist(lapply(x$filename, function(x)
{
tmp = unlist(strsplit(x, "\\."))
tmp = tmp[-length(tmp)]
tmp = tmp[-1]
tmp = tmp[1]
tmp
} ))
x$seqs = unlist(lapply(x$file, function(x)
{
if (regexpr("fasta", x)>-1)
{
tmp = system(paste("grep -c \"^>\" ", x, sep="" ), intern=TRUE, ignore.stderr=TRUE)
tmp = as.numeric(tmp)
}
else
{
tmp = system(paste("wc -l ", x, sep="" ), intern=TRUE, ignore.stderr=TRUE)
tmp = unlist(strsplit(tmp, " "))
tmp = as.numeric(tmp[1])
if (regexpr("mate", x)>-1)
{
tmp = tmp/4
}
}
return(tmp)
} ))
#x$wc = ifelse(x$filetype=="mate1", x$wc/4, x$wc)
#x$wc = ifelse(x$filetype=="mate2", x$wc/4, x$wc)
x$idtype = apply(x[,c("id", "filetype")], 1, function(x)
{
x = paste(x, sep="\t", collapse="\t")
x
})
y = aggregate(x$seqs, x[, c("idtype", "step")], sum)
names(y)[length(names(y))]=c("sum")
z = dcast(y, idtype ~ step)
names(z)[1] = "id\ttype"
z[is.na(z)]=""
write.table(z, "summary.dir.tab.txt", sep="\t", col.names=TRUE, row.names=FALSE, quote=FALSE )