CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
HOW TO RUN PERL SCRIPT ON WEBBROWSER

 



ankit
Novice

Apr 9, 2010, 12:37 AM

Post #1 of 4 (3473 views)
HOW TO RUN PERL SCRIPT ON WEBBROWSER Can't Post

Hi Friends,
can any one tell me taht i am not able to run my script on webpage but it is running in command promt & I am also try Cgi module but i am not suceed can any one help me.
my script is--
and i am also attached the file bionet for the list of restriction enzymes.

[#!/usr/bin/perl
#Make restriction map from user queries on names of restriction enzymes
use strict;
use warnings;
#Declare and intialize variables
my %rebase_hash = ( );
my @file_data =( );
my $query = ' ';
my $dna = ' ';
my $recognition_site = ' ';
my $regexp = ' ';
my @locations = ( );
my $dnafilename = ( );
my @DNA = ( );
my @positions = ( );
my $ sequence = ( );
my $RNA = ( );
# Read in the file "sample.dna"
print "ENTER THE FILENAME OF THE DNA SEQUENCE:= ";
$dnafilename = <STDIN>;
chomp $dnafilename;
unless ( open(DNAFILE, $dnafilename) ) {
print "Cannot open file \"$dnafilename\"\n\n";
}
@DNA = <DNAFILE>;
print "The original DNA Sequence :=\n\n";
@file_data=@DNA;
print"@file_data";


# Extract the DNA sequence data from the contents of the file "sample.dna"
$dna = extract_sequence_from_fasta_data(@file_data);
# Get the REBASE data into a hash, from file "bionet.txt"
%rebase_hash = parseREBASE('bionet.txt');


# Promt user for restriction enzyme names, create restriction map
do {
print "Search for what restriction site for (or quit)?: ";

$query = <STDIN>;

chomp $query;
print"$query";

#

#Exit if empty query
if ( $query =~ /^\s*$/ ) {

exit;
}

# Perform the search in the DNA sequence
if ( exists $rebase_hash{$query} ) {

($recognition_site, $regexp) = split ( " " , $rebase_hash{$query});

print " -------------------------->$regexp#:\n\n";


#Create the restriction map
@locations = match_positions($regexp, $dna);

#Report the restriction map to the user
if (@locations) {
print "Searching for $query $recognition_site $regexp\n";
print "A restriction site for $query at location:\n";
print join(" \n",@locations), "\n";
exit;
} else {
print " A restriction site for $query is not in the DNA:\n";
}
}
print "\n";
} until ( $query =~ /quit/ );
exit;

#############################
#
# Subroutine
#
# Find locations of a match of a regular expression in a string #
#
# return an array of positions where the regular expression
# appears in the string
#
sub match_positions {

my($regexp, $sequence) = @_;

use strict;



#
# Declare varibles
#

my @positions = ( );
#
# Determine positions of regular expression matches
#

while ( $sequence =~ /$regexp/ig ) {

push ( @positions, pos($sequence) - length($&) + 1 );

}

return @positions;

}

###########
# From example9-1.pl

# Example 9-1 Translate IUB ambiguity codes to regular expressions
# IUB_to_regexp
#
# A subroutine that, given a sequence with IUB ambiguity codes,
# outputs a translation with IUB codes changed to regular expressions
#
# These are the IUB ambiguity codes
# (Eur. J. Biochem. 150: 1-5, 1985):
# R = G or A
# Y = C or T
# M = A or C
# K = G or T
# S = G or C
# W = A or T
# B = not A (C or G or T)
# D = not C (A or G or T)
# H = not G (A or C or T)
# V = not T (A or C or G)
# N = A or C or G or T

sub IUB_to_regexp {

my($iub) = @_;

my $regular_expression = '';

my %iub2character_class = (

A => 'A',
C => 'C',
G => 'G',
T => 'T',
R => '[GA]',
Y => '[CT]',
M => '[AC]',
K => '[GT]',
S => '[GC]',
W => '[AT]',
B => '[CGT]',
D => '[AGT]',
H => '[ACT]',
V => '[ACG]',
N => '[ACGT]',
);

# Remove the ^ signs from the recognition sites
$iub =~ s/\^//g;

# Translate each character in the iub sequence
for ( my $i = 0 ; $i < length($iub) ; ++$i ) {
$regular_expression
.= $iub2character_class{substr($iub, $i, 1)};
}

return $regular_expression;

}

###########
# From example9-2.pl

# Example 9-2 Subroutine to parse a REBASE datafile
# parseREBASE-Parse REBASE bionet file
#
# A subroutine to return a hash where
# key = restriction enzyme name
# value = whitespace-separated recognition site and regular expression

sub parseREBASE {

my($rebasefile) = @_;

use strict;
use warnings;

# Declare variables
my @rebasefile = ( );
my %rebase_hash = ( );
my $name;
my $site;
my $regexp;

# Read in the REBASE file
my $rebase_filehandle = open_file($rebasefile);

while(<$rebase_filehandle>) {

# Discard header lines
( 1 .. /Rich Roberts/ ) and next;

# Discard blank lines
/^\s*$/ and next;

# Split the two (or three if includes parenthesized name) fields
my @fields = split( " ", $_);

# Get and store the name and the recognition site

# Remove parenthesized names, for simplicity's sake,
# by not saving the middle field, if any,
# just the first and last
$name = shift @fields;

$site = pop @fields;

# Translate the recognition sites to regular expressions
$regexp = IUB_to_regexp($site);

# Store the data into the hash
$rebase_hash{$name} = "$site $regexp";
}

# Return the hash containing the reformatted REBASE data
return %rebase_hash;
}

# extract_sequence_from_fasta_data
#
# A subroutine to extract FASTA sequence data from an array

sub extract_sequence_from_fasta_data {

my(@fasta_file_data) = @_;

use strict;
use warnings;

# Declare and initialize variables
my $sequence = '';
my $line = '';

foreach my $line (@fasta_file_data) {

# discard blank line
if ($line =~ /^\s*$/) {
next;

# discard comment line
} elsif($line =~ /^\s*#/) {
next;

# discard fasta header line
} elsif($line =~ /^>/) {
next;

# keep line, add to sequence string
} else {
$sequence .= $line;
}
}

# remove non-sequence data (in this case, whitespace) from $sequence string
$sequence =~ s/\s//g;

return $sequence;
}

############################################################
# open_file
#
# - given filename, set filehandle

sub open_file {

my($filename) = @_;
my $fh;

unless(open($fh, $filename)) {
print "Cannot open file $filename\n";
exit;
}
return $fh;
}


][quote][/quote][quote][/quote][quote][/quote][quote][reply][/code][code]


roolic
User

Apr 12, 2010, 9:38 PM

Post #2 of 4 (3459 views)
Re: [ankit] HOW TO RUN PERL SCRIPT ON WEBBROWSER [In reply to] Can't Post

web browser communicates with the remote (or local) host using the http protocol. however the the browser itself is not able to execute the script and the perl interpreter can not process http requests. So the script execution should be performed by some server which will
1. accept the http request
2. call perl interpreter for requested script
3. retransfer the script response back to browser using http

the following is simple steps make script running under apache
1. ensure the apache should treat the perl scripts as cgi-scripts:
apache configuration file httpd.conf should contain the following lines

Code
AddHandler cgi-script .cgi 
AddHandler cgi-script .pl


2. ensure the script execution is allowed from the folder where the script is (the host aliases to folder should be already set):
- for windows:

Code
<Directory "d:/myscripts"> 
Options Indexes FollowSymLinks ExecCGI
# ...
</Directory>


- for *nix:

Code
<Directory "/home/me/myscripts"> 
Options Indexes FollowSymLinks ExecCGI
# ...
</Directory>


in addition (for *nixes) the script file attribute should be set as executable (X)

3. ensure the perl interpreter can be found by apache:
script's very first line should contain the path to perl (with no any leading characters):
- for windows

Code
#!c:\perl\bin\perl.exe

- for *nixes (usually)

Code
#!/usr/bin/perl


4. ensure the script output is browser compatible:
- the server response should contain the specific information in the very beginning to instruct the browser how it should understand the incoming data. if the server (script) just types the text (or html) the very first 'print' in the script should be the following:

Code
print "Content-type: text/html\n\n";


finally type the url in the browser like
http://host/path/script.pl
and you'll see the script output over there


ankit
Novice

Apr 12, 2010, 10:17 PM

Post #3 of 4 (3454 views)
Re: [roolic] HOW TO RUN PERL SCRIPT ON WEBBROWSER [In reply to] Can't Post

Thanks ,,
but i already do that and my program is working.
can you tell me how can i give it template or theme in back site f this program into web browser.

In Reply To


roolic
User

Apr 13, 2010, 7:56 AM

Post #4 of 4 (3446 views)
Re: [ankit] HOW TO RUN PERL SCRIPT ON WEBBROWSER [In reply to] Can't Post

rather popular is HTML::Template library
check it here: http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm
there a lot of examples

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives