"没有这样的文件"在目录中打开多个文件时,但只打开一个文件时没有错误

时间:2016-06-27 19:24:55

标签: perl file directory subroutine

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

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

我知道很多人之前都有此错误,并发布了类似的问题,但任何帮助都将不胜感激。

工作代码:

#!/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";
}

这是我的非工作代码的缩写版本。没有改变的事情我没有包括:

标题和全局:

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 

2 个答案:

答案 0 :(得分:2)

我刚学会了#34; FASTA文件&#34;是一个固定的术语。我们没有意识到这一点,以前认为它们是一些文件并包含文件名或其他内容。正如@zdim已经说过的那样,你要两次打开这些文件。

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

# 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); 
    # ...
}

答案 1 :(得分:1)

您似乎试图两次打开文件。这条线

<button class="button button1" onclick="hide">Hide</button> 
<button class="button button1" onclick="show">Show</button> 

为您提供文件列表。然后

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,它会为您提供文件列表,其中glob确实需要以下opendir才能访问这些文件。 尝试将readdir重命名为$OpenedFile(它是),然后查看它的外观。