Hello all – Congratulations on making it through assignment #1! Lest you be bored, please find Assignment #2 here: MCB_5472_Assignment_2 (Note: updated to correct error discovered in class)
Today’s lecture can be found here: MCB5472_Assignment_2_lecture_Jan-29-14. There are no specifically assigned text readings, but as always you are encouraged to use any resource that you have to do the assignment. As we are starting into more complicated code this week, I especially encourage you to post specific questions here. When you do so, please include an exact copy of your code so that your colleagues in class, Peter and Jonathan can help you troubleshoot.
Happy coding!
Hi guys, just wanted to share about a very confusing bug that I made and finally fixed:
If you are using a for loop on an array, make sure you separate the three controlling parts of the loops with semicolons and not commas. For example, this is correct:
for ($count = 40; $count > 100; $count = $count + 1) {
}
While this will actually run and not give you errors, but will give you terribly confusing results:
for ($count = 40, $count > 100, $count = $count + 1) {
}
… It might just be me, maybe no one else will have such a problem, but I hate it when things will actually run and give me the wrong results!
Good advice Cera – I’ve done that one too! Another one to watch out for is normal and curly brackets – classic hiccups.
Thanks for sharing!
Jonathan
Hey guys,
So I am still stuck on question # 1 when it comes to writing the code. And since Jonathan so kindly told us to not waste our intellectual energy, I am posting my question! 🙂 This is my code:
#!/usr/bin/perl
open (INFILE, “sequence.fasta.fasta”);
open (OUTFILE1, “sumaira1.out”);
while ($line = ){if ($line eq “>”) {print $line;} else {print sumaira1.out $line;}}
I believe this translates into, read the file line by line, if the line matches “>” print it to the screen otherwise print it to the out file. However, terminal keeps telling me “syntax error at sumaira1.pl line 4, near “} else”
Execution of sumaira1.pl aborted due to compilation errors.”
Since I’ve tried all the possible combinations of the bracket, I think something is wrong with my code. Any help is appreciated!
My bad, some of my code got cut out, this is what I actually had:
#!/usr/bin/perl
open (INFILE, “sequence.fasta.fasta”);
open (OUTFILE1, “sumaira1.out”);
while ($line = ){if ($line eq “>”) {print $line;} else {print sumaira1.out $line;}}
Hi Sumaira! I think you mean to be saying is:
while ($line = ) { # because INFILE is an object that holds the file you opened
if $line =~ />/ { # because you’re wanting to use regular expressions here and so you have to use slashies instead of quotes
print $line; #Or possibly there’s a command “next;” I think you’re just trying to move past this line ’cause you’re not keeping it
}
else { # here’s what you want to do if the line isn’t a header
print OUTFILE $line; # Rather than printing to sumaira1.out, you need to direct the print output to the object OUTFILE which you already opened before
}
}
Ohhhhh, I see, our less-than greater-than brackets are interpreted as HTML here. I’m replacing ≶ with ≶ and > with > and hopefully that will work…
while ($line = <INFILE>) { # because INFILE is an object that holds the file you opened
if $line =~ />/ { # because you’re wanting to use regular expressions here and so you have to use slashies instead of quotes
print $line; #Or possibly there’s a command “next;” I think you’re just trying to move past this line ’cause you’re not keeping it
}
else { # here’s what you want to do if the line isn’t a header
print OUTFILE $line; # Rather than printing to sumaira1.out, you need to direct the print output to the object OUTFILE which you already opened before
}
}
(Sorry, replacing < with < — enough from me!)
Thank you so much Cera! I totally forgot about the regular expressions. I fixed the code and this is what it looks like:
#!/usr/bin/perl
open (INFILE, “sequence.fasta.fasta”);
open (OUTFILE, “sumaira1.out”);
while ($line = ) {
if ($line=~/>/) {
print $line;
}
else {print OUTFILE $line;
}
}
close OUTFILE;
If only it were that easy. I ran it on terminal and it gave me absolutely no results. My outfile is completely empty 🙁
Terminal isn’t giving me any errors, but it’s not giving me any results which is strange. I tried replacing />/ with />/. Still Nothing
Hi Sumaira – Check your statement opening the outfile. Remember that you need to declare your file as “>file.out” or “>>file.out” for it to open properly, specifying if you want Perl to overwrite or append to previous files with that file name. Also, Cera has the correct synax for reading through your input file line by line: while ($line = ){ etc…
Yup, I declared it as “>>sumaira1.out”. I ran it on terminal and it gave me no errors but outfile is still empty.
Sorry, it appears WordPress filters out things in angle brackets – check that your while statement looks like: while ($file =~ ){ (taking out the spaces and underscores – hopefully this shows up right now). One other naive question: there is something in your input file, correct? Else paste your code in again and I will take a look at what you have.
Trying again: while ($file =~ \){
Wow, wordpress really doesn’t like greater than and less than symbols together.
while ($file =~ [less than]INFILE[greater than]){
#!/usr/bin/perl
open (INFILE, “sequence.fasta.fasta”);
open (OUTFILE, “>>sumaira1.out”);
while ($line=){
if ($line =~/>/){
print $line;
}
else {print OUTFILE $line;
}
}
close OUTFILE;
(yes, I have checked my infile multiple times to make sure the sequence was actually there.)
I don’t know why, but the comment keeps deleting part of my code. The fourth line is suppose to say: while ($line=){
Apparently, typing it out doesn’t work either. You’ll just have to take it on faith that I am telling it to ready the infile line by line
I had the same problem with the brackets.
You’re not going to like this answer, but your code runs for me once I fixed what WordPress messed up (like the input operator) and using my test fasta file. Feel free to email me your input file and .pl script in a zipped folder and I will take a look.
Another naive suggestion: try deleting your output file(s) and rerunning your script. I once thought I had an error when I was just looking at the wrong output file.
I GOT IT!!!!!!! I didn’t have to change the operator or anything. But I moved my fasta in to my home directory and it worked! I didn’t know the infile had to be in my home directory? Sorry guys…
and thank you for everyone’s help!
Hi Sumaira,
I think you were pretty close…
#/usr/bin/perl
open (INFILE, “sequence.fasta.fasta”); #you already had this correctly
open (OUTFILE, “>sumaira1.out”);
{while ($line = )
{if ($line !~ />/) { #the “!~” is something I looked up online and I think its used to say “not equal to”… so it will print every line not starting with ~
print OUTFILE $line;
}}}
close OUTFILE
So all you really had to change was the !~ I think… I just ran this and it worked for me… it took me forever to figure it out too
Great! I’ll add this for those who follow, and because Sumaira you weren’t the first to bring me a problem like this: remember that all of file path issues that we talked about last week still apply when coded in a perl script. This is one of the reasons why hard-coding files into scripts is avoided in favor of @ARGV etc. In the latter you can move things around with fewer consequences, assuming that you compensate in your input file path.
Srinath-Reddi’s suggestion of “!~” is a good one. Perl has lots of these “opposite” operators: “!=” vs “==” and “ne” vs “eq” are others. There is also “unless” vs “if”. Rule of thumb: if it seems logical to you, perl will often have something similar.
So I started working on the 2nd part of the assignment and I am having trouble figuring out how to remove white spaces in the fast file…
#/usr/bin/perl
open (INFILE, “srinath1.out”);
open (OUTFILE, “>srinath2.out”);
{while ($line = ) {
@array = split “”, $line; {
for ($count = scalar @array; $count >= 0; $count = $count – 1){ print OUTFILE $array[$count], “”;
}}}
close OUTFILE}
What I currently have prints to an out file but the output reverses each line individually…
so if I had
CATGG
GGTTT
the output would say
GGTAC
TTTGG
I think this issue would be solved if I removed white spaces but Im not sure
Hi Srinath-Reddi – Trying not to give away the answer completely, I’ll pose this question: thinking of the problem as a whole, what is the information that you want to reverse compliment? How does that compare to the strings that you were reverse complimenting in that version of your script? I will say that your problem right now is that you have wrongly combined two logical steps into one.