Created
April 9, 2018 07:37
-
-
Save jaudoux/363de58468220e0be15533552d1df0dc to your computer and use it in GitHub Desktop.
Comptute jaccard index from a k-mer abundance matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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