in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Bioinformatics. A Practical Guide to the Analysis of Genes and Proteins - Baxevanis A.D.

Baxevanis A.D. Bioinformatics. A Practical Guide to the Analysis of Genes and Proteins - New York, 2001. - 493 p.
ISBN 0-471-22392-1
Download (direct link): bioinformaticsapractic2001.pdf
Previous << 1 .. 238 239 240 241 242 243 < 244 > 245 246 247 248 249 250 .. 251 >> Next

A second shortcut is to append an i flag at the end of the regular expression right after the second slash. This puts the regular expression into case-insensitive mode, and allows the statement to be written as
$length = $length + length if /л[GATCN]+ $/i;
Finally, because adding a value to a variable and storing the sum back into the same variable is such a common operation, Perl provides the shortcut operator += (read as ‘‘plus equals’’). This operator takes a numeric value on its right side, adds it to the contents of the variable on its left side, and stores the result back into the same variable, all in one graceful step. Taking advantage of this feature gives statements of the form
$length += length if /л [GATCN] + $/i;
Similar assignment shortcuts are summarized in Table 17.5. Putting all these shortcuts together gives the final version of the DNA length-tallying script:
# $length = 0;
$lines = 0; while (<>) { chomp;
$length += length if /л[GATCN]+ $/i;
$lines += 1;
print "LENGTH = $length\n"; print "LINES = $lines\n";
TABLE 17.5. Assignment Shortcut Operators
Operator Example Description
+ = $a += 3 Add a number to a variable
- = $a -= 3 Subtract a number from a variable
*= $a *= 10 Multiply variable by a number
/= $a /= 2 Divide a variable by a number
.= $txt .= "abc" Append a string to a variable
Not only are regular expressions good for detecting patterns in text, but they can be used to extract matching portions of the text as well. To see how this might be useful, consider a FASTA description line like this one:
>M18580 Clone 305A4, complete sequence
In addition to the initial >, the description line contains two different fields that we might like to capture. The first is “M18580,” the mandatory sequence ID. The second is ‘‘Clone 305A4, complete sequence,’’ an optional human-readable comment. In regular expression terms, the description line looks like this:
Reading from left to right is the beginning of the line (л), a > sign, one or more non-white-space characters corresponding to the sequence ID, zero or more spaces or other white space, zero or more of any character corresponding to the optional description, and the end of the line ($). This regular expression match can be made to extract the ID and description lines simply by putting parentheses around the parts that should be captured:
$id = $1;
$description = $2;
When a string successfully matches a regular expression, any portions of the expression that are contained within parentheses are extracted and placed into the automatic variables $1, $2, $3, and so forth. The extraction works from left to right and only happens if the entire regular expression matches.
This trick can be used to fix a deficiency in the DNA length calculator from the previous section. Previous versions of this script naively treated the entire FASTA file as a single DNA sequence. However, a FASTA file usually contains entries for multiple sequences. With pattern matching, the description lines can be identified and the sequence IDs extracted, allowing for the length of each sequence to be printed out separately.
$id = ''; # holds sequence ID of current sequence
$length =0; # holds length of current sequence
$total_length =0; # tallies aggregate length of all seqs
while (<>) { chomp;
if (^>(\S + )$/) { # found a new description line print "$id: $length\n" if $length > 0;
$id = $1;
$length = 0;
} else {
$length += length;
$total_length += length;
print "$id: $length\n" if $length > 0; # last entry print "TOTAL LENGTH = $total_length\n";
After initializing the three variables at the top of the program, the script enters a while loop. As before, it reads a line at a time into $_ and removes the terminating newline. The new feature is an if-else block. The block performs a pattern match on the line, looking for a FASTA description line. If one is found, it signals the beginning of a new sequence. The following then occurs:
• If $length is non-zero, the ID and length of the previous sequence are printed. The check on $length prevents the program from printing out an empty line when it hits the very first sequence in the file.
• The matched sequence ID is copied into $id.
• $length is set to zero.
Otherwise, the program is in the middle of a sequence, in which case the length of the current line is added to the appropriate counters. After the last line is read, the ID and length of the last sequence in the file are printed, as well as the total length of all the sequences:
% ests.fasta
D28205: 1105 BCD2 07F: 402 BCD2 07R: 332 BCD386F: 192 BCD386R: 362 CDO98F: 374 TOTAL LENGTH = 2767
Previous examples have worked with single-valued scalar variables only. However, Perl has the ability to work with multivalued variables as well. There are two basic multivalued variables, named arrays and hashes. An array is a list of data values indexed by number. A hash is a list of data values indexed by string. Both are very easy to use and incredibly handy. To understand arrays, consider how one might keep track of a large number of identifiers, such as clone names. With scalar variables, one approach could be to assign each clone name to a different variable:
Previous << 1 .. 238 239 240 241 242 243 < 244 > 245 246 247 248 249 250 .. 251 >> Next