forked from angieyen/ChromDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc_perc.awk
More file actions
152 lines (133 loc) · 3.88 KB
/
calc_perc.awk
File metadata and controls
152 lines (133 loc) · 3.88 KB
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
#!/bin/awk
# Copyright 2015 Angela Yen
# This file is part of ChromDiff.
# ChromDiff is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# ChromDiff is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with ChromDiff. If not, see <http://www.gnu.org/licenses/>.
BEGIN{
OFS="\t"
tab="\t"
ind=1
genenames_file="genes/" genelabel "/genenames.txt"
while(getline < genenames_file) {
genename[ind]=$1
ind+=1
}
currgeneind=1
countsfile="perc/" model "/numsonly/" celltype "_counts.txt"
}
## moved on to new gene, so print out results for old gene and save new
($5!=currgeneid) {
if(currgeneid!="") {
## increment through all genenames that had no associated calls (they didn't show up in intersection file)
## keep printing NAs for these genes
while(currgeneid!=genename[currgeneind]) {
## print "NA" for each state that we have
state=1
printf "%s\t", genename[currgeneind]
while(state<=maxstate) {
printf "NA\t"
print "0" > countsfile
state+=1
}
print ""
## increment because we have printed another row
currgeneind+=1
}
currstate=1
## print each row as geneid covstate1 covstate2 covstate3.... where each column is percent coverage of that gene with the given state
printf "%s\t", currgeneid
while(currstate<=maxstate) {
if(currstate in statecounts) {
entry=statecounts[currstate]/total
} else {
entry=0
}
printf "%f\t", entry
print total > countsfile
currstate+=1
}
print ""
currgeneind+=1
}
## save gene id
## reset total
## reset statecounts array
currgeneid=$5
total=0
delete statecounts
}
## save information on this line
($5==currgeneid){
## calc total length in this gene for this state (geneid in currgeneid and $5, current state in $9)
split($9, elts, "_")
state=elts[1]
newval=($8-$7)
## take care of cases where ChromHMM region goes beyond gene location
if ($7<$2) {
newval-=($2-$7)
}
if ($8>$3) {
newval-=($8-$3)
}
statecounts[state]+=newval
total+=newval
}
END{
if(currgeneid!="") {
## increment through all genenames that had no associated calls (they didn't show up in intersection file)
## keep printing NAs for these genes
while(currgeneid!=genename[currgeneind]) {
## print "NA" for each state that we have
printf "%s\t", genename[currgeneind]
state=1
while(state<=maxstate) {
printf "NA\t"
print "0" > countsfile
state+=1
}
print ""
## increment because we have printed another row
currgeneind+=1
}
currstate=1
## print each row as covstate1 covstate2 covstate3.... where each column is percent coverage of that gene with the given state
## currgeneid has geneid if necessary: printf "%s\t", currgeneid
printf "%s\t", currgeneid
while(currstate<=maxstate) {
if(currstate in statecounts) {
entry=statecounts[currstate]/total
} else {
entry=0
}
printf "%f\t", entry
print total > countsfile
currstate+=1
}
print ""
currgeneind+=1
}
## we have no more saved information about any genes from input file, so just print "NA" for rest
## increment through all genenames that had no associated enhancer (they didn't show up in intersection file)
## keep printing NAs for these genes
while(currgeneind<=length(genename)) {
## print "NA" for each enhancer state that we have
printf "%s\t", genename[currgeneind]
state=1
while(state<=maxstate) {
printf "NA\t"
print "0" > countsfile
state+=1
}
print ""
## increment because we have printed another row
currgeneind+=1
}
}