I have a fasta file which looks like this

>ENST00000632684.1 cdna chromosome:GRCh38:7:142786213:142786224:1 gene:ENSG00000282431.1 gene_biotype:TR_D_gene transcript_biotype:TR_D_gene gene_symbol:TRBD1 description:T cell receptor beta diversity 1 [Source:HGNC Symbol;Acc:HGNC:12158]
>ENST00000434970.2 cdna chromosome:GRCh38:14:22439007:22439015:1 gene:ENSG00000237235.2 gene_biotype:TR_D_gene transcript_biotype:TR_D_gene gene_symbol:TRDD2 description:T cell receptor delta diversity 2 [Source:HGNC Symbol;Acc:HGNC:12255]

I want to extract the gene_symbol and description. But unfortunately the description has spaces in between and I am not able to extract the complete description.

I have tried this

cat Homo_sapiens.GRCh38.cdna.all.fa | grep ">" | cut -f 7,8 -d" "  > Human_Annotations

but this gives me an output like this, where the description is broken.

gene_symbol:TRBD1 description:T
gene_symbol:TRDD2 description:T

I want output like this

TRBD1 T cell receptor beta diversity 1
TRDD2 T cell receptor delta diversity 2

Try something like this:

cat ... | sed -n '/^>/ { s/.*description: *//; s/\[.*//; p; }'

(Untested as I’m on mobile.)

There are more elegant ways; for instance an Awk loop would be most flexible.

  • This works but cat isn't needed. – Nasir Riley Sep 19 at 12:43

Using awk:

awk -F ':' '/^>/ { sub(" .*",    "", $10)
                   sub(" \\[.*", "", $11)
                   print $10, $11 }' file.fa

The data that you'd like to extract is the first word in the 10th field and everything up to the [ in the 11th field of each header line, if the fields are :-separated.

The code strips everything off from the first space in the 10th field, and everything after the [ in the 11th field (including the [ and the preceding space).

The modified 10th and 11th fields are then printed.

The output given the data in the question:

TRBD1 T cell receptor beta diversity 1
TRDD2 T cell receptor delta diversity 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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