Categories: BIO, Perl | Tags: | Views: 2,042

 

我只能说,How 强大 Perl is!

我用过不同的方法来实现网页的post,但是perl是最方便最好用的!

废话不多说,看代码(这是一段实际工作中用到的代码,也可以修改一下用作其他用途,严禁做坏事:)) ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/perl
#
# Panwen Wang, April 7th, 2009
# Submit a query to http://consurf.tau.ac.il/
# to get conservation scores of each residue.
#
 
 
use URI::Escape;
use LWP::UserAgent;
use LWP::Simple;
use HTTP::Request::Common qw(POST);
use strict;
 
my $ua = LWP::UserAgent->new;
 
if(@ARGV<1){
    print STDERR "\n $0: \n";
    print STDERR "\tPanwen Wang, April 7th, 2009";
    print STDERR "\tSubmit a query to http://consurf.tau.ac.il/ \n";
    print STDERR "\tto get conservation scores of each residue.\n";
    print STDERR "\n Parameters:\n";
    print STDERR "\t-p <pdb_ID>         pdb id in four letters\n";
    print STDERR "\t-c <chain>          chain label of given pdb\n";
    print STDERR "\t-a <algorithm>      Bayes/LikelihoodML\n";
    print STDERR "\t\tDEFAULT:Bayes\n";
    print STDERR "\t-m <MSAprogram>     MUSCLE/CLUSTALW\n";
    print STDERR "\t\tDEFAULT: MUSCLE\n";
    print STDERR "\t-d <DATABASE>       the Homologues database, SWISS-PROT/Uniprot\n";
    print STDERR "\t\tDEFAULT: SWISS-PROT\n";
    print STDERR "\t-n <MAX_NUM_HOMOL>  Max. Number of Homologues\n";
    print STDERR "\t\tDEFAULT: 50\n";
    print STDERR "\t-i <ITERATIONS>     No. of PSI-BLAST Iterations\n";
    print STDERR "\t\tDEFAULT: 1\n";
    print STDERR "\t-e <ESCORE>         PSI-BLAST E-value Cutoff\n";
    print STDERR "\t\tDEFAULT: 0.001\n";
    print STDERR "\t-s <MATRIX>         Model of substitution for proteins\n";
    print STDERR "\t\tJTT(DEFAULT)/mtREV(for mitochondrial proteins)/cpREV(for chloroplasts proteins)/WAG/Dayhoff\n";
    print STDERR "\t-l <email>          user's email\n";
    print STDERR "\t-o <output folder>  where the result files(1A0O_A.score) restore\n";
    print STDERR "\n Usage:\n";
    print STDERR "\t$0 -p 2UUY -c A -l xxx\@xxx.com -o ./conservation\n";
    print STDERR "\t$0 -p 2UUY -c A -l xxx\@xxx.com -o ./conservation -a LikelihoodML ...\n\n";
    exit;
}
 
my ($temp, $pdb_ID, $chain, $user_email, $ALGORITHM, $MSAprogram, $DATABASE, $MAX_NUM_HOMOL, $ITERATIONS, $ESCORE, $MATRIX, $out_dir);
 
while(@ARGV){
    $temp = shift;
    if($temp eq "-p") { $pdb_ID = shift; }
    elsif($temp eq "-c") { $chain = shift; }
    elsif($temp eq "-a") { $ALGORITHM = shift; }
    elsif($temp eq "-m") { $MSAprogram = shift; }
    elsif($temp eq "-d") { $DATABASE = shift; }
    elsif($temp eq "-n") { $MAX_NUM_HOMOL = shift; }
    elsif($temp eq "-i") { $ITERATIONS = shift; }
    elsif($temp eq "-e") { $ESCORE = shift; }
    elsif($temp eq "-s") { $MATRIX = shift; }
    elsif($temp eq "-l") { $user_email = shift; }
    elsif($temp eq "-o") { $out_dir = shift; }
    else {
        print STDERR "Warning: argument $temp igorned.\n";
    }
}
 
die "Error: argument '-p' is essential.\n" if(!defined($pdb_ID));
die "Error: argument '-c' is essential.\n" if(!defined($chain));
die "Error: argument '-l' is essential.\n" if(!defined($user_email));
die "Error: argument '-o' is essential.\n" if(!defined($out_dir));
$ALGORITHM = "Bayes"                       if(!defined($ALGORITHM));
$MSAprogram = "MUSCLE"                     if(!defined($MSAprogram));
$DATABASE = "SWISS-PROT"                   if(!defined($DATABASE));
$MAX_NUM_HOMOL = "50"                      if(!defined($MAX_NUM_HOMOL));
$ITERATIONS = "1"                          if(!defined($ITERATIONS));
$ESCORE = "0.001"                          if(!defined($ESCORE));
$MATRIX = "JTT"                            if(!defined($MATRIX));
 
# end of handling parameters
 
# read and encode the queries
#my $encoded_query;
##foreach my $query (@ARGV)
##    {
##    open(QUERY,$query);
##    while(<QUERY>)
##        {
##        $encoded_query = $encoded_query . uri_escape($_);
##        }
# }
 
# build the request
my $args = "pdb_ID=$pdb_ID&chain=$chain&user_email=$user_email&ALGORITHM=$ALGORITHM&MSAprogram=$MSAprogram&DATABASE=$DATABASE&MAX_NUM_HOMOL=$MAX_NUM_HOMOL&ITERATIONS=$ITERATIONS&ESCORE=$ESCORE&MATRIX=$MATRIX&send_user_mail=yes&BayesInterval=3";
 
print "Querying ...\n";
 
my $req = new HTTP::Request POST => 'http://consurf.tau.ac.il/cgi-bin/consurf.cgi';
$req->content_type('application/x-www-form-urlencoded');
$req->content($args);
 
# get the response
my $response = $ua->request($req);
 
# parse out the request id
$response->content =~ /The document has moved <a href=\"(.+)\">here/;
 
my $result_page = $1;
 
print "Waiting ...\n";
sleep 25;
 
# poll for results
for(;;){
    sleep 5;
 
    my $req = new HTTP::Request GET => $result_page;
    $response = $ua->request($req);
 
    my $content = $response->content;
    if($content =~ /ConSurf calculation is finished/){
        print "Getting result ...\n";
        my $prefix_url = $1 if($result_page =~ /(.+)output\.html/);
        my $result_file = "${prefix_url}pdb".lc($pdb_ID).".gradesPE";
        print "Saving result ...\n";
        getstore($result_file, "$out_dir/${pdb_ID}_$chain.score");
        #my $req1 = new HTTP::Request GET => $result_file;
        #my $response1 = $ua->request($req1);
        #open(OUT,">$out_dir/${pdb_ID}_$chain.score");
        #print OUT  $response1->content;
        last;
    } elsif($content =~ /For assistance please/){
        die("ERROR! ConSurf session has been terminated.\n");
    } else {
        print "Waiting ...\n";
        next;    
    }
    die("Sth. unexpected happened.\n");
} # end poll loop

这个例子稍复杂了点,但是个比较全面的例子了,也可以post文件,但需要中间那段uri_escape.另外,我在考虑一个问题,这样post后,页面接收到的$_SERVER["HTTP_REFERER"](以php为例)会是什么呢?只是觉得很奇怪,但是没有实验过,哪天有时间实验一下,再把结果贴出来。

 

这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明

  1. May 5th, 2009 at 12:34
    Reply | Quote | #1

    Perl确实very good very strong…我打算利用暑假好好学习一下

  2. aaron
    February 8th, 2010 at 21:34
    Reply | Quote | #2

    问下,这句我不明白, POST => ‘http://consurf.tau.ac.il/cgi-bin/consurf.cgi’这是一个匿名HASH吗?谢谢帮忙

    • February 11th, 2010 at 01:16
      Quote | #3

      @aaron:是的,这个匿名的HASH作为HTTP::Request的参数来进行实例化对象。

  3. aaron
    February 8th, 2010 at 21:35
    Reply | Quote | #4

    如果可以请把答案发我邮箱谢谢27538301@qq.com

1 trackbacks

  1. req1 Pingback | 2010/04/03
;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

你可以使用@somebody:开头, 来邮件通知somebody你回复了他的留言(用户名区分大小写).