click on the Biowiki logo to go to homepage
Edit Raw Print
Links Diffs RSS
About Stats Recent


Research Teaching Blog
Fall09 | Sandbox
Biowiki > Teaching > Undergraduate Class > RevComp

Search

Advanced search...

Topics

PageRank Checker

[back to UndergraduateClass]

Remember that there are many ways to write a program in Perl, so this is just one possible way to write it

Reverse complement a FASTA file.

  • (a) Write a Perl program to do the following:
    • Open a file, whose filename is specified by the user as a single command-line argument. That is, if the name of your Perl script is programname and the name of the file is filename, then the script should be run by typing the following at the Unix commandline perl programname filename or, alternatively (if you have made programname an executable file whose first line is #!/usr/bin/perl), by typing programname filename
    • Read the contents of this file, assuming it is a FASTA file of DNA sequences;
    • Print the reverse-complement of every sequence on the standard output, in FASTA format.

    #! /usr/bin/perl
    
    open FILE, $ARGV[0];
    while (<FILE>) {
            if (/^>/) {
                    if (defined($seq)) {
                            $rev = reverse(uc($seq));
                            $rev =~ tr/ACTG/TGAC/;
                            print "$rev\n";
                            $seq = "";
                    }
                    print;
            } else {
                    chomp;
                    $seq .= $_;
            }
    }
    $rev = reverse(uc($seq));
    $rev =~ tr/ACTG/TGAC/;
    print "$rev\n";
    

  • (b) Make a comprehensive list of errors that could occur at runtime, when this program is used.
    • wrong # of arguments from command line
    • file does not exist/problems with opening the file
    • file is not a FASTA file
    • file does not contain DNA sequences

  • (c) For at least two of these errors, modify the code to guard against the errors and issue informative warnings if they are encountered.

    #! /usr/bin/perl
    
    if (@ARGV != 1) {    # corrects error #1
       die "Please call this program with a file name\n";
    }
    
    open FILE, $ARGV[0] or die "there was a problem opening the file\n";   # corrects error #2
    while (<FILE>) {
            if (/^>/) {
                    if (defined($seq)) {
                            $rev = reverse(uc($seq));
                            $rev =~ tr/ACTG/TGAC/;
                            print "$rev\n";
                            $seq = "";
                    }
                    print;
            } else {
                    chomp;
                    $seq .= $_;
            }
    }
    $rev = reverse(uc($seq));
    $rev =~ tr/ACTG/TGAC/;
    print "$rev\n";
    

-- AngiChau - 05 Oct 2005

Actions: Edit | Attach | New | Ref-By | Printable view | Raw view | Normal view | See diffs | Help | More...