I am trying to write a perl script to make a bunch of new perl scripts but as it is it won't allow me to use variable/arrays/etc in a print "".
The error I get back says I need to define them even though they are within in the print "". Is there away around this?
Below is my preliminary script:
#!/usr/bin/perl
use warnings;
use strict;
my $idfile = $ARGV[0];
open (IDFILE,'<',$idfile)
or die "Could not open $idfile \n";
my $outfile_name;
##CUT##
my $outfile = $outfile_name."pl"; # warning: undefined value in concatenation or string
open (OUTFILE, '>', $outfile)
or die "Could not open $outfile \n"; # this will make a single file named ".pl" with all code inside
##END CUT##
while (my $line = <IDFILE>) {
chomp ($line);
if ($line =~ /(T4-GC_[0-9]+)/) {
my $outfile_name = "Pull_".$line; # this masks previous definition of $outfile_name and has no effect
## I think you want to move ##CUT## - ##END CUT## here
my $script = "
#!/usr/bin/perl
use warnings;
use strict;
use Bio::SearchIO;
use Bio::SeqIO;
my @ARGV = glob("*.fa"); ## why use ARGV? it will work, but ARGV is a special variable containing the arguments
## Your search strategy is very inefficient! Given the relative numbers of identifiers in IDFILE and all entries in fa files,
## can you devise a more efficient search strategy than N*M? (This is your homework: Assume #IDFILE (N) < #All Fasta entries (M))
foreach my $fil (@ARGV) {
my $seqio = Bio::SeqIO->new(-format => 'fasta', -file => $fil);
while (my $seqobj = $seqio->next_seq) {
my $seqid = $seqobj->display_id;
$fil =~ /([A-Z]+[0-9]+)/;
my $phage_name = $1;
my $id = $seqid."|".$phage_name;
my $nuc = $seqobj->seq();
if ($seqid =~ /$line/) {
print ">$id\n$nuc\n";
}
}
}"
print OUTFILE $script;
## close OUTFILE forgotten.
}
}
off-topic, simple perl questions
Deletion needs to be reserved to posts that have absolutely nothing to do with bioinformatics. A question that involves bioperl or one that involves any script or program that is meant to analyze bioinformatics data is on topic.
It is possible that the solution to a question is not bioinformatics related that but that should not be grounds for deletion.
Then bring back the close option!
To me this question has nothing to with bioinformatics but is a good question for stack-overflow. If it had, any script could be made relevant by simply adding a line "import Bio::*";
true, there is really no way around that.
This is how I see it: whoever has an
import Bio
in their script is a budding bioinformatician. They are one of us, or want to be one of us. This site is written by bioinformaticians for bioinformaticians. Why not help , guide them in the right direction.I'd do just that, just please put the tools in place. The right response imo to such a question is to close it and to link to the numerous results that explain it.
yes I agree - we need to indicate that this question is really a programming question and the author will may get better answers on stackexchange. This would be a great answer to the question, would not even need to be a comment.
But we don't need to preclude someone else to also adding an extra answer. There may be something in the code that is not done properly or there may be an alternative that the OP is not even asking about. Bioinformatics is fundamentally more complicated than programming. The StackExchange binary model is not appropriate to science.
What I don't like about closing is the finality of someone deciding that there is no other opinion that could make this thread better today or in the future.
sorry, whoever undeleted this, if this is not off topic, what then?
there is a moderator log (that I will make more visible) that shows moderator actions: http://www.biostars.org/modlog/list/
also I think we should comment to tell people that they should be asking this elsewhere but I think deleting the question especially after it got an answer and upvote only leaves more people unhappy
the upvote for the answer is from me. in fact this is the result of the missing close option.
I have added some comments into your original perl code to show some more problems with the code.
Thanks! Sorry if it is a code question. I am new to bioinformatics.
Reading the code, it's clear that they are trying to solve a bioinformatics problem (matching a list of IDs to FASTA headers). It's just that the problem was not well-defined in the question.