我可以打开目录中的一个文件并运行以下代码。但是,当我尝试在一个目录中的多个文件上使用相同的代码时,我得到一个关于没有文件的错误。
我已经尽力确保我的文件命名正确,格式正确,位于我当前的工作目录中,并且内容被正确引用。
我知道很多人以前都有过这个错误,并发布了类似的问题,但是任何帮助都会很感激。
工作代码:
#!/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 $distn";
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$BestMatchE/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('{rtf1ansiansicpg1252cocoartf1404cocoasubrtf470x{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
刚刚得知"FASTA file"是一个定语。我没有意识到这一点,以前认为它们是一些文件,包含文件名或其他东西。正如@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);
# ...
}
您似乎试图打开文件两次。行
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
重命名为,比如说,$line
(它是),看看它看起来如何。