Skip to content

Instantly share code, notes, and snippets.

@jaudoux
Created April 9, 2018 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaudoux/363de58468220e0be15533552d1df0dc to your computer and use it in GitHub Desktop.
Save jaudoux/363de58468220e0be15533552d1df0dc to your computer and use it in GitHub Desktop.
Comptute jaccard index from a k-mer abundance matrix
#! /usr/bin/perl
#
use strict;
use warnings;
my %jaccard_h;
while(<>) {
chomp;
my($k,@v) = split "\t", $_;
for(my $i = 0; $i < scalar @v; $i++) {
if($v[$i] == 0) {
for(my $j = $i+1; $j < scalar @v; $j++) {
my $key = $i.'@'.$j;
if($v[$j] == 0) {
next;
} else {
$jaccard_h{$key}{union}++;
}
}
} else {
for(my $j = $i+1; $j < scalar @v; $j++) {
my $key = $i.'@'.$j;
if($v[$j] == 0) {
$jaccard_h{$key}{union}++;
} else {
$jaccard_h{$key}{inter}++;
$jaccard_h{$key}{union}++;
}
}
}
}
}
print join("\t", "sample1", "sample2", "jaccard_index", "inter", "union"), "\n";
foreach my $key (keys %jaccard_h) {
my($sample1, $sample2) = split "@", $key;
print join("\t", $sample1, $sample2, $jaccard_h{$key}{inter} / $jaccard_h{$key}{union}, $jaccard_h{$key}{inter}, $jaccard_h{$key}{union}),"\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment