Re: Writing to file...
by cdarke (Prior) on Jul 22, 2009 at 07:36 UTC
|
You might learn more if you posted your code. Here is my stab at it: use strict;
use warnings;
my $dir = '.';
for my $file (glob("$dir/*.seq")) {
my $output = "$file.out";
open (my $in, '<', $file) or
die "Unable to open $file: $!";
open (my $out, '>', $output) or
die "Unable to open $output: $!";
while (<$in>) {
s/\^\^/\n^^/g;
print $out $_
}
close ($in);
close ($out);
rename ($output, $file) or
die "Unable to rename $output to $file:$!"
}
I am sure others will have alternative ways of doing this. | [reply] [d/l] |
|
|
Very nicely indented and done. Just some minor points:
1. glob is not that portable (BSD,DOS,Windows,SYS V), it is a mess. I prefer readdir and grep over glob.
2. I prefer lining up the { and } brackets over the old style 'C' way.
3. The best thing I liked about your code was that it was nicely spaced out. Whitespace is one of the most important things to put into code! Yes, blank lines are important!
4. I may have some mistakes below, but I think it continues along your fine way.
use strict;
use warnings;
my $dir = '.';
# for my $file (glob("$dir/*.seq"))
# "glob" is not portable even amoungst *nix systems
# and certainly not amoungst Windows systems.
opendir(DIR,$dir)
or die "unable to open directory $dir:$!";
foreach my $file ( grep{/\.seq$/ && -f $dir/$_}readdir DIR)
{
my $output_path = "$dir/$file.out";
my $input_path = "$dir/$file";
open (my $in, '<', $input_path) or
die "Unable to open $input_path: $!";
open (my $out, '>', $output_path) or
die "Unable to open $output_path: $!";
while (<$in>)
{
s/\^\^/\n^^/g;
print $out $_; # oopps ; was missing...
}
close ($in);
close ($out);
unlink ($input_path)
or die "unable to unlink $input_path: $!";
rename ($output_path, $input_path) or
die "Unable to rename $output_path to $input_path:$!";
}
Update: the unlink before rename is not necessary.
| [reply] [d/l] |
|
|
1. I've never had problems with glob except where spaces appear in the pattern, and then I use File::Glob 'glob';. I did not here because I thought it an unnecessary complication. KISS. If it is that easy to produce a portable version using opendir/readdir/closedir then one wonders why glob is not fixed in the base.
2. I used to use BSD style braces until I discovered TheDamian's Perl Best Practices.
3. Whitespace? I thought everyone coded like that.
4. It is a good idea to do a closedir (I'm sure you knew that).
Update: improved format.
| [reply] [d/l] |
|
|
|
|
glob is not that portable (BSD,DOS,Windows,SYS V), it is a mess. I prefer readdir and grep over glob.
In what respect? As long as you are using forward slashes for your pathes (in particular important if you are on Windows and dealing with UNC pathes), the fine thing about glob is that it works the same on all platform, since it doesn't rely on platform specific issues. At least this is true for any "reasonably recent" Perl; I darkly remember that for very early versions of Perl, glob used the shell mechanism on some platforms, which led to compatibility problems ... but this was long time ago.
--
Ronald Fischer <ynnor@mm.st>
| [reply] [d/l] |
|
|
|
|
|
|
|
Line 9
foreach my $file ( grep{/\.seq$/ && -f $dir/$_}readdir DIR)
is giving me the error that the argument <filename> isn't numeric in division (/). Why is it saying this?
| [reply] [d/l] |
|
|
|
|
|
|
Also, if ^^ is not present I need to add it in with a new line character before the sequence.
Something like...
while (<$in>)
{
if $in =~ /^^/ {
s/\^\^/\n^^/g;
print $out $_;
else {
s/[a]|[c]|[t]|[g]{6}/^^\n
}
How would I correctly write the else?
I was thinking just find a series of a, c, t, or g's and add the ^^ before it but I don't know how to get the same series of bases in the substitution. Sorry I'm really bad at this lol | [reply] [d/l] |
|
|
|
|
Thanks for all the help so far guys!
One last question: I have this code trying to capture the locus name and place it after the ">" but nothing I do lets me capture it.
while (<$in>)
{
my $locus =~ /LOCUS\s+(\w+)/;
s/\^\^/>$locus\n^^\n/g;
print $out $_;
}
within the file the Locus name is formatted like this
Created: Tuesday, July 12, 2005 4:14 PM
LOCUS AJ877264 704 bp DNA linear INV 15
+-APR-2005
| [reply] [d/l] [select] |
|
|
Re: Writing to file...
by imrags (Monk) on Jul 22, 2009 at 07:48 UTC
|
Depends where you are failing...
If you are failing to browse thru the directory, then here's the code.
print_dir ( "YOUR/DIRECTORY/PATH" );
sub print_dir {
my ($dir_name) = @_;
opendir ( my $dir_h , "$dir_name") or die "Unable to open dir :$dir
+_name: $!\n";
while ( my $file = readdir($dir_h) ) {
next if ( "$dir_name/$file" =~ /\/\.$/ or "$dir_name/$file" =~ /\
+/\.\.$/ );
if ( -d "$dir_name/$file" ) {
print_dir ( "$dir_name/$file" );
}
print "$dir_name/$file - "."\n";
}
}
It'll browse thru directories/sub-directories as well.
If you are failing to replace the carats, then this will help to find carats in a string
my $string = "xyz^^ is strange";
print $string if($string =~ /\^\^/);
| [reply] [d/l] [select] |