0
$\begingroup$

I would like to create a URL that takes input from different arrays to generate a search. The data I would like to retrieve is from NCBI. Below you can find the code I wrote. But I can't get it to work; I am stuck. Thank you in advance.

I should have something like this as result but i got nothing. https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d

$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
$db = "gene";
$gene_symbols = array("A20","HBB","ABO");
$species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae");
$links = array();
foreach ($gene_symbols as $key => $GS){
    $SP = $species[$key];
    $query = "$GS AND $SP [orgn] AND alive";
    $esearch = "$eutils/esearch.fcgi?db=$db&retmode=xml&retmax=10&sort=relevance&term=";
    #echo $esearch;
    $links[] = get($esearch.$query);
}
foreach($links as $lk){
    xml = simplexml_load_file($lk) or die("feed not loading");
}
var_dump(xml);

As output I got nothing.

But I would like to have this: https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d And this for every gene. For some reason it put them all after each other and the foreach loop doesn't seem to work. I got no errors.

I run this code in php using visual studio code. The result should be visualised in the webbrowser (http://localhost/test.php).

$\endgroup$
7
  • $\begingroup$ Are you getting no output at all or simply the wrong output? $\endgroup$
    – Devon Ryan
    Apr 27, 2018 at 9:59
  • $\begingroup$ Please edit your question and i) explain how you run this code and on what system; ii) tell us what language it is written in. Is that PHP? iii) Most importantly, explain exactly how it fails. Any errors? Any data at all? $\endgroup$
    – terdon
    Apr 27, 2018 at 10:04
  • $\begingroup$ Can you print the links to see if they are valid? That would narrow down where the problem is $\endgroup$ Apr 27, 2018 at 10:23
  • $\begingroup$ As output I get this:eutils.ncbi.nlm.nih.gov/entrez/eutils/… But I would like to have this: eutils.ncbi.nlm.nih.gov/entrez/eutils/… And this for every gene. For some reason it doesn't add the term and the foreach loop doesn't seem to work. I got no errors. I run this code in php using visual studio code. The result should be visualised in the webbrowser (localhost/test.php). $\endgroup$
    – Nemo
    Apr 27, 2018 at 11:05
  • $\begingroup$ The output you linked to is an error: Invalid db name specified: bccmtest. Your code shows you are setting the db to gene, so what do you get when you run the actual code from your question? $\endgroup$
    – terdon
    Apr 27, 2018 at 11:14

3 Answers 3

0
$\begingroup$

I don't know PHP, so I'm sure there are better ways of doing this, but you had various issues in your code. This seems to be a working version of what you're after:

<?php
$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils"; $db = "gene"; $gene_symbols = array("A20","HBB","ABO");

$species = array("homo%20sapiens", "Escherichia%20coli","Saccharomyces%20cerevisiae");
$links = array();

foreach ($gene_symbols as $key => $GS){ 
  $SP = $species[$key];
  $query = "$GS%20AND%20$SP%20[orgn]%20AND%20alive";

  $esearch = "$eutils/esearch.fcgi?db=$db&retmode=xml&retmax=10&sort=relevance&term=";

  echo "$esearch$query\n";
  $links[] = file_get_contents($esearch.$query);
  foreach($links as $lk){
    echo "|------ $lk ----|";
    $xml = simplexml_load_string($lk) or die("feed not loading");
    var_dump($xml);
  }
}
?>

There doesn't seem to be a get() function in PHP, you want file_get_contents. Also, your loops were nested incorrectly and you needed var_dump($xml) to dump the variable $xml not var_dump(xml); which tries to dump the string xml. You're not getting a file here but a string, so you need simplexml_load_string. Finally, you can't have unescaped whitespace in URLs.

Next time, the way to debug this sort of thing is to have it print the URL it is generating and try to visit it manually. You would have seen the spaces and noticed that the working example you show uses %20 instead of space.

$\endgroup$
0
$\begingroup$

I don't know anything about php, but when I copy your code into here it isn't happy about the variable name $query.

This code appears to produce valid urls, but I can't make the get part work:

$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils"; $db = "gene";$gene_symbols = array("A20","HBB","ABO");
$species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae"); $links = array();

foreach ($gene_symbols as $key => $GS){ 
   $SP = $species[$key];
   $SearchTerm = "$GS AND $SP [orgn] AND alive";

   $esearch = "$eutils/esearch.fcgi?db=$db&retmode=xml&retmax=10&sort=relevance&term=";

   echo $esearch.$SearchTerm;
}

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20 AND homo sapiens [orgn] AND alivehttp://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=HBB AND Escherichia coli [orgn] AND alivehttp://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=ABO AND Saccharomyces cerevisiae [orgn] AND alive
$\endgroup$
0
$\begingroup$
$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";

   $databank = "gene";
   $gene_symbols = array("A20","HBB","ABO");

   $species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae");

foreach($gene_symbols as $value => $GS){
    $Species = $species[$value];


    $esearch = "$GS%AND%alive[prop]%AND+$Species[orgn]";
    $esearch_test = "$eutils?db=$databank&retmode=xml&retmax=10&sort=relevance&term=$esearch";
    $result = file_get_contents($esearch_test);
    echo "Query: " . $esearch_test. "<br \>\n";
    echo "Result: " . $result . "<br \>\n";

    $xml = simplexml_load_string($result);
    echo "Count:<br \>\n";
    echo $xml->Count;
    $C =  $xml->Count;
    echo $C;
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.