Select a random position in a string Choose a random nucleotide
7.3.1.1 Select a random position in a string
How can you randomly select a position in a string? Recall that the built-in function length returns the length of a string. Also recall that positions in strings are numbered from to length-1 , just like positions in arrays. So you can use the same general idea as in Example 7-1 , and make a subroutine: randomposition A subroutine to randomly select a position in a string. WARNING: make sure you call srand to seed the random number generator before you call this function. sub randomposition { mystring = _; This expression returns a random number between 0 and length-1, which is how the positions in a string are numbered in Perl. return intrandlengthstring; } randomposition is really a short function, if you dont count the comments. Its just like the idea in Example 7-1 to select a random array element. Of course, if you were really writing this code, youd make a little test to see if your subroutine worked: usrbinperl -w Test the randomposition subroutine my dna = AACCGTTAATGGGCATCGATGCTATGCGAGCT; srandtime|; for my i=0 ; i 20 ; ++i { print randompositiondna, ; } print \n; exit; IT-SC 145 sub randomposition { mystring = _; return int rand length string; } Heres some representative output of the test your results should vary: 28 26 20 1 29 7 1 27 2 24 8 1 23 7 13 14 2 12 13 27 Notice the new look of the for loop: for my i=0 ; i 20 ; ++i { This shows how you can localize the counter variables in this case, i to the loop by declaring them with my inside the for loop.7.3.1.2 Choose a random nucleotide
Next, lets write a subroutine that randomly chooses one of the four nucleotides: randomnucleotide A subroutine to randomly select a nucleotide WARNING: make sure you call srand to seed the random number generator before you call this function. sub randomnucleotide { mynucs = _; scalar returns the size of an array. The elements of the array are numbered 0 to size-1 return nucs[rand nucs]; } Again, this subroutine is short and sweet. Most useful subroutines are; although writing a short subroutine is no guarantee it will be useful. In fact, youll see in a bit how you can improve this one. Lets test this one too: usrbinperl -w Test the randomnucleotide subroutine my nucleotides = A, C, G, T; srandtime|; for my i=0 ; i 20 ; ++i { IT-SC 146 print randomnucleotidenucleotides, ; } print \n; exit; sub randomnucleotide { mynucs = _; return nucs[rand nucs]; } Heres some typical output its random, of course, so theres a high probability your output will differ: C A A A A T T T T T A C A C T A A G G G7.3.1.3 Place a random nucleotide into a random position
Parts
» OReilly.Beginning.Perl For Bioinformatics
» The Organization of Proteins
» In Silico Biology and Computer Science
» A Low and Long Learning Curve
» Ease of Programming Rapid Prototyping
» Portability, Speed, and Program Maintenance
» Perl May Already Be Installed No Internet Access?
» Downloading Binary Versus Source Code
» Unix and Linux Macintosh Windows
» Unix or Linux How to Run Perl Programs
» Text Editors Getting Started with Perl
» Finding Help Getting Started with Perl
» Saves and Backups Error Messages
» Individual Approaches to Programming Programming Strategies
» The Design Phase The Programming Process
» Algorithms The Programming Process
» Pseudocode and Code Comments
» Representing Sequence Data Sequences and Strings
» Control Flow Comments Revisited Command Interpretation
» Assignment Print Exit Statements
» Concatenating DNA Fragments Sequences and Strings
» Using the Perl Documentation
» Calculating the Reverse Complement in Perl
» Proteins, Files, and Arrays Reading Proteins in Files
» Arrays Sequences and Strings
» Scalar and List Context Exercises
» Conditional tests and matching braces
» Code Layout Motifs and Loops
» Getting User Input from the Keyboard Turning Arrays into Scalars with join
» Regular expressions and character classes
» Counting Nucleotides Motifs and Loops
» Exploding Strings into Arrays
» Operating on Strings Motifs and Loops
» Writing to Files Motifs and Loops
» Advantages of Subroutines Subroutines
» Arguments Scoping and Subroutines
» Scoping Scoping and Subroutines
» Command-Line Arguments and Arrays
» Subroutines: Pass by Value Subroutines: Pass by Reference
» Modules and Libraries of Subroutines
» use warnings; and use strict; Fixing Bugs with Comments and Print Statements
» How to start and stop the debugger Debugger command summary
» Stepping through statements with the debugger
» Setting breakpoints The Perl Debugger
» Fixing another bug use warnings; and use strict; redux
» Exercises Subroutines and Bugs
» Random Number Generators Mutations and Randomization
» Seeding the Random Number Generator Control Flow
» Making a Sentence Randomly Selecting an Element of an Array
» Formatting A Program Using Randomization
» Select a random position in a string Choose a random nucleotide
» Improving the Design Combining the Subroutines to Simulate Mutation
» Exercises Mutations and Randomization
» A Gene Expression Database Gene Expression Data Using Unsorted Arrays
» Gene Expression Data Using Sorted Arrays and Binary Search
» Gene Expression Data Using Hashes
» Translating Codons to Amino Acids
» The Redundancy of the Genetic Code
» Using Hashes for the Genetic Code
» Translating DNA into Proteins
» FASTA Format Reading DNA from Files in FASTA Format
» A Design to Read FASTA Files
» A Subroutine to Read FASTA Files
» Writing Formatted Sequence Data
» Regular Expressions Restriction Maps and Regular Expressions
» Background Planning the Program
» Restriction Enzyme Data Restriction Maps and Restriction Enzymes
» Logical Operators and the Range Operator
» Finding the Restriction Sites
» Exercises Restriction Maps and Regular Expressions
» Using Arrays Separating Sequence and Annotation
» Pattern modifiers Examples of pattern modifiers
» Separating annotations from sequence
» Using Arrays Parsing Annotations
» When to Use Regular Expressions
» Main Program Parsing Annotations
» Parsing Annotations at the Top Level
» Features Parsing the FEATURES Table
» Parsing Parsing the FEATURES Table
» DBM Essentials Indexing GenBank with DBM
» Overview of PDB Protein Data Bank
» Opening Directories Files and Folders
» Processing Many Files Files and Folders
» Extracting Primary Sequence Parsing PDB Files
» Finding Atomic Coordinates Parsing PDB Files
» The Stride Secondary Structure Predictor
» Parsing Stride Output Controlling Other Programs
» String Matching and Homology
» Extracting Annotation and Alignments
» Parsing BLAST Alignments Parsing BLAST Output
» The printf Function Presenting Data
» here Documents format and write
» Bioperl Tutorial Script Bioperl
» The Art of Program Design Web Programming
» Algorithms and Sequence Alignment Object-Oriented Programming Complex Data Structures
Show more