Skip to content

Commit

Permalink
fixing a bug for when a user gives spaces in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
lskatz committed Aug 28, 2023
1 parent 6268a5c commit b74ae20
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
7 changes: 4 additions & 3 deletions bin/mashtree
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ sub mashSketch{
logmsg "WARNING: $fastq is a zero byte file. Skipping.";
next;
} else {
my $sketchCommand="mash sketch -S $$settings{seed} -k $$settings{kmerlength} -s $$settings{'sketch-size'} $sketchXopts -o $outPrefix $fastq 2>&1";
my $sketchCommand="mash sketch -S $$settings{seed} -k $$settings{kmerlength} -s $$settings{'sketch-size'} $sketchXopts -o \Q$outPrefix\E \Q$fastq\E 2>&1";
my $stdout = `$sketchCommand`;
if ($?){
logmsg "ERROR running $sketchCommand!\n $stdout";
Expand Down Expand Up @@ -421,8 +421,9 @@ sub mashDist{
for my $msh(@$mshArr){
#my $outfile="$outdir/".basename($msh).".tsv";
logmsg "Distances for $msh";
my @distRes =`mash dist -t $msh -l $mshList`;
die "ERROR with 'mash dist -t $msh -l $mshList'" if $?;
my $cmd = "mash dist -t \Q$msh\E -l $mshList";
my @distRes = `$cmd`;
die "ERROR with $cmd" if $?;
chomp(@distRes);

my $query="";
Expand Down
2 changes: 1 addition & 1 deletion lib/Mashtree.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Used to mark whether a file is being read, so that Mashtree limits disk I/O
######
# CONSTANTS

our $VERSION = "1.4.3";
our $VERSION = "1.4.4";
our $MASHTREE_VERSION=$VERSION;
our @fastqExt=qw(.fastq.gz .fastq .fq .fq.gz);
our @fastaExt=qw(.fasta .fna .faa .mfa .fas .fsa .fa);
Expand Down
7 changes: 6 additions & 1 deletion lib/Mashtree/Db.pm
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,12 @@ sub toString_phylip{

$str.=(" " x 4) . "$numGenomes\n";
for(my $i=0;$i<$numGenomes;$i++){
$str.=$name[$i];

# sanitize sample names by removing spaces
my $sanitizedName = $name[$i];
$sanitizedName =~ s/\s/_/g;

$str.=$sanitizedName;
$str.=" " x ($maxGenomeLength - length($name[$i]) + 2);
my $distanceHash = $dist{$name[$i]};

Expand Down
30 changes: 29 additions & 1 deletion t/02_lambda.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,43 @@ use FindBin qw/$RealBin/;

use lib "$RealBin/../lib";
use File::Basename qw/dirname basename/;
use File::Temp qw/tempdir/;
use File::Copy qw/cp/;
use Digest::MD5 qw/md5_hex/;

use Test::More tests => 6;
use Test::More tests => 7;

use_ok 'Mashtree';
use Mashtree qw/treeDist mashDist raw_mash_distance mashHashes/;

$ENV{PATH}="./bin:$ENV{PATH}";

my $tempdir = tempdir(basename($0).".XXXXXX", TMP=>1, CLEANUP=>1);

subtest 'space in filename' => sub {
my $wd = "$tempdir/spaces";
mkdir $wd;
my @target;
for my $filename(glob("$RealBin/lambda/*.fastq.gz")){
my $target = "$wd/".basename($filename);
$target =~ s/sample(\d)/sample $1/; # add in a space for funsies
cp($filename, $target) or die "ERROR: could not copy $filename to $target: $!";

push(@target, $target);
}

# e.g., '02_lambda.tmp.tiF_bn/spaces/sample 1.fastq.gz' '02_lambda.tmp.tiF_bn/spaces/sample 2.fastq.gz' '02_lambda.tmp.tiF_bn/spaces/sample 3.fastq.gz' '02_lambda.tmp.tiF_bn/spaces/sample 4.fastq.gz'
my $targets = "'" . join("' '", @target) . "'";
my $cmd = "mashtree --outmatrix lambdadist.tsv --genomesize 40000 --numcpus 1 $targets 2>$0.log";
note $cmd;
system($cmd);
my $exit_code = $? >> 8;
if($exit_code){
note `cat $0.log`;
}
is($exit_code, 0, "Ran mashtree with exit code $exit_code");
};

my $correctMashtree="(sample3:0.00195,sample4:0.00205,(sample1:0.00205,sample2:0.00205):0.00010);";
$correctMashtree=~s/(\d+\.)(\d+)/$1 . substr($2,0,4)/ge; # global and expression

Expand Down

0 comments on commit b74ae20

Please sign in to comment.