-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgc.cr
42 lines (39 loc) · 1.2 KB
/
gc.cr
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
lines_chan = Channel(String).new(16)
# ------------------------------------------------------------------------------
# Loop over the input file in a separate fiber (and thread, if you set the
# CRYSTAL_WORKERS count to something larger than 1), and send its output on a
# channel
# ------------------------------------------------------------------------------
spawn do
gcfile = File.new("chry_multiplied.fa")
gcfile.each_line() do |line|
lines_chan.send(line)
end
lines_chan.close
gcfile.close
end
# ------------------------------------------------------------------------------
# Loop over the lines on the channel in the main thread, and count GC fraction.
# ------------------------------------------------------------------------------
at = 0
gc = 0
while line = lines_chan.receive?
if line.starts_with?('>')
next
end
line.each_byte() do |chr|
case chr
when 'A', 'T'
at += 1
next
when 'G', 'C'
gc += 1
next
end
end
end
# ------------------------------------------------------------------------------
# Output results
# ------------------------------------------------------------------------------
gcfrac = gc / (gc + at)
puts "GC fraction: #{gcfrac}"