-
Notifications
You must be signed in to change notification settings - Fork 6
/
clustify.R
202 lines (167 loc) · 3.91 KB
/
clustify.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# clustify.R
# This script generates gene clusters given two files, a cutoff
# value, and a +/- sign signifying whether we want to accept
# values above or below the cutoff, respectively.
#
# Usage
# Rscript clustify.R [gff file] [hit values file] [cutoff] [+|-] [hole size] [min distance]
#
# [gff file]
# A GFF3 formated file the matches the Names found in the hit
# values file.
#
# [hit values file]
# A tab delimited, 2 column file with each row containing:
# Gene ID in the first column
# Numerical value in the second column
#
# [cutoff]
# A numerical cutoff value to use for the second column
#
# [1/-1]
# A numerical value of 1 or -1. 1 Implies to get anything above the cutoff
# while -1 implies to get anything below the cutoff
#
# [hole size]
# A numeric value to specify the number of invalid (not meeting
# the cutoff) genes to allow between two valid (meeting the
# cutoff) genes.
#
# [min distance]
# Select the minimum distance between genes allowed within a
# cluster. If set to a value below 0 (-1 for example), this
# option will not be used.
library(methods)
library(hash)
getGeneName = function(attrString) {
attrs = c()
name = ""
ID = ""
attrVect = unlist(strsplit(attrString, ';'))
for(i in 1:length(attrVect)) {
attr = unlist(strsplit(attrVect[i], '='))
if(attr[1] == "Name") {
name = attr[2]
} else if(attr[1] == "ID") {
ID = ""
}
}
if(name != "") {
return(name)
} else {
return(ID)
}
}
getHits = function(hitF, getU, cut) {
hitT = read.table(hitF, sep = "\t", comment.char = "")
hitH = hash()
for(i in 1:nrow(hitT)) {
gene = toString(hitT[i,1])
val = as.numeric(hitT[i,2])
if(getU && val > as.numeric(cut)){
hitH[[gene]] = 1
} else if((!getU) && val < as.numeric(cut)) {
hitH[[gene]] = 1
}
}
return(hitH)
}
printClusters = function(gffF, hitH, holeSz, mDist) {
gffT = read.table(gffF, sep = "\t")
holeCount = 0;
currClust = c()
currHole = c()
currChrom = ""
prevEnd = -1
prevChr = ""
for(i in 1:nrow(gffT)) {
if(toString(gffT[i,3]) != "gene") {
next
}
chr = gffT[i, 1]
start = gffT[i, 4]
end = gffT[i, 5]
if(prevChr == "") {
prevChr = chr
}
if(prevEnd < 0) {
prevEnd = start
}
chrom = toString(gffT[i, 1])
if(chrom == "") {
next
}
if(chrom != currChrom) {
printCluster(currClust)
currClust = c()
currChrom = chrom
currHole = c()
holeCount = 0
prevEnd = -1
}
gene = getGeneName(toString(gffT[i,9]))
if(mDist > 0 && (prevEnd - start) > mDist) {
printCluster(currClust)
currClust = c()
currHole = c()
holeCount = 0
prevEnd = -1
}
if(toString(hitH[[gene]]) == 1) {
if(length(currClust) == 0) {
currHole = c()
}
valGene = paste("*", gene, "*", sep = "")
currClust = c(currClust, currHole, valGene)
currHole = c()
holeCount = 0
} else {
if(length(currClust) == 0) {
prevEnd = -1
}
holeCount = holeCount + 1
currHole = c(currHole, gene)
if(holeCount > holeSz) {
printCluster(currClust)
currClust = c()
currHole = c()
holeCount = 0
prevEnd = -1
}
}
}
}
printCluster = function(cluster) {
if(length(cluster) < 2) {
return()
}
for(i in cluster) {
cat(i, "\t", sep = "")
}
cat("\n")
}
argv = commandArgs(trailingOnly = TRUE)
if(length(argv) != 6) {
cat("Rscript clustify.R [gff file] [hit values file] [cutoff] [+|-] [hole size] [min distance]\n", file = stderr())
stop()
}
gffF = argv[1]
hitF = argv[2]
cut = as.numeric(argv[3])
getU = FALSE
if(argv[4] == "+") {
getU = TRUE
} else if(argv[4] == "-") {
getU = FALSE
} else {
cat("[+/-] not set appropriately, must either be '+' or '-'\n", file = stderr())
stop()
}
holeSz = as.numeric(argv[5])
mDist = as.numeric(argv[6])
cat("Applying cutoff...", file = stderr())
hitH = getHits(hitF, getU, cut)
cat("Done\n", file = stderr())
cat("Printing Clusters...", file = stderr())
printClusters(gffF, hitH, holeSz, mDist)
cat("Done\n", file = stderr())