#!/usr/bin/perl use strict; use warnings; # get the file name from command line my $fname = $ARGV[0]; open FILE, $fname or die("$fname: $!\n"); #print the first line of the STDERR print STDERR "Filename: $fname\n"; #intake from the file my %sizes; while(my $l = <FILE>){ my @split_line = split(/\t/,$l); $sizes{$split_line[1]} = $split_line[2]; } # sum the contigs my $total_size = 0; foreach my $k (keys(%sizes)){ $total_size += $sizes{$k}; } #sort the contigs my @sorted_keys = reverse sort{$sizes{$a} <=> $sizes{$b}} keys %sizes; #find the n50 and print my $n50_index; my $n50_sum; for(my $i=0;$i<=scalar(@sorted_keys);$i++){ if($n50_sum < $total_size/2 && ($n50_sum + $sizes{$sorted_keys[$i]}) >= $total_size/2 ){ $n50_index = $i; print "The N50 for the contigs in $fname is ".$sizes{$sorted_keys[$i]}."\n"; } $n50_sum += $sizes{$sorted_keys[$i]}; } #print the contigs larger than the n50 foreach my $i (0 ... $n50_index){ print STDERR $sorted_keys[$i]."\t".$sizes{$sorted_keys[$i]}."\n"; }