<p>You could wrap the call too <code>entrez_summary</code> in a loop.<br />
For example to cut the data into chunks of five genes:</p>
<pre><code>gene.vector # this would be the genes you are interested in
gene.chunks &lt;- ceiling(length(gene.vector) / 5)
# initializing an empty list
summary.list &lt;- vector(mode = &quot;list&quot;, length = gene.chunks)
for(i in gene.chunks){
   # select the ith set of five genes from your gene vector
   goi &lt;- gene.vector[(i - 1) * 5 + 1:5]
   # if there are less than five elements there will be NAs that need to be removed
   goi &lt;- goi[!is.na(goi)]
   # adding your code 
   summary.list[[i]] &lt;- org.Mm.eg.db::org.Mm.egSYMBOL2EG %&gt;% 
     as.data.frame() %&gt;% 
     filter(gene_id %in% goi) %&gt;% # here the current five genes are selected
     pull(gene_id) %&gt;% 
     rentrez::entrez_summary(id = ., db = &quot;gene&quot;) %&gt;%
     { do.call(&quot;rbind&quot;, .) } %&gt;%
     as.data.frame() %&gt;%
     select(name, description, summary)
  
   Sys.sleep(10) # wait for 10 seconds
}

</code></pre>
<p>There will also be a way to do this using <code>dplyr</code> <code>group</code>. In this case the loop is a little easier to debug.</p>
<p>I added <code>Sys.sleep</code> at the end to reduce the frequency with which you are making requests from entrez. If you are posting too many requests in a short time this will be like a DDOS attack. You (and potentially your whole IP range including other people at your institution) could get banned from the service for a couple of hours. Maybe try it with a subset first.</p>
<p>If you really want to get the information for all mouse genes this probably will be rather slow. Alternatively, you can go to the entrez webpage and try to download the information as a list. Unfortunately, I don't know exactly where you will find this information.</p>

CC BY-SA 4.0
2021-06-01T13:32:05.930Z
2021-06-01T13:32:05.930Z
PPK
<p>The following bash script downloads the summaries into an XML file.</p>
<pre class="lang-bash prettyprint-override"><code>#!/usr/bin/env bash

# This requires the ncbi tool `gene2xml`
# sudo apt install ncbi-tools-bin

DIR=&quot;download_cache&quot;
mkdir -p &quot;$DIR&quot;

URL=&quot;ftp://ftp.ncbi.nlm.nih.gov/gene/DATA/ASN_BINARY/Mammalia/Mus_musculus.ags.gz&quot;
wget -S -O - &quot;<span class="math-container">$URL" | gzip -d | gene2xml -b T -l T | gzip -9 &gt; "$</span>DIR/mm.xml.gz&quot;

URL=&quot;ftp://ftp.ncbi.nlm.nih.gov/gene/DATA/ASN_BINARY/Mammalia/Homo_sapiens.ags.gz&quot;
wget -S -O - &quot;<span class="math-container">$URL" | gzip -d | gene2xml -b T -l T | gzip -9 &gt; "$</span>DIR/hs.xml.gz&quot;
</code></pre>
<p>These XML files can be converted to tables along <a href="https://github.com/hsiaoyi0504/gene_dictionary/blob/4d84c5c4e4acb6103c6d22f8cc0e101e9535ab85/extract.py" rel="nofollow noreferrer">these lines</a>, viz.:</p>
<pre class="lang-py prettyprint-override"><code>
import xml.etree.ElementTree as ET
import gzip

from pathlib import Path

from tcga.utils import mkdir, unlist1, relpath


def parse(src):
    def find_text(e, path):
        e = e.findall(path)
        if e:
            return unlist1(e).text
        else:
            return &quot;&quot;

    yield ('gene_id', 'description', 'summary')

    with gzip.open(src, mode='r') as fd:
        e: ET.Element
        for (x, e) in ET.iterparse(fd, events=['end']):
            if e.tag == &quot;Entrezgene&quot;:
                gene_id = find_text(e, &quot;**/Gene-track_geneid&quot;)
                descptn = find_text(e, &quot;**/Gene-ref_desc&quot;)
                summary = find_text(e, &quot;Entrezgene_summary&quot;)

                yield (gene_id, descptn, summary)

                e.clear()


def main():
    for src in Path(__file__).parent.glob(&quot;download_cache/*.xml.gz&quot;):
        trg = mkdir(Path(__file__).with_suffix('')) / Path(src.stem).with_suffix('.tsv.gz')
        if trg.is_file():
            print(&quot;Skipping existing file:&quot;, relpath(trg))
        else:
            print(&quot;Writing:&quot;, relpath(trg))
            with gzip.open(trg, mode='wt') as fd:
                for t in parse(src):
                    print(*t, sep='\t', file=fd)


if __name__ == '__main__':
    main()
</code></pre>
<p>Btw., a table of vertebrate gene homology is available <a href="http://www.informatics.jax.org/downloads/reports/index.html" rel="nofollow noreferrer">here</a> (it's just a tab-separated table), and can be used to impute summaries by homology.</p>

CC BY-SA 4.0
2021-06-02T14:00:57.233Z
2021-06-02T14:00:57.233Z
user66081
<p>The following
(thx to <a href="https://bioinformatics.stackexchange.com/questions/2634/retrieve-detailed-gene-descriptions">these answers</a>)
retrieves the info, in particular a long summary (such as &quot;The membrane-associated protein encoded by this gene is a member of the superfamily of ATP-binding cassette... etc.&quot;), for a few mouse genes.</p>
<pre><code>library(dplyr)

org.Mm.eg.db::org.Mm.egSYMBOL2EG %&gt;% 
  as.data.frame() %&gt;% 
  pull(gene_id) %&gt;% 
  head() %&gt;%  # !!!
  rentrez::entrez_summary(id = ., db = &quot;gene&quot;) %&gt;%
  { do.call(&quot;rbind&quot;, .) } %&gt;%
  as.data.frame() %&gt;%
  select(name, description, summary)
</code></pre>
<p>Note the <code>head</code>. The protocol/server does not allow large queries. How can I get the same information (incl. the long summary) for all the mouse genes at once?</p>

CC BY-SA 4.0
2021-06-01T13:51:38.890Z
Chunk your query, then use [`purrr::insistently`](https://purrr.tidyverse.org/reference/insistently.html). No need for a loop, this can be vectorised. I also recommend against a fixed-length sleep; using an exponential backoff is more polite.
Konrad Rudolph
CC BY-SA 4.0
2021-06-02T14:04:58.310Z
Thanks, I'll certainly have occasion for this one.
user66081
<r><phylogenetics><entrez>
Long-form gene summary
# Long-form gene summary

(Asked by: username_0 on 2021-05-26)

The following
(thx to these answers)
retrieves the info, in particular a long summary (such as "The membrane-associated protein encoded by this gene is a member of the superfamily of ATP-binding cassette... etc."), for a few mouse genes.
library(dplyr)

org.Mm.eg.db::org.Mm.egSYMBOL2EG %>% 
  as.data.frame() %>% 
  pull(gene_id) %>% 
  head() %>%  # !!!
  rentrez::entrez_summary(id = ., db = "gene") %>%
  { do.call("rbind", .) } %>%
  as.data.frame() %>%
  select(name, description, summary)

Note the head. The protocol/server does not allow large queries. How can I get the same information (incl. the long summary) for all the mouse genes at once?

**Tags:** r, phylogenetics, entrez
> **Comments:**
> **username_1**: Chunk your query, then use [`purrr::insistently`](https://purrr.tidyverse.org/reference/insistently.html). No need for a loop, this can be vectorised. I also recommend against a fixed-length sleep; using an exponential backoff is more polite.  
> **username_0**: Thanks, I'll certainly have occasion for this one.  

---

## 2 answers

### Answer by username_2 on 2021-06-01. Score: 1

You could wrap the call too entrez_summary in a loop.
For example to cut the data into chunks of five genes:
gene.vector # this would be the genes you are interested in
gene.chunks <- ceiling(length(gene.vector) / 5)
# initializing an empty list
summary.list <- vector(mode = "list", length = gene.chunks)
for(i in gene.chunks){
   # select the ith set of five genes from your gene vector
   goi <- gene.vector[(i - 1) * 5 + 1:5]
   # if there are less than five elements there will be NAs that need to be removed
   goi <- goi[!is.na(goi)]
   # adding your code 
   summary.list[[i]] <- org.Mm.eg.db::org.Mm.egSYMBOL2EG %>% 
     as.data.frame() %>% 
     filter(gene_id %in% goi) %>% # here the current five genes are selected
     pull(gene_id) %>% 
     rentrez::entrez_summary(id = ., db = "gene") %>%
     { do.call("rbind", .) } %>%
     as.data.frame() %>%
     select(name, description, summary)
  
   Sys.sleep(10) # wait for 10 seconds
}


There will also be a way to do this using dplyr group. In this case the loop is a little easier to debug.
I added Sys.sleep at the end to reduce the frequency with which you are making requests from entrez. If you are posting too many requests in a short time this will be like a DDOS attack. You (and potentially your whole IP range including other people at your institution) could get banned from the service for a couple of hours. Maybe try it with a subset first.
If you really want to get the information for all mouse genes this probably will be rather slow. Alternatively, you can go to the entrez webpage and try to download the information as a list. Unfortunately, I don't know exactly where you will find this information.


---

### Answer by username_0 on 2021-06-02. Score: 1

The following bash script downloads the summaries into an XML file.
#!/usr/bin/env bash

# This requires the ncbi tool `gene2xml`
# sudo apt install ncbi-tools-bin

DIR="download_cache"
mkdir -p "$DIR"

URL="ftp://ftp.ncbi.nlm.nih.gov/gene/DATA/ASN_BINARY/Mammalia/Mus_musculus.ags.gz"
wget -S -O - "$URL" | gzip -d | gene2xml -b T -l T | gzip -9 > "$DIR/mm.xml.gz"

URL="ftp://ftp.ncbi.nlm.nih.gov/gene/DATA/ASN_BINARY/Mammalia/Homo_sapiens.ags.gz"
wget -S -O - "$URL" | gzip -d | gene2xml -b T -l T | gzip -9 > "$DIR/hs.xml.gz"

These XML files can be converted to tables along these lines, viz.:

import xml.etree.ElementTree as ET
import gzip

from pathlib import Path

from tcga.utils import mkdir, unlist1, relpath


def parse(src):
    def find_text(e, path):
        e = e.findall(path)
        if e:
            return unlist1(e).text
        else:
            return ""

    yield ('gene_id', 'description', 'summary')

    with gzip.open(src, mode='r') as fd:
        e: ET.Element
        for (x, e) in ET.iterparse(fd, events=['end']):
            if e.tag == "Entrezgene":
                gene_id = find_text(e, "**/Gene-track_geneid")
                descptn = find_text(e, "**/Gene-ref_desc")
                summary = find_text(e, "Entrezgene_summary")

                yield (gene_id, descptn, summary)

                e.clear()


def main():
    for src in Path(__file__).parent.glob("download_cache/*.xml.gz"):
        trg = mkdir(Path(__file__).with_suffix('')) / Path(src.stem).with_suffix('.tsv.gz')
        if trg.is_file():
            print("Skipping existing file:", relpath(trg))
        else:
            print("Writing:", relpath(trg))
            with gzip.open(trg, mode='wt') as fd:
                for t in parse(src):
                    print(*t, sep='\t', file=fd)


if __name__ == '__main__':
    main()

Btw., a table of vertebrate gene homology is available here (it's just a tab-separated table), and can be used to impute summaries by homology.


---

