my
$ANALYSIS_SPEC
= {
name
=>
'MeSH term retrival'
,
type
=>
'Entry retrieval'
};
my
$INPUT_SPEC
= [
{
mandatory
=>
'true'
,
type
=>
'scalar'
,
'name'
=>
'value'
,
},
];
my
$RESULT_SPEC
=
{
''
=>
'Bio::Phenotype::MeSH::Term'
,
raw
=>
'raw output'
,
};
sub
_init {
my
$self
=
shift
;
$self
->url(
$URL
);
$self
->{
'_ANALYSIS_SPEC'
} =
$ANALYSIS_SPEC
;
$self
->{
'_INPUT_SPEC'
} =
$INPUT_SPEC
;
$self
->{
'_RESULT_SPEC'
} =
$RESULT_SPEC
;
$self
->{
'_ANALYSIS_NAME'
} =
$ANALYSIS_SPEC
->{
'name'
};
$self
->_webmodule;
return
$self
;
}
sub
_webmodule {
my
(
$self
) =
shift
;
$self
->{
'_webmodule'
} =
''
;
eval
{
};
unless
($@) {
$self
->{
'_webmodule'
} =
'WWW::Mechanize'
;
return
;
}
eval
{
};
unless
($@) {
$self
->{
'_webmodule'
} =
'Bio::WebAgent'
;
return
;
}
$self
->{
'_webmodule'
} =
'Bio::Root::HTTPget'
;
1;
}
sub
get_exact_term {
my
(
$self
,
$value
) =
@_
;
$self
->{
'_term'
} =
undef
;
$self
->run(
$value
)
if
$value
;
$self
->throw(
"Could not connect to the server"
)
unless
$self
->status eq
'COMPLETED'
;
return
$self
->result;
}
sub
run {
my
(
$self
,
$value
) =
@_
;
$self
->throw(
"Need a MeSH name or ID as an input [$value]"
)
if
ref
$value
;
$self
->_run(
$value
);
}
sub
_cgi_url {
my
(
$self
,
$field
,
$term
) =
@_
;
}
sub
_run {
my
(
$self
,
$value
) =
@_
;
$self
->throw(
'Need a value [$value]'
)
unless
$value
;;
$self
->status(
'TERMINATED_BY_ERROR'
);
if
(
$self
->{
'_webmodule'
} eq
'WWW::Mechanize'
) {
$self
->debug(
"using WWW::Mechanize...\n"
);
my
$agent
= WWW::Mechanize->new();
$agent
->get(
$self
->url);
$agent
->status == 200
or
$self
->
warn
(
"Could not connect to the server\n"
) and
return
;
$agent
->form_name(
'MB'
);
$agent
->field(
"term"
,
$value
);
if
(
$value
=~ /\w\d{6}/) {
$agent
->field(
"field"
,
'uid'
);
}
else
{
$agent
->field(
"field"
,
'entry'
);
}
$agent
->click(
"exact"
);
$self
->{
'_content'
} =
$agent
->content();
$self
->status(
'COMPLETED'
);
return
;
}
elsif
(
$self
->{
'_webmodule'
} eq
'Bio::WebAgent'
) {
$self
->debug(
"using LWP::UserAgent...\n"
);
my
$response
;
if
(
$value
=~ /\w\d{6}/) {
$self
->{
'_content'
} =
$response
=
eval
{
$self
->get(
$self
->_cgi_url(
'uid'
,
$value
) )
};
$self
->
warn
(
"Could not connect to the server\n"
) and
return
if
$@;
}
else
{
$self
->{
'_content'
} =
eval
{
$response
=
$self
->get(
$self
->_cgi_url(
'entry'
,
$value
) )
};
$self
->
warn
(
"Could not connect to the server\n"
) and
return
if
$@;
}
if
(
$response
->is_success) {
$self
->{
'_content'
} =
$response
->content;
$self
->status(
'COMPLETED'
);
}
return
;
}
else
{
$self
->debug(
"using Bio::Root::HTTPget...\n"
);
my
$agent
= Bio::Root::HTTPget->new();
if
(
$value
=~ /\w\d{6}/) {
$self
->{
'_content'
} =
eval
{
$agent
->get(
$self
->_cgi_url(
'uid'
,
$value
) )
};
$self
->
warn
(
"Could not connect to the server\n"
) and
return
if
$@;
}
else
{
$self
->{
'_content'
} =
eval
{
$agent
->get(
$self
->_cgi_url(
'entry'
,
$value
) )
};
$self
->debug(
"Could not connect to the server\n"
) and
return
if
$@;
}
$self
->status(
'COMPLETED'
);
}
}
sub
result {
my
(
$self
,
$value
) =
@_
;
$self
->throw(
"Could not retrive results"
)
unless
$self
->status(
'COMPLETED'
);
return
$self
->{
'_content'
}
if
$value
&&
$value
eq
'raw'
;
$_
=
$self
->{
'_content'
};
$self
->debug(
substr
(
$_
, 0, 100) .
"\n"
);
my
(
$id
) = m|Unique \s+ ID </TH>
<TD (?: \s+ [^>]+ )? >
(.*?)
</TD> |ix;
my
(
$name
) = m|MeSH \s+ Heading </TH>
<TD (?: \s+ [^>]+ )? >
(.*?)
</TD> |ix;
my
(
$desc
) = m|Scope \s+ Note </TH>
<TD (?: \s+ [^>]+ )? >
(.*?)
</TD>|isx;
$self
->throw(
"No description returned: $_"
)
unless
defined
$desc
;
$desc
=~ s/<.*?>//sg;
$desc
=~ s/(?:\r)?\n/ /g;
$desc
=~ s/\( +/\(/g;
$desc
=~ s/ {2,}/ /g;
my
$term
= Bio::Phenotype::MeSH::Term->new(
-id
=>
$id
,
-name
=>
$name
,
-description
=>
$desc
);
my
(
$trees
) =
$self
->{
'_content'
} =~ /MeSH Tree Structures(.*)/s;
while
(m|Entry Term</TH><TD(?: [^>]+)?>(.*?)</TD>|ig) {
$term
->add_synonym($1);
$self
->debug(
"Synonym: |$1|\n"
);
}
foreach
(
split
/<HR>/i,
$trees
) {
next
unless
/
$name
/;
s/<TD.*?>/ /sgi;
s/<.*?>//sg;
s/
 
;/ /sg;
my
(
$treeno
) = /
$name
\[([^]]+)]/;
my
(
$parent_treeno
) =
$treeno
=~ /(.*)\.\d{3}/;
my
(
$parent
) = /\n +(\w.+) \[
$parent_treeno
\]/;
my
$twig
= Bio::Phenotype::MeSH::Twig->new(
-parent
=>
$parent
);
$term
->add_twig(
$twig
);
$self
->debug(
"Parent: |$parent|\n"
);
while
(/\n +(\w.+) \[
$treeno
\./g ) {
$twig
->add_child($1);
$self
->debug(
"Child: |$1|\n"
);
}
while
(/\n +(\w.+) \[
$parent_treeno
\./g ) {
next
if
$name
eq $1;
$twig
->add_sister($1);
$self
->debug(
"Sister: |$1|\n"
);
}
}
return
$term
;
}
1;