Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

change files names bug

by Yuma248 (Initiate)
on Oct 30, 2013 at 09:40 UTC ( #1060305=perlquestion: print w/ replies, xml ) Need Help??
Yuma248 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to change the name of several files from some like "sample_ACCTGG.fq" to some like "LA01.fq" based in a file with the interchangeable names (ID.txt). This files is in a format where the separation between columns are tabs and looks like

LA01 ACCTGG LA02 ATTGGC LA03 ATCCCG

I wrote this scrip

#!/usr/bin/perl opendir(DIR, "/samplesA") or die "Can not open folder, $!\n"; @files = readdir(DIR); closedir (DIR); open (IN,"</ID.txt")or die "Can not open barcode file, $!\n"; @lines=(<IN>); close IN; foreach $file (@files) { if ($file =~ m/fq$/) { foreach $line (@lines) { my ($id, $barcode) = split(/\t/,$line); if ($file =~ m/$barcode/) { my $new = $file; $new =~ s/sample_$barcode/$id/; rename ($file, $new) or die "Can not change $file to $new, $!\ +n"; }}}}

it works for the first files, buy then die with the massage "Can not change sample_ACCTGG.fq to ACCTGG.fq". But when I print $new, it is "LA01.fq" that is correct.

Comment on change files names bug
Select or Download Code
Re: change files names bug
by choroba (Abbot) on Oct 30, 2013 at 09:45 UTC
    What does the $! contain? Are you chomping the @lines?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      which?

      $!

      Yep the lines are chomped in fact if you print $barcodes print the right string without new line

        You said works for the first files, buy then die with the massage "Can not change sample_ACCTGG.fq to ACCTGG.fq". But when I print $new, it is "LA01.fq" that is correct.

        but the code is  rename ($file, $new) or die "Can not change $file to $new, $!\n"; so the error message you quoted is incomplete, it doesn't state the reason it rename didn't work

        This reason is everything

Re: change files names bug
by builat (Scribe) on Oct 30, 2013 at 10:10 UTC
    And you sure that witespases is realy tabulation simbol? May be good idea is use regexp like this
    my @RegExp = /^([A-Za-z]{2}\d{2,})\s+(.*)$/g;

    So in $RegExp[0] will be ID and in $RegExp[1] will be BARCODE

      Yep I am sure the space between columns is tab, in fact when you print $id or $barcode you get the right strings inclusive if you print them just before the rename command

Re: change files names bug
by Random_Walk (Parson) on Oct 30, 2013 at 10:42 UTC

    Can you try this version and report its output.

    #!/usr/bin/perl use strict; use warnings; opendir(DIR, "/samplesA") or die "Can not open folder, $!\n"; my @files = readdir(DIR); closedir (DIR); open my $barcodes, "<", "/ID.txt" or die "Can not open barcode file, $ +!\n"; my @lines=(<$barcodes>); close $barcodes; for my $file (@files) { next unless $file =~ m/fq$/; foreach my $line (@lines) { chomp $line; my ($id, $barcode) = split(/\t/,$line); if ($file =~ m/$barcode/) { my $new = $file; $new =~ s/sample_$barcode/$id/; rename ($file, $new) or die "Can not change $file to $new, $! +\n"; } } }

    Perhaps strict and warnings will help

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Not, the strict and warning did not solve the problem, but I notice that when the folder contain few files (less than 20) the original scrip finish the job without problems. When the folder contain more files the original scrip stop working when it fund a file that does not have any barcode in the name. Any idea?

Re: change files names bug
by RichardK (Priest) on Oct 30, 2013 at 10:43 UTC

    readdir does NOT return the complete path, so 'ACCTGG.fg' is not the same file as 'samplesA/ACCTGG.fg' .

Re: change files names bug
by keszler (Priest) on Oct 30, 2013 at 10:45 UTC
    Does sample_ACCTGG.fq exist when the attempt to rename is made? Try
    if ( -e $file ) { rename ($file, $new) or die "Can not change $file to $new, $!\n"; } else { print "$file isn't there to be renamed $new\n"; }

      The file exist and the scrip actually changes the name correctly, but I dont understand why, the scrip try to change the name again and of course, because it just changed the name of the file, the file does not exit any more. However the validation that you propose

       if ( -e $file )

      allows the script finish the job, thank you

Re: change files names bug
by oiskuu (Friar) on Oct 30, 2013 at 10:46 UTC
    Test the s///.
    my $new = $file; if ($new =~ s/sample_$barcode/$id/) { rename ($file, $new) or die; }

      I tried it before, but it did not work. Thank you

Re: change files names bug
by Random_Walk (Parson) on Oct 30, 2013 at 11:03 UTC

    How about undoing some of those loops. If your file names are as consistent as they look from your example, this is a simpler way to do it

    #!/usr/bin/perl use strict; use warnings; # Read barcodes, and look for coresponding files to rename my $dir = "/samplesA"; my $bcf = "/ID.txt"; open my $barcodelist, "<", $bcf or die "Can not open barcode file $bcf +: $!\n"; while (my $line = <$barcodelist>) { chomp; my ($id, $barcode) = split(/\t/,$line); # I like validating inputs warn "bad line at $.\n" unless $barcode =~/^[ACGT]+$/; my $file = "$dir/sample_$barcode.fq"; if (-e $file) { my $new = "$dir/$id.fq"; rename $file, $new or die "Can't change $file to $new, $!\n"; } } close $barcodelist;

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Thank you a lot, looks like the validation

       if (-e $file)

      just makes the job

Re: change files names bug (no readdir ever, use Path::Tiny)
by Anonymous Monk on Oct 30, 2013 at 11:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1060305]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-25 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls