logo资料库

Bioinformatics Programming Using Python.pdf

第1页 / 共524页
第2页 / 共524页
第3页 / 共524页
第4页 / 共524页
第5页 / 共524页
第6页 / 共524页
第7页 / 共524页
第8页 / 共524页
资料共524页,剩余部分请下载后查看
Table of Contents
Preface
Introduction
About This Book
About Bioinformatics
About the Reader
Python
Some Context
The Python Language
Installing Python
Running Python
Notes
Reading and Reference Recommendations
Example Code
Unfortunate and Unavoidable Vocabulary Overlap
Comments
Conventions Used in This Book
We╎d Like to Hear from You
Using Code Examples
Safari® Books Online
Acknowledgments
Chapter€1.€Primitives
Simple Values
Booleans
Integers
Floats
Strings
Expressions
Numeric Operators
Logical Operations
String Operations
String operators
Subscription
Slicing
Calls
Function calls
Method calls
Compound Expressions
Tips, Traps, and Tracebacks
Tips
Statements and expressions
Running Python interactively
Traps
Tracebacks
Chapter€2.€Names, Functions, and Modules
Assigning Names
Defining Functions
Function Parameters
Comments and Documentation
Assertions
Default Parameter Values
Using Modules
Importing
Module namespaces
The random module
Python Files
Tips, Traps, and Tracebacks
Tips
Names
Function definitions
Executing code
Using IDLE
Managing Python files
Traps
Tracebacks
Chapter€3.€Collections
Sets
Sequences
Strings, Bytes, and Bytearrays
Creating
Testing
Searching
Replacing
Changing case
Reformatting
The format method and function
Ranges
Tuples
Tuple syntax
Tuple packing and unpacking
Lists
Statements that modify lists
List modification methods
Sequence-oriented string methods
Mappings
Dictionaries
Dictionary example: RNA codon translation table
Dictionary operations
Dictionary methods
Streams
Files
Creating file objects
File methods
Example
Generators
Collection-Related Expression Features
Comprehensions
List comprehensions
Set and dictionary comprehensions
Generator expressions
Conditional comprehensions
Nested comprehensions
Functional Parameters
The parameter ╜key╚
Function objects
Anonymous functions
Tips, Traps, and Tracebacks
Tips
Language features
Developing and testing code
Traps
Tracebacks
Chapter€4.€Control Statements
Conditionals
Loops
Simple Loop Examples
Initialization of Loop Values
Looping Forever
Loops with Guard Conditions
Iterations
Iteration Statements
Kinds of Iterations
Do
Collect
Combine
Search
Filter
Nested iterations
Recursive iterations
Exception Handlers
Python Errors
Tracebacks
Runtime errors
Exception Handling Statements
Optional features of exception handling statements
Exception handling and generator objects
Raising Exceptions
The raise statement
Raising an exception to end a loop
Extended Examples
Extracting Information from an HTML File
The Grand Unified Bioinformatics File Parser
Reading the sequences in a FASTA file
Generalized parsing
Parsing GenBank Files
Translating RNA Sequences
Step 1
Step 2
Step 3
Step 4
Step 5
Constructing a Table from a Text File
Step 1
Step 2
Step 3
Step 4
Step 5
Tips, Traps, and Tracebacks
Tips
Traps
Tracebacks
Chapter€5.€Classes
Defining Classes
Instance Attributes
Access methods
Predicate methods
Initialization methods
String methods
Modification methods
Action methods
Support methods
Summary
Class Attributes
Class fields
Class methods
Classes as objects
Class and Method Relationships
Decomposition
Class decomposition
Method decomposition
Singleton classes
Inheritance
Defining subclasses
Factoring out common code
Generalization
Subclass methods
Tips, Traps, and Tracebacks
Tips
Traps
Tracebacks
Chapter€6.€Utilities
System Environment
Dates and Times: datetime
Classes
Instance creation
Operations
Methods
System Information
The Python runtime environment: sys
The system clock: time
Command-Line Utilities
Reading multiple files: fileinput
Command-line options: optparse
Command-line commands: subprocess
Communications
Sending email: smtplib
Logging: logging
The Filesystem
Operating System Interface: os
Environment access
Managing files and directories
Directories
Files
Directory contents
Temporary files: tempfile
Manipulating Paths: os.path
Path components
Path manipulations
Path information
Filename Expansion: fnmatch and glob
fnmatch
glob
Shell Utilities: shutil
Comparing Files and Directories
File and directory comparison: filecmp
Showing the differences between two files: difflib
Working with Text
Formatting Blocks of Text: textwrap
String Utilities: string
Comma- and Tab-Separated Formats: csv
String-Based Reading and Writing: io
Persistent Storage
Persistent Text: dbm
Persistent Objects: pickle
Keyed Persistent Object Storage: shelve
Debugging Tools
The Python debugger: pdb
The IDLE debugger
Tips, Traps, and Tracebacks
Tips
Traps
Tracebacks
Chapter€7.€Pattern Matching
Fundamental Syntax
Fixed-Length Matching
Literal matches
Character sets
Character classes
Boundaries
Variable-Length Matching
Repetition
Greedy Versus Nongreedy Matching
Grouping and Disjunction
The Actions of the re Module
Functions
Flags
Methods
Results of re Functions and Methods
Match Object Fields
Match Object Methods
Putting It All Together: Examples
Some Quick Examples
Using regular expressions to ignore case
Listing files in a directory, excluding some
Finding open reading frames
Extracting Descriptions from Sequence Files
Extracting Entries From Sequence Files
Using regular expressions to extract entries
Keeping track of the position between calls
Buffering input
Tips, Traps, and Tracebacks
Tips
Traps
Tracebacks
Chapter€8.€Structured Text
HTML
Simple HTML Processing
Searching HTML text
More elaborate HTML pattern matching
Turning HTML into plain text
Structured HTML Processing
Problems with HTML pattern matching
Structured HTML parsing: html.parser
XML
The Nature of XML
An XML File for a Complete Genome
The ElementTree Module
Getting started with ElementTree
Navigating around an ElementTree
Event-Based Processing
Function calls, exceptions, and the call stack
Callbacks: Reversed function calls
Programming for an event-based processor
expat
Obtaining a single piece of information
Extracting a few pieces of data
Getting the content of all tags with a specified name
Tips, Traps, and Tracebacks
Tips
Traps
Tracebacks
Chapter€9.€Web Programming
Manipulating URLs: urllib.parse
Disassembling URLs
Assembling URLs
Opening Web Pages: webbrowser
Module Functions
Constructing and Submitting Queries
Constructing and Viewing an HTML Page
Web Clients
Making the URLs in a Response Absolute
Constructing an HTML Page of Extracted Links
Downloading a Web Page╎s Linked Files
Web Servers
Sockets and Servers
Server fundamentals
Running a server: The http.server module
CGI
Serving CGI requests
Setting up CGI
CGI script arguments and responses
Simple Web Applications
Using CGI scripts
HTML forms with CGI scripts
Tips, Traps, and Tracebacks
Tips
Web pages
Debugging CGI scripts
HTTP servers
Traps
Tracebacks
Chapter€10.€Relational Databases
Representation in Relational Databases
Database Tables
Avoiding duplication of values
Representation of relationships
Database definition
A Restriction Enzyme Database
The data
Reading the data
A schema for the Rebase database
Using Relational Data
SQL Basics
Using the sqlite3 module
Connecting to the database
Creating the database
Loading data into tables
SQL Queries
Simple database queries
Qualified database queries
Relationship queries
Querying the Database from a Web Page
HTML frames
An HTML page of query forms
Tips, Traps, and Tracebacks
Tips
Traps
Tracebacks
Chapter€11.€Structured Graphics
Introduction to Graphics Programming
Concepts
Coordinate system
Components
Properties
GUI Toolkits
GUI toolkits for Python
Using a GUI toolkit
Structured Graphics with tkinter
tkinter Fundamentals
The basic steps
Common widget options
Canvas drawing methods
Writing the contents of a canvas to a PostScript file
Examples
An abstract class for tkinter graphics
Dot plots
Histograms
Structured Graphics with SVG
SVG File Contents
SVG tags
SVG styles
Examples
Histograms of codon use
Sequencing trace file curves using SVG
Tips, Traps, and Tracebacks
Tips
Traps
SVG traps
Tracebacks
Appendix€A.€Python Language Summary
Language Components
Special Syntactic Elements
Keywords
Special Names
Operators
Anonymous Functions
Types and Expressions
Statements
Notes
Appendix€B.€Collection Type Summary
Types and General Operations
Common Operations
Creating Collections
Specific Collection Types
Sets
Sequences
Strings
Lists
Mappings
Streams
File objects
Generators
Iteration Templates
Basic Iteration Templates
Filtering Templates
Other Kinds of Templates
Index
www.1024ebook.com
Bioinformatics Programming Using Python www.1024ebook.com
www.1024ebook.com
Bioinformatics Programming Using Python Mitchell L Model Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.1024ebook.com
Bioinformatics Programming Using Python by Mitchell L Model Copyright © 2010 Mitchell L Model. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Mike Loukides Production Editor: Sarah Schneider Copyeditor: Rachel Head Proofreader: Sada Preisch Printing History: December 2009: First Edition. Indexer: Lucie Haskins Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Bioinformatics Pro- gramming Using Python, the image of a brown rat, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. TM This book uses RepKover, a durable and flexible lay-flat binding. ISBN: 978-0-596-15450-9 [M] 1259959883 www.1024ebook.com
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Expressions Simple Values Booleans Integers Floats Strings 1. Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 2 2 3 4 5 5 7 9 12 16 18 18 20 20 Numeric Operators Logical Operations String Operations Calls Compound Expressions Tips Traps Tracebacks Tips, Traps, and Tracebacks Assigning Names Defining Functions Function Parameters Comments and Documentation Assertions Default Parameter Values 2. Names, Functions, and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 23 24 27 28 30 32 34 34 38 40 40 Tips, Traps, and Tracebacks Importing Python Files Using Modules Tips www.1024ebook.com v
Traps Tracebacks 45 46 Mappings Dictionaries Sets Sequences Strings, Bytes, and Bytearrays Ranges Tuples Lists 3. Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 48 51 53 60 61 62 66 67 72 73 78 79 79 89 94 94 96 97 Comprehensions Functional Parameters Streams Files Generators Collection-Related Expression Features Tips Traps Tracebacks Tips, Traps, and Tracebacks Iterations Conditionals Loops Iteration Statements Kinds of Iterations Simple Loop Examples Initialization of Loop Values Looping Forever Loops with Guard Conditions 4. Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 101 104 105 106 107 109 111 111 113 134 136 138 141 143 143 146 148 151 155 Extracting Information from an HTML File The Grand Unified Bioinformatics File Parser Parsing GenBank Files Translating RNA Sequences Constructing a Table from a Text File Python Errors Exception Handling Statements Raising Exceptions Exception Handlers Extended Examples vi | Table of Contents www.1024ebook.com
分享到:
收藏