Databases and Perl Using Perl to talk to databases.

Slides:



Advertisements
Similar presentations
Programming MySQL, Perl COEN 351. Reading List Paul DuBois: MySQL and Perl for the Web, New Riders, 2002 Jacqueline D. Hamilton: CGI Programming 101,
Advertisements

NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.
Murali Mani SQL-PL Interface. Murali Mani Some Possible Options Web Interface Perl /CGI with Oracle/mySQL Install your own web server and use servlets.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Getting Organised Subroutines, modules and the wonder of CPAN.
Patterns, Patterns and More Patterns Exploiting Perl's built-in regular expression technology.
Implementing Session Support COEN 351. State Maintenance Client Side Mechanisms  Cookies Client needs to allow cookies Cookie handling done by browser.
MSc Bioinformatics 2006 Biocomputing Databases and Perl 13 th February 2006.
1 Lecture 6 Some useful bits&pieces that every Perl programmer should know.
Web Automation Using Perl to automate web surfing.
Perl and MySQL Using apache server with perl cgi Using MySQL Need to install dbi and dbd modules to your perl installation. There are some good examples.
Databases Learning to talk database. Introducing Databases ● A database is a collection of one or more related tables ● A table is a collection of one.
How to create a simple Web application with Web::App Template::Toolkit and Class::DBI Leonard Miller June 17, 2008.
CSE 190: Internet E-Commerce Lecture 13: Database code.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #21 M.P. Johnson Stern School of Business, NYU Spring, 2005.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
The GUS 3.0 Perl Object Layer CBIL Jonathan Schug June
Perl/cgi and an Access Database Please use speaker notes for additional information!
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Set 5: Perl and Database Connections
1 Lecture 7 Introduction to Bioinformatics Dr N AYDIN.
Survey of Advanced Perl Topics Database access "OpSys"-like functions Signal Handling Inside-Out Objects.
27/07/2001 Copyright 2001, Munica Corporation, 1 How to write a DBD driver Daini Xie Strathy President Munica Corporation
DBI tutorial February 5 th, What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is.
Applied Research Laboratory UNCLASSIFIED IOUW 2000 Managing Oracle and Other Cool Things You Can Do with PERL John D. Groenveld Manufacturing Systems Division.
GUS Plugin System Michael Saffitz Genomics Unified Schema Workshop July 6-8th, Philadelphia, Pennsylvania.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Bioinformatics Course Day 3 MySQL. Topics ● Databases ● MySQL ● SQL ● Permissions ● Usage ● Examples.
Templates, Databases and Frameworks. Databases: DBI Common database interface for perl Provides a functional,
– Introduction to CGI 4/8/ Introduction to CGI Introduction to CGI – Session 2 · Introduction to CGI:  Generating.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
Databases On The Web with perl Archie Warnock
Bioinformatics 生物信息学理论和实践 唐继军
96-Summer 生物資訊程式設計實習 ( 二 ) Bioinformatics with Perl 8/13~8/22 蘇中才 8/24~8/29 張天豪 8/31 曾宇鳯.
Perl for People Singer XJ Wang Learning Center Available At:
How to create a simple Web application with CGI::Application Template::Toolkit and DBIx::Class Leonard Miller February 7, 2009.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
McGraw-Hill/Irwin The O’Leary Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Access 2002 Lab 3 Analyzing Tables and Creating.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Perl II Part III: Motifs and Loops. Objectives Search for motifs in DNA or Proteins Interact with users at the keyboard Write data to files Use loops.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
ITN Wake Tech1 ITN270 Advanced Internet Databases Lecture 11. The Perl DBI API - Part II Topics: – Perl DBI interface to MySQL.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Four Instructor:
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Three Instructor:
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Students’ Registration for the MultiMedia Festival Students’ Registration for the MultiMedia Festival A Project on Perl Submitted by: Jayati Das Guided.
DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Advanced Perl For Bioinformatics Part 1 2/23/06 1-4pm Module structure Module path Module export Object oriented programming Part 2 2/24/06 1-4pm Bioperl.
Perl Subroutines User Input Perl on linux Forks and Pipes.
MySQL-database kaivos.csc.fi. MySQL-database  MySQL database server linked to supercomputer  Enables users.
COMP234 - perl Perl DBI Topics Database vs access methods DBMS and DBMS API's Relational database SEQUEL Perl DBI SQL.
Please remember to thank our sponsors!
Chapter 7 - Introduction to Common Gateway Interface (CGI)
using non-Cobol programs
Input, Output and Other Things
Using the Internet to publish data and applications
Perl Modules.
Perl Database – Just Enough
Advanced Topics Web Programming.
Introduction to Bioinformatics
Presentation transcript:

Databases and Perl Using Perl to talk to databases

Why Program Databases?

Maxim 13.1 If at all possible, avoid the use of database driver ``enhancements''

$ man DBI $ man DBD::mysql $ find `perl -Te 'print ` -name '*.pm' -print | grep 'DBI.pm' $ find `perl -Te 'print ` -name '*.pm' -print | grep 'mysql.pm' $ locate DBI.pm $ locate mysql.pm Preparing Perl

#! /usr/bin/perl -w # check_drivers - check which drivers are installed with DBI. use strict; use DBI; = DBI->available_drivers; foreach my $driver ) { print "Driver: $driver installed.\n"; } Checking the DBI installation

#! /usr/bin/perl -w # show_tables - list the tables within the MER database. # Uses "DBI::dump_results" to display results. use strict; use DBI qw( :utils ); use constant DATABASE => "DBI:mysql:MER"; use constant DB_USER => "bbp"; use constant DB_PASS => "passwordhere"; my $dbh = DBI->connect( DATABASE, DB_USER, DB_PASS ) or die "Connect failed: ", $DBI::errstr, ".\n"; my $sql = "show tables"; my $sth = $dbh->prepare( $sql ); $sth->execute; print dump_results( $sth ), "\n"; $sth->finish; $dbh->disconnect; Programming Databases With DBI

Maxim 13.2 Be sure to adhere to any established naming conventions within a programming community

$ chmod +x show_tables $./show_tables 'citations' 'crossrefs' 'dnas' 'proteins' 4 rows 4 Results from show_tables...

Maxim 13.3 Avoid littering programs with username/password combinations

package DbUtilsMER; # DbUtilsMER.pm - the database utilities module from "Bioinformatics, # Biocomputing and Perl". # # Version 0.01: module created to hold MERconnectDB. require Exporter; = qw( Exporter ); = qw( MERconnectDB ); = qw(); our %EXPORT_TAGS = (); our $VERSION = 0.01; Developing A Database Utility Module

use constant DATABASE => "DBI:mysql:MER"; use constant DB_USER => "bbp"; use constant DB_PASS => "passwordhere"; sub MERconnectDB { # # Given: nothing. # Return: a "connected" database handle to the MER database or # if no connection is possible, return "undef". # return DBI->connect( DATABASE, DB_USER, DB_PASS ); } 1; Developing A Database Utility Module, cont.

use lib "$ENV{'HOME'}/bbp/"; use DbUtilsMER; my $dbh = MERconnectDB or die "Connect failed: ", $DBI::errstr, ".\n"; use constant DATABASE => "DBI:mysql:MER"; use constant DB_USER => "bbp"; use constant DB_PASS => "passwordhere"; my $dbh = DBI->connect( DATABASE, DB_USER, DB_PASS ) or die "Connect failed: ", $DBI::errstr, ".\n"; Using the Database Utility Module

#! /usr/bin/perl -w # show_tables2 - list the tables within the MER database. # Uses "fetchrow_array" to display results. use strict; use DBI; use lib "$ENV{'HOME'}/bbp/"; use DbUtilsMER; my $dbh = MERconnectDB or die "Connect failed: ", $DBI::errstr, ".\n"; my $sql = "show tables"; my $sth = $dbh->prepare( $sql ); Improving upon dump_results

$sth->execute; print "The MER database contains the following tables:\n\n"; while ( = $sth->fetchrow_array ) { foreach my $column_value ) { print "\t$column_value\n"; } $sth->finish; $dbh->disconnect; Improving upon dump_results, cont.

The MER database contains the following tables: citations crossrefs dnas proteins Results from show_tables2...

There are 22 cross references in the database. The protein P04336 is cross referenced with J The protein P08332 is cross referenced with J The protein P08654 is cross referenced with M The protein P20102 is cross referenced with X The protein Q52107 is cross referenced with AF The protein P03830 is cross referenced with J The protein Q52109 is cross referenced with AF The protein P20102 is cross referenced with J The protein Q52106 is cross referenced with AF The protein P04337 is cross referenced with K The protein P08664 is cross referenced with M Customizing Output

#! /usr/bin/perl -w # which_crossrefs - nicely displayed list of protein->dna # cross references. use strict; use DBI; use lib "$ENV{'HOME'}/bbp/"; use DbUtilsMER; my $dbh = MERconnectDB or die "Connect failed: ", $DBI::errstr, ".\n"; my $sql = "select * from crossrefs"; my $sth = $dbh->prepare( $sql ); Customizing Output, cont.

$sth->execute; print "There are ", $sth->rows, " cross references in the database.\n\n"; while ( = $sth->fetchrow_array ) { print "The protein $row[0] is cross referenced with $row[1].\n"; } $sth->finish; $dbh->disconnect; Customizing Output, cont.

while ( my ( $protein, $dna ) = $sth->fetchrow_array ) { print "The protein $protein is cross referenced with $dna.\n"; } while ( my $row = $sth->fetchrow_hashref ) { print "The protein $row->{ ac_protein } is cross referenced "; print "with $row->{ ac_dna }.\n"; } Alternatives to fetchrow_array

Maxim 13.4 Use fetchrow_hashref to guard against changes to the structure of a database table

Provide a protein accession number to cross reference ('quit' to end): p03377 Not found: there is no cross reference for that protein in the database. Provide a protein accession number to cross reference ('quit' to end): p04337 Found: P04337 is cross referenced with J Provide a protein accession number to cross reference ('quit' to end): q52109 Found: Q52109 is cross referenced with AF Provide a protein accession number to cross reference ('quit' to end): x6587 Not found: there is no cross reference for that protein in the database. Provide a protein accession number to cross reference ('quit' to end): quit Customizing Input

#! /usr/bin/perl -w # specific_crossref - allow for the "interactive" checking # of crossrefs from the command-line. # Keep going until the user enters "quit". use strict; use DBI; use lib "$ENV{'HOME'}/bbp/"; use DbUtilsMER; use constant TRUE => 1; my $dbh = MERconnectDB or die "Connect failed: ", $DBI::errstr, ".\n"; my $sql = qq/ select ac_dna from crossrefs where ac_protein = ? /; my $sth = $dbh->prepare( $sql ); Customizing Input

while ( TRUE ) { print "\nProvide a protein accession number to cross "; print "reference ('quit' to end): "; my $protein2find = <>; chomp $protein2find; $protein2find = uc $protein2find; if ( $protein2find eq 'QUIT' ) { last; } Customizing Input, cont.

$sth->execute( $protein2find ); my $dna = $sth->fetchrow_array; $sth->finish; if ( !$dna ) { print "Not found: there is no cross reference for that protein "; print "in the database.\n"; } else { print "Found: $protein2find is cross referenced with $dna.\n"; } $dbh->disconnect; Customizing Input, cont.

#! /usr/bin/perl -w # The 'db_match_embl' program - check a sequence against each EMBL # database entry stored in the dnas # table within the MER database. use strict; use DBI; use lib "$ENV{'HOME'}/bbp/"; use DbUtilsMER; use constant TRUE => 1; use constant FALSE => 0; my $dbh = MERconnectDB or die "Connect failed: ", $DBI::errstr, ".\n"; my $sql = qq/ select accession_number, sequence_data, sequence_length from dnas /; my $sth = $dbh->prepare( $sql ); Extending SQL

while ( TRUE ) { my $sequence_found = FALSE; print "Please enter a sequence to check ('quit' to end): "; my $to_check = <>; chomp( $to_check ); $to_check = lc $to_check; if ( $to_check =~ /^quit$/ ) { last; } $sth->execute; while ( my ( $ac, $sequence, $sequence_length ) = $sth->fetchrow_array ) { $sequence =~ s/\s*//g; Extending SQL, cont.

if ( $sequence =~ /$to_check/ ) { $sequence_found = TRUE; print "The EMBL entry in the database: ", $ac, " contains: $to_check.\n"; print "[Lengths: ", length $sequence, "/$sequence_length]\n\n"; } if ( !$sequence_found ) { print "No match found in database for: $to_check.\n\n"; } $sth->finish; } $dbh->disconnect; Extending SQL, cont.

Please enter a sequence to check ('quit' to end): aattgc The EMBL entry in the database: AF contains: aattgc. [Lengths: 6838/6838] Please enter a sequence to check ('quit' to end): aatttc The EMBL entry in the database: AF contains: aatttc. [Lengths: 6838/6838] The EMBL entry in the database: J01730 contains: aatttc. [Lengths: 5747/5747] Please enter a sequence to check ('quit' to end): accttaaatttgtacgtg No match found in database for: accttaaatttgtacgtg. Please enter a sequence to check ('quit' to end): quit Results from db_match_embl...

Where To From Here