<p>Not exactly what you were looking for but may be you can use something like this?</p>
<pre><code>ncbi2=NC_002021
efetch -db protein -format gpc -id &quot;$ncbi2&quot; \
    | xtract -pattern INSDSeq_feature-table \
        -group INSDFeature \
        -if INSDFeature_key -equals 'mat_peptide' \
        -tab '\n' \
        -element INSDFeature_location,INSDQualifier_value \
    | grep 'Segment 2'
</code></pre>
<p>For explorations of the INSDSet XMLs, <code>xtract</code> has <code>-insd</code> option that you can use. In this particular case, you can do something like this:</p>
<pre><code>efetch -db nuccore -id &quot;$ncbi2&quot; -format gpc \
    | xtract -insd mat_peptide product peptide
</code></pre>
<p>For more information, checkout <a href="https://www.ncbi.nlm.nih.gov/books/NBK179288/#_chapter6_Sequence_Records_" rel="nofollow noreferrer">EDirect documentation</a> and some  <a href="https://www.ncbi.nlm.nih.gov/books/NBK179288/#_chapter6_Examples_" rel="nofollow noreferrer">example queries</a></p>

CC BY-SA 4.0
2019-05-16T12:22:29.157Z
Thank you its perfect. Stacked together in a small subroutine and parsed with a bit of regex its fantastic. "esearch -query" is used to obtain the accession numbers.
M__
CC BY-SA 4.0
2019-05-16T11:00:23.290Z
2020-12-17T16:28:32.750Z
2020-12-17T16:28:32.750Z
vkkodali
<p>I'm using efetch to dig out a middle &quot;envelope&quot; protein within a viral genome specifically within fasta format. The code below works but may not be robust for an entire database. Thus I want to replace the two &quot;perl&quot; lines with formal code within the Entrez package. I tried using <code>efilter -query</code> but couldn't get it to work. Genome positions are one approach but are not absolute from one virus to another. Rewriting this may be the best option.</p>

<pre class="lang-none prettyprint-override"><code>ncbi2=NC_002021
efetch -db protein -format gpc -id &quot;<span class="math-container">$ncbi2" |\
xtract -pattern INSDSeq -element INSDQualifier_value |\
perl -p -e 's/^(.+?)\s+taxon.*.*\s+\b([A-Z]{300}[A-Z]+).*/&gt;$</span>1\n$2/'|\
perl -p -e &quot;s/[\t \;\/]+/_/g&quot;
</code></pre>
<p>Output</p>
<pre class="lang-none prettyprint-override"><code>&gt;InfluenzaAvirus_A_PuertoRico_8_1934
MDVNPTLLFLKVPAQNAISTTFPYTGD.....
</code></pre>
<p>I will provide a more detailed explanation shortly.
Any ideas welcome.</p>

CC BY-SA 4.0
2019-05-14T12:33:16.047Z
In the meantime, you could streamline the perl a little to `| perl -pe 's/^(.+?)\s+taxon.*envelope protein.*\s+\b([A-Z]{300}[A-Z]+).*nonstructural protein 1.*/>$1\n$2/; s/[\t ;\/]+/_/g`. There's no need to process the file twice and you also don't need to escape the `;` inside a character class.
terdon
CC BY-SA 4.0
2019-05-14T13:16:57.553Z
Hi @terdon, yes and thanks now you've mentioned it, it's obvious.
M__
CC BY-SA 4.0
2019-05-15T00:56:49.173Z
Not exactly what you were looking for but may be you can use something like this? `efetch -db protein -format gpc -id "$ncbi2" | xtract -pattern INSDSeq_feature-table -group INSDFeature -if INSDFeature_key -equals 'mat_peptide' -tab '\n' -element INSDFeature_location,INSDQualifier_value | grep 'envelope'`
vkkodali
CC BY-SA 4.0
2019-05-15T11:48:51.660Z
Perfect it works a treat @vkkodali. Huge thanks. A small amount of regex will finish the fasta format. Maybe you would like to provide a formal answer so I can sign this question as complete?
M__
<covid-19><bash><sars-cov-2><perl><efetch>
Multi-line efetch digging
# Multi-line efetch digging

(Asked by: username_0 on 2019-05-13)

I'm using efetch to dig out a middle "envelope" protein within a viral genome specifically within fasta format. The code below works but may not be robust for an entire database. Thus I want to replace the two "perl" lines with formal code within the Entrez package. I tried using efilter -query but couldn't get it to work. Genome positions are one approach but are not absolute from one virus to another. Rewriting this may be the best option.
ncbi2=NC_002021
efetch -db protein -format gpc -id "$ncbi2" |\
xtract -pattern INSDSeq -element INSDQualifier_value |\
perl -p -e 's/^(.+?)\s+taxon.*.*\s+\b([A-Z]{300}[A-Z]+).*/>$1\n$2/'|\
perl -p -e "s/[\t \;\/]+/_/g"

Output
>InfluenzaAvirus_A_PuertoRico_8_1934
MDVNPTLLFLKVPAQNAISTTFPYTGD.....

I will provide a more detailed explanation shortly.
Any ideas welcome.

**Tags:** covid-19, bash, sars-cov-2, perl, efetch
> **Comments:**
> **username_1**: In the meantime, you could streamline the perl a little to `| perl -pe 's/^(.+?)\s+taxon.*envelope protein.*\s+\b([A-Z]{300}[A-Z]+).*nonstructural protein 1.*/>$1\n$2/; s/[\t ;\/]+/_/g`. There's no need to process the file twice and you also don't need to escape the `;` inside a character class.  
> **username_0**: Hi @terdon, yes and thanks now you've mentioned it, it's obvious.  
> **username_2**: Not exactly what you were looking for but may be you can use something like this? `efetch -db protein -format gpc -id "$ncbi2" | xtract -pattern INSDSeq_feature-table -group INSDFeature -if INSDFeature_key -equals 'mat_peptide' -tab '\n' -element INSDFeature_location,INSDQualifier_value | grep 'envelope'`  
> **username_0**: Perfect it works a treat @vkkodali. Huge thanks. A small amount of regex will finish the fasta format. Maybe you would like to provide a formal answer so I can sign this question as complete?  

---

## 1 answers

### Answer by username_2 on 2019-05-16. Score: 2 | Accepted answer

Not exactly what you were looking for but may be you can use something like this?
ncbi2=NC_002021
efetch -db protein -format gpc -id "$ncbi2" \
    | xtract -pattern INSDSeq_feature-table \
        -group INSDFeature \
        -if INSDFeature_key -equals 'mat_peptide' \
        -tab '\n' \
        -element INSDFeature_location,INSDQualifier_value \
    | grep 'Segment 2'

For explorations of the INSDSet XMLs, xtract has -insd option that you can use. In this particular case, you can do something like this:
efetch -db nuccore -id "$ncbi2" -format gpc \
    | xtract -insd mat_peptide product peptide

For more information, checkout EDirect documentation and some  example queries

> **Comments:**
> **username_0**: Thank you its perfect. Stacked together in a small subroutine and parsed with a bit of regex its fantastic. "esearch -query" is used to obtain the accession numbers.  

---

