"No such file"打开一个目录下的多个文件时,只打开一个文件时不报错

"No such file" when opening multiple files in a directory, but no error when opening only one file

我可以打开目录中的一个文件和 运行 下面的代码。但是,当我尝试对一个目录中的多个文件使用相同的代码时,我收到关于没有文件的错误。

我已尝试确保我正确命名了文件,它们的格式正确,它们位于我当前的工作目录中,并且正确引用了这些内容。

我知道很多人以前都遇到过这个错误并发布了类似的问题,但我们将不胜感激。

工作代码:

#!/usr/bin/perl

use warnings;
use strict;
use diagnostics;

use List::Util qw( min max );

my $RawSequence = loadSequence("LDTest.fasta");
my $windowSize  = 38;
my $stepSize    = 1;
my %hash;
my $s1;
my $s2;
my $dist;

for ( my $windowStart = 0; $windowStart <= 140; $windowStart += $stepSize ) {

    my $s1 = substr( $$RawSequence, $windowStart, $windowSize );
    my $s2 = 'CGGAGCTTTACGAGCCGTAGCCCAAACAGTTAATGTAG';
            # the 28 nt forward primer after the barcode plus the first 10 nt of the mtDNA dequence

    my $dist = levdist( $s1, $s2 );

    $hash{$dist} = $s1;

    #print "Distance between '$s1' and '$s2' is $dist\n";

    sub levdist {
        my ( $seq1, $seq2 ) = (@_)[ 0, 1 ];

        my $l1 = length($s1);
        my $l2 = length($s2);
        my @s1 = split '', $seq1;
        my @s2 = split '', $seq2;
        my $distances;

        for ( my $i = 0; $i <= $l1; $i++ ) {
            $distances->[$i]->[0] = $i;
        }

        for ( my $j = 0; $j <= $l2; $j++ ) {
            $distances->[0]->[$j] = $j;
        }

        for ( my $i = 1; $i <= $l1; $i++ ) {

            for ( my $j = 1; $j <= $l2; $j++ ) {
                my $cost;

                if ( $s1[ $i - 1 ] eq $s2[ $j - 1 ] ) {
                    $cost = 0;
                }
                else {
                    $cost = 1;
                }

                $distances->[$i]->[$j] = minimum(
                    $distances->[ $i - 1 ]->[ $j - 1 ] + $cost,
                    $distances->[$i]->[ $j - 1 ] + 1,
                    $distances->[ $i - 1 ]->[$j] + 1,
                );
            }
        }

        my $min_distance = $distances->[$l1]->[$l2];

        for ( my $i = 0; $i <= $l1; $i++ ) {
            $min_distance = minimum( $min_distance, $distances->[$i]->[$l2] );
        }

        for ( my $j = 0; $j <= $l2; $j++ ) {
            $min_distance = minimum( $min_distance, $distances->[$l1]->[$j] );
        }

        return $min_distance;
    }
}

sub minimum {
    my $min = shift @_;

    foreach (@_) {
        if ( $_ < $min ) {
            $min = $_;
        }
    }

    return $min;
}

sub loadSequence {
    my ($sequenceFile) = @_;
    my $sequence = "";

    unless ( open( FASTA, "<", $sequenceFile ) ) {
        die $!;
    }

    while (<FASTA>) {
        my $line = $_;
        chomp($line);

        if ( $line !~ /^>/ ) {
            $sequence .= $line;    #if the line doesn't start with > it is the sequence
        }
    }

    return $sequence;
}

my @keys = sort { $a <=> $b } keys %hash;
my $BestMatch = $hash{ keys [0] };

if ( $keys[0] < 8 ) {
    $$RawSequence =~ s/\Q$BestMatch\E/CGGAGCTTTACGAGCCGTAGCCCAAACAGTTAATGTAG/g;
    print ">|Forward|Distance_of_Best_Match: $keys[0] |Sequence_of_Best_Match: $BestMatch", "\n",
            "$$RawSequence", "\n";
}

这是我的 non-working 代码的简化版本。没有改变的东西我没有包括在内:

Headers 和全局变量:

my $dir          = ("/Users/roblogan/Documents/FakeFastaFiles");
my @ArrayofFiles = glob "$dir/*.fasta";

foreach my $file ( @ArrayofFiles ) {

    open( my $Opened, $file ) or die "can't open file: $!";

    while ( my $OpenedFile = <$Opened> ) {

        my $RawSequence = loadSequence($OpenedFile);

        for ( ... ) {

            ...;

            print
                    ">|Forward|Distance_of_Best_Match: $keys[0] |Sequence_of_Best_Match: $BestMatch",
                    "\n", "$$RawSequence", "\n";
        }
    }
}

准确的错误是:

Uncaught exception from user code:
        No such file or directory at ./levenshtein_for_directory.pl line 93, <$Opened> line 1.
    main::loadSequence('{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470\x{a}') called at ./levenshtein_for_directory.pl line 22 

第 93 行:

     89 sub loadSequence{
     90         my ($sequenceFile) = @_;
     91         my $sequence = "";
     92         unless (open(FASTA, "<", $sequenceFile)){
     93                 die $!;
     94         } 

第 22 行:

     18         foreach my $file ( @ArrayofFiles ) {
     19             open (my $Opened, $file) or die "can't open file: $!";
     20             while (my $OpenedFile = <$Opened>) {
     21 
     22                 my $RawSequence = loadSequence($OpenedFile);
     23 

您似乎试图打开文件两次。行

my @ArrayofFiles = glob "$dir/*.fasta";

为您提供文件列表。那么

foreach my $file (@ArrayofFiles){
    open (my $Opened, $file) or die "can't open file: $!"; 
    while (my $OpenedFile = <$Opened>) { 
        my $RawSequence = loadSequence($OpenedFile); 
        # ...

逐行执行以下操作。它遍历文件,打开每个文件,从中读取一行,然后将 提交给函数 loadSequence().

但是,在该函数中您尝试再次打开文件

sub loadSequence{
    my ($sequenceFile) = @_;
    my $sequence = "";
    unless (open(FASTA, "<", $sequenceFile)){
    # ...

函数中的 $sequenceFile 变量作为 $OpenedFile 传递给函数——这是文件中已经打开并正在读取的一行,而不是文件名。虽然我不确定您的代码细节,但您显示的错误似乎与此一致。

您可能混淆了提供文件列表的 globopendir,后者确实需要跟随 readdir 才能访问文件。 尝试将 $OpenedFile 重命名为 $line(它是),然后看看它看起来如何。

我刚知道"FASTA file"是个固定期限。没有意识到这一点,之前认为它们是 一些 文件并包含文件名或其他内容。正如@zdim 已经说过的,你打开了这些文件两次。

以下代码获取 FASTA 文件列表(仅文件名),然后使用每个这样的文件名调用 loadSequence。该子例程然后打开给定的文件,将 none-^> 行连接到一个大行和 returns 它。

# input:  the NAME of a FASTA file
# return: all sequences in that file as one very long string
sub loadSequence
{
    my ($fasta_filename) = @_;
    my $sequence = "";
    open( my $fasta_fh, '<', $fasta_filename ) or die "Cannot open $fasta_filename: $!\n";
    while ( my $line = <$fasta_fh> ) {
        chomp($line);
        if ( $line !~ /^>/ ) {
            $sequence .= $line;    #if the line doesn't start with > it is the sequence
        }
    }
    close($fasta_fh);
    return $sequence;
}

# ...

my $dir = '/Users/roblogan/Documents/FakeFastaFiles';
my @ArrayofFiles = glob "$dir/*.fasta";
foreach my $filename (@ArrayofFiles) {
    my $RawSequence = loadSequence($filename); 
    # ...
}