Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: Listing of files using glob

by AG87 (Acolyte)
on Jan 05, 2012 at 08:24 UTC ( #946337=note: print w/replies, xml ) Need Help??

in reply to Re: Listing of files using glob
in thread Listing of files using glob

Here is the complete code. It basically takes a file...splits it and save the portions of file in separate files by creating a directory. Afterwards I want to list the individual file using glob. The output is still the same. I dont know where am wrong? The folders are being created, files are being created, the complete file name is not being listed only the extension is printed which is ".fasta".

#!/usr/bin/perl use Cwd; use strict; use warnings; my $path_to_fastaSeqs = $ARGV[0]; my $path_to_fragmentScript = $ARGV[1]; my %seqInfo = (); &storeInHash; &createFolderAndFiles; &callFragmentScript; sub storeInHash { open(FILE, "$path_to_fastaSeqs") or die("cannot open file"); { while(<FILE>) { my $line = $_; if ($line =~ />.*/) { #print "$&\n"; } else { #print "$line\n"; } $seqInfo{$&} = $line; } close(FILE); } } sub createFolderAndFiles { my $dir = getcwd; while(my ($key, $value) = each(%seqInfo)) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; mkdir $nameWithoutFastaSign, 0777; open(FILEOUT, ">$dir/$nameWithoutFastaSign/$nameWithoutFastaSign.fasta +"); { print FILEOUT "$key\n$value\n"; } } } sub callFragmentScript { while(my $key = each %seqInfo) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; my $currentDir = getcwd; chdir "$currentDir/$nameWithoutFastaSign"; my $changedDir = getcwd; print "changed directory is: $changedDir\n"; while(my $fastaFile = glob("*.fasta")) { print "$fastaFile\n"; } chdir "$currentDir"; } }

output is

changed directory is: /home/ammarah/Documents/code/1elwA .fasta changed directory is: /home/ammarah/Documents/code/1ghwA .fasta changed directory is: /home/ammarah/Documents/code/1flwA .fasta

Replies are listed 'Best First'.
Re^3: Listing of files using glob
by Anonymous Monk on Jan 05, 2012 at 08:55 UTC

    storeInHash seems problematic. I have seen a .fasta file, and it looks like  >gi|21040368|ref| ... , and on my platform there is no way to create a file/directory with a | character in the name, so what Platform/Filesystem are you on?

      I can't speak for AG87, but on Linux it's pretty easy to create a file with a pipe symbol in it.

      touch 'foo|bar' && ls -l foo* && rm -i 'foo|bar'

      If I recall correctly, the only character disallowed in Linux filenames is the slash.

        You also can't create a file with \0 in its name :).

      I am working on linux and it has no issues in creating a filename with "|" sign in it. I have figured out the problem. In actual the file created in sub createFoldersAndFiles automatically inserts a space before the extension .fasta due to which the complete file name is not being grabbed by glob. Can anyone suggest how to overcome this problem coz its very strange. Normally the FILEOUT handle does not add a space before extension. Any thoughts??? :(

        The handle does not create the filename. Your program does.
        open(FILEOUT, ">$dir/$nameWithoutFastaSign/$nameWithoutFastaSign.fasta +");
        Remove the space from $nameWithoutFastaSign.

      I cant see any space though

      Even the code (part of the previous complete code) below does not print the complete filename. It only prints the extension

      $nameWithoutFastaSign =~ tr/>//d; print "$nameWithoutFastaSign.fasta\n";

      Any more thoughts?? :(

        What is in $nameWithoutFastaSign before tr?

      Before tr it has ">1elwA". and tr is used to omit the ">" sign

        Strange. Works for me:
        perl -e '$nameWithoutFastaSign = ">1elwA"; print "before: $nameWithoutFastaSign\n"; $nameWithoutFastaSign =~ tr/>//d; print "after: $nameWithoutFastaSign\n";'
        before: >1elwA after: 1elwA

      A separate code does work for me as well. But in the full fledge code its not working. The file names ">1elwA" etc etc are stored in a hash as a key. In a loop it has to retrieve the key value, tr it, and then print the output with .fasta extension. In the embedded code it is not printing the complete file name. Maybe there is some problem in storing the file in hash. Is there some other way to capture the output of the regular expression into the key of the hash other than the way used in my code in the first sub with the name of "StoreInHash"????

        Are you chomping lines when storing names into the hash?
        Please, be careful to what post you are replying to.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946337]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-05-21 19:35 GMT
Find Nodes?
    Voting Booth?