Skip to content

Commit 326773e

Browse files
committed
Add utility scripts
1 parent aad8641 commit 326773e

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

bin/biom_convert

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
infile="$1"
4+
5+
biom convert -i "$infile" -o "$infile".tsv --to-tsv
6+
7+
awk 'BEGIN { FS="\t"; OFS="\t" }
8+
{
9+
if (NR == 2)
10+
{
11+
$1 = "name";
12+
print;
13+
}
14+
else if (NR >= 3)
15+
{
16+
print;
17+
}
18+
}' "$infile".tsv > "$infile".tsv.iroki.txt

bin/biom_tsv_to_iroki

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
infile="$1"
4+
5+
awk 'BEGIN { FS="\t"; OFS="\t" }
6+
{
7+
if (NR == 2)
8+
{
9+
$1 = "name";
10+
print;
11+
}
12+
else if (NR >= 3)
13+
{
14+
print;
15+
}
16+
}' "$infile"

bin/combine_biom_files.rb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env ruby
2+
3+
Signal.trap("PIPE", "EXIT")
4+
5+
require "abort_if"
6+
7+
include AbortIf
8+
9+
require "trollop"
10+
11+
opts = Trollop.options do
12+
banner <<-EOS
13+
14+
Combine multiple biom files, e.g., the output from running
15+
multiple_rarefactions.py.
16+
17+
Weird things may happen like not every biom file has every OTU or
18+
not every biom file has every sample (due to the rarefaction), but
19+
this will just count up everything.
20+
21+
Options:
22+
EOS
23+
24+
opt(:biom, "Biom files", type: :strings)
25+
opt(:method, "Method to combine values", default: "mean")
26+
end
27+
28+
VALID_METHODS = %w[mean]
29+
30+
abort_if !opts[:biom] || opts[:biom].empty?,
31+
"--biom is a required option"
32+
33+
opts[:biom].each do |fname|
34+
abort_unless_file_exists fname
35+
end
36+
37+
abort_unless VALID_METHODS.include?(opts[:method]),
38+
"--method must be one of #{VALID_METHODS.join(', ')}. Got: #{opts[:method]}"
39+
40+
num_biom_files = opts[:biom].count
41+
all_samples = []
42+
otus = {}
43+
44+
opts[:biom].each do |fname|
45+
46+
cidx2sample = {}
47+
48+
idx = 0
49+
File.open(fname, "rt").each_line do |line|
50+
line.chomp!
51+
unless line == "# Constructed from biom file"
52+
ary = line.split "\t"
53+
54+
this_otu = nil
55+
56+
if idx.zero?
57+
ary.each_with_index do |sample, cidx|
58+
# the first one should be name
59+
unless cidx == 0
60+
all_samples << sample
61+
cidx2sample[cidx] = sample
62+
end
63+
end
64+
else
65+
ary.each_with_index do |item, cidx|
66+
# these are the data rows
67+
if cidx == 0
68+
this_otu = item
69+
70+
unless otus.has_key? this_otu
71+
otus[this_otu] = Hash.new 0
72+
end
73+
else
74+
count = item
75+
sample = cidx2sample[cidx]
76+
otus[this_otu][sample] += count.to_f
77+
end
78+
end
79+
end
80+
81+
idx += 1
82+
end
83+
end
84+
end
85+
86+
all_samples.uniq!
87+
88+
puts ["name", all_samples].join "\t"
89+
90+
otus.each do |otu, counts|
91+
count_str = all_samples.map do |sample|
92+
if opts[:method] == "mean"
93+
counts[sample] / num_biom_files.to_f
94+
else
95+
counts[sample]
96+
end
97+
end.join "\t"
98+
99+
puts [otu, count_str].join "\t"
100+
end

bin/log_transform

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
awk 'BEGIN {FS="\t"; OFS="\t"}
4+
{
5+
if (NR == 1) {
6+
print;
7+
}
8+
else {
9+
for (i = 2; i <= NF; ++i) {
10+
if ($i <= 1) {
11+
$i = 0;
12+
}
13+
else {
14+
$i = log($i);
15+
}
16+
}
17+
{
18+
print;
19+
}
20+
}
21+
}' "$1" > "${1%.*}".log_transform.txt

0 commit comments

Comments
 (0)