Re: I can't find my mistake here!!!
by AppleFritter (Vicar) on Jul 02, 2014 at 11:07 UTC
|
Do you see anything wrong in the syntax???
Many things.
- You've got two opening parentheses in if(($sequence=~/ABC/), but only one closing one.
- You've not populated $sequence.
- You've not populated %hash_seqs_all.
- You've not populated $infile.
- You've not populated $id.
- You've not populated $protein.
- You're not checking for errors after opening your output file.
I'd love to try to help you, as I'm sure my brothers here in the Monastery would, but you've not given us nearly enough to work with. We'd need the following, ideally:
- Complete, syntactically correct code; a minimal test case that demonstrates the problem you're having.
- Example data.
- Expected output.
- Actual output.
As it is, we've got a code snippet that doesn't even compile, run on data we haven't seen, producing results we can only guess at. This isn't enough.
Here are some further links to get you started:
Please help us help you!
| [reply] |
Re: I can't find my mistake here!!!
by Corion (Patriarch) on Jul 02, 2014 at 10:26 UTC
|
Most likely, your $sequence never matches ABC. Alternatively, maybe opening the file failed. Use
open OUT, '>', "$infile.OUTFILE"
or die "Couldn't create file '$infile.OUTFILE': $!";
Also, consider printing both to STDOUT and to OUT, just to debug that what you think should be printed to the file really gets printed. | [reply] [d/l] [select] |
Re: I can't find my mistake here!!!
by baxy77bax (Deacon) on Jul 02, 2014 at 10:33 UTC
|
if(($sequence=~/ABC/) ?????
I guess two (( is a typing mistake. but that implies this is not copy-past of your code....
$sequence= "ABC";
open (OUT, ">OUTFILE");
if($sequence=~/ABC/)
{
print OUT "blas\n";
}
close (OUT);
this works, so the error must be somewhere else. are you sure you are getting inside the if block ? | [reply] [d/l] [select] |
Re: I can't find my mistake here!!!
by jellisii2 (Hermit) on Jul 02, 2014 at 11:24 UTC
|
Tangential: PerlCritic balks at 2 arg opens. My understanding if the current best practice is open(my $file, '>', "$infile.OUTFILE"); | [reply] [d/l] [select] |
|
|
Hi guys!
Many thanks for all the posts!
My problem is actually down to why this:
print ">$protein\n$respective_seq\n";
works, and I see the results on my screen (so the if block is executed normally, while this:
print OUT ">$protein\n$respective_seq\n";
does not work! The file IS created, but nothing is printed inside...I tried also with open OUT, '>', "outfile"; but no luck!
I don't get a warning either...
I am so confused, I can't make sense of it... | [reply] [d/l] [select] |
Re: I can't find my mistake here!!!
by choroba (Cardinal) on Jul 02, 2014 at 11:08 UTC
|
What's $protein? Where does it get populated?
| [reply] |
Re: I can't find my mistake here!!!
by perlfan (Parson) on Jul 02, 2014 at 11:54 UTC
|
- use strict;
- use warnings;
- Unmatched parans in if (($sequence
- Use the "3 argument" open with a scalar filehandle (no bareword).
- You don't need ">" in the print statement, this would be more clear to you if you used 3 argument open.
- You can skip the parens in close.
- If you are only writing in the case of /ABC/, just open and close inside of the if statement.
| [reply] [d/l] [select] |
|
|
You don't need ">" in the print statement
You do, if your output is in the FASTA format.
| [reply] [d/l] |
|
|
Ah, tricky. I was just trying to cover all of my bases (pun intended). =)
| [reply] |
|
|
Ok, something came up:
It seems to work, if I do the open, within the if block... Any ideas why? But in that case it's not helpful because later on I will have to open it again, and all this open file it not time-efficient, right?
| [reply] [d/l] [select] |
|
|
|
|
Re: I can't find my mistake here!!!
by Laurent_R (Canon) on Jul 02, 2014 at 21:31 UTC
|
Unless I missed something, nobody has pointed out that you are opening your output files probably multiple times (maybe million times) within a probable read loop (you did not show enough of your code for us to figure out exactly what your are doing) and that, each time you do that, you overwrite the content gathered the previous file version. So that, at the end, you get in the file what gathered for the last input line, which might be nothing if /ABC/ does not match. Whereas if you print to the screen, you get the full output.
You might have solved the issue with another technique, but I think that if you showed us more of your code, we could give you a much better solution, probably based on the idea of opening the output file before entering the read loop.
| [reply] |