# Phylum only abundance table

**URL:** <https://forum.biobakery.org/t/phylum-only-abundance-table/1044>\
**Category:** MetaPhlAn\
**Created:** [September 18, 2020, 9:48am UTC](https://forum.biobakery.org/t/phylum-only-abundance-table/1044 "2020-09-18T09:48:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mars](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/mars/32/1295_2.png) [@mars](https://forum.biobakery.org/u/mars)\
**Post date:** [September 18, 2020, 9:48am UTC](https://forum.biobakery.org/t/phylum-only-abundance-table/1044/1 "2020-09-18T09:48:09Z")

</div>

Hi,  
I am new to this. I used the following command to get the specie only abundance table as mentioned in the tutorial.

```auto
grep -E "s __|clade" merged_abundance_table.txt | sed 's/^.*s__ //g' | cut -f1,3-8 | sed -e 's/clade_name/body_site/g' > merged_abundance_table_species.txt

```

But I am also interested in getting the phylum only abundance table. Can someone with expertise in shell help me to modify the above script in order to get phylum only abundance table?

many thanks in advance.

---

<div class="post-metadata">

**Author:** ![kescobo](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/kescobo/32/47_2.png) [@kescobo](https://forum.biobakery.org/u/kescobo)\
**Post date:** [September 18, 2020, 2:12pm UTC](https://forum.biobakery.org/t/phylum-only-abundance-table/1044/2 "2020-09-18T14:12:06Z")

</div>

> [@mars](#):
>
> But I am also interested in getting the phylum only abundance table.

The way I do this is to first `grep` the desired level, and then `grep -v` the next lowest level. The `-v` does an “inverted” match. So for example, if you want just phylum, you’d do

```auto
$ grep "p __|clade" merged_abundance_table.txt | grep -v "c__" | # other stuff...

```

The `grep -E "p__|clade"` matches every line that contains the phylum (and the header, which is what `|clade` is for), and then `grep -v "c__"` matches every line that _doesn’t_ have the class.

---

<div class="post-metadata">

**Author:** ![mars](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/mars/32/1295_2.png) [@mars](https://forum.biobakery.org/u/mars)\
**Post date:** [September 19, 2020, 8:29am UTC](https://forum.biobakery.org/t/phylum-only-abundance-table/1044/3 "2020-09-19T08:29:10Z")

</div>

I am so sorry… can you please also write the remaining part of the code? like what #other stuff… i should write? I am new to shell. I tried the first part but it doesn’t work for me. I get an empty file…

---

<div class="post-metadata">

**Author:** ![kescobo](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/kescobo/32/47_2.png) [@kescobo](https://forum.biobakery.org/u/kescobo)\
**Post date:** [September 21, 2020, 1:49pm UTC](https://forum.biobakery.org/t/phylum-only-abundance-table/1044/4 "2020-09-21T13:49:30Z")

</div>

I was thinking all the stuff you had before (from `sed` on), though if you’re getting an empty file, there’s some other problem, I think.

With these sorts of built-up shell commands, it’s typically a good idea to do them in pieces. The `|` is passing the output from the previous command into the following command. So for example, if you start with `cat merged_abundance_table.txt`, you’ll see the whole file printed to the screen (if the output is really long, you can do something like `| head -20` at the end of each of these commands, which will only take the first 20 lines). Then add `| grep "p__|clade"`\*, and the output should change (in this case, you will only see lines that have `p__` or `clade`). Then add on `| grep -v "c __"`, and you should see any line with `c__ ` go away. Just keep tacking stuff on…

* * *

\*In writing this, I think I found the problem, which is that I didn’t include the `-E` flag in my example above, which might cause it to look for lines matching literally `"p __|clade"` rather than `p__ ` OR `clade`. The `-E` flag gives `grep` more regex symbols to work with. Anyway, if you follow my procedure above, you’d see no output when you add that piece on, which should be a clue that that’s where the problem is.

```auto
$ grep -E "p __|clade" merged_abundance_table.txt | grep -v "c__" | sed 's/^.*s__//g' | cut -f1,3-8 | sed -e 's/clade_name/body_site/g' > merged_abundance_table_species.txt

```

Should do it? But regardless, I recommend you build it up piece by piece and see how each pipe changes things. Also - [https://explainshell.com/](https://explainshell.com/) is a godsend. Plus, keep in mind that even people that use the shell regularly are constantly looking up syntax. You’ll get there, just keep practicing!

---

<div class="post-metadata">

**Author:** ![mars](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.biobakery.org/mars/32/1295_2.png) [@mars](https://forum.biobakery.org/u/mars)\
**Post date:** [September 24, 2020, 6:31am UTC](https://forum.biobakery.org/t/phylum-only-abundance-table/1044/5 "2020-09-24T06:31:02Z")

</div>

Great. This was very helpful. Thank you sooo much kescobo for your help!

Cheers!
