Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some bray curtis example
<?php
$a = [
'a' => [
'a1' => 2,
'a2' => 0
],
'b' => [
'b1' => 2,
'b2' => 5
],
];
$b = [
'a' => [
'a1' => 1,
'a2' => 0
],
'b' => [
'b1' => 2,
'b2' => 1
],
];
$c = [
'a' => [
'a1' => 0,
'a2' => 2
],
'b' => [
'b1' => 2,
'b2' => 2
],
];
function reduce_phylum($a) {
$r = [];
foreach ( $a as $phylum => $otus ) {
$r[$phylum] = 0;
foreach ( $otus as $value ) {
$r[$phylum] += $value;
}
}
return $r;
}
function reduce_otu($a) {
$r = [];
foreach ( $a as $otus )
foreach ( $otus as $otu => $value ) {
$r[$otu] = $value;
}
return $r;
}
function sum_lesser($a, $b) {
$sum = 0;
foreach ( $a as $key => $value ) {
if ($value > 0 && isset($b[$key]) && $b[$key] > 0 ) {
$sum += min($value, $b[$key]);
}
}
return $sum;
}
function dist($a, $b) {
$s_a = array_sum($a);
$s_b = array_sum($b);
return 2 * sum_lesser($a, $b) / ($s_a + $s_b);
}
printf("AB\t\tBC\t\tAC\n");
$a_otu = reduce_otu($a);
$b_otu = reduce_otu($b);
$c_otu = reduce_otu($c);
printf("%f\t%f\t%f\n", dist($a_otu, $b_otu), dist($b_otu, $c_otu), dist($c_otu, $a_otu));
$a_p = reduce_phylum($a);
$b_p = reduce_phylum($b);
$c_p = reduce_phylum($c);
printf("%f\t%f\t%f\n", dist($a_p, $b_p), dist($b_p, $c_p), dist($c_p, $a_p));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.