Basically a GenBank file consists of gene entries (announced by 'gene') followed by its corresponding 'CDS' entry (only one per gene) like the two shown here below. I would like to extract part of the data from the input file shown below according to the following rules and print it in the terminal.
There are two blocks of gene data shown below. I want to extract part of both blocks.
The extracted text for each block starts with a line that contains spaces at the beginning of the line followed by
gene
The extracted text for each block ends with a line that contains
/db_xref="GeneID
The perl and awk tags are just suggestions. An answer can use a different program(s).
Input file (source: biotech):
gene complement(8972..9094)
/locus_tag="HAPS_0004"
/db_xref="GeneID:7278619"
CDS complement(8972..9094)
/locus_tag="HAPS_0004"
/codon_start=1
/transl_table=11
/product="hypothetical protein"
/protein_id="YP_002474657.1"
/db_xref="GI:219870282"
/db_xref="GeneID:7278619"
/translation="MYYKALAHFLPTLSTMQNILSKSPLSLDFRLLFLAFIDKR"
gene 9632..11416
/gene="frdA"
/locus_tag="HAPS_0005"
/db_xref="GeneID:7278620"
CDS 9632..11416
/gene="frdA"
/locus_tag="HAPS_0005"
/note="part of four member fumarate reductase enzyme
complex FrdABCD which catalyzes the reduction of fumarate
to succinate during anaerobic respiration; FrdAB are the
catalytic subcomplex consisting of a flavoprotein subunit
and an iron-sulfur subunit, respectively; FrdCD are the
membrane components which interact with quinone and are
involved in electron transfer; the catalytic subunits are
similar to succinate dehydrogenase SdhAB"
/codon_start=1
/transl_table=11
/product="fumarate reductase flavoprotein subunit"
/protein_id="YP_002474658.1"
/db_xref="GI:219870283"
/db_xref="GeneID:7278620"
/translation="MQTVNVDVAIVGAGGGGLRAAIAAAEANPNLKIALISKVYPMRS
HTVAAEGGAAAVAKEEDSYDKHFHDTVAGGDWLCEQDVVEYFVEHSPVEMTQLERWGC
PWSRKADGDVNVRRFGGMKIERTWFAADKTGFHLLHTLFQTSIKYPQIIRFDEHFVVD
ILVDDGQVRGCVAMNMMEGTFVQINANAVVIATGGGCRAYRFNTNGGIVTGDGLSMAY
RHGVPLRDMEFVQYHPTGLPNTGILMTEGCRGEGGILVNKDGYRYLQDYGLGPETPVG
KPENKYMELGPRDKVSQAFWQEWRKGNTLKTAKGVDVVHLDLRHLGEKYLHERLPFIC
ELAQAYEGVDPAKAPIPVRPVVHYTMGGIEVDQHAETCIKGLFAVGECASSGLHGANR
LGSNSLAELVVFGKVAGEMAAKRAVEATARNQAVIDAQAKDVLERVYALARQEGEESW
SQIRNEMGDSMEEGCGIYRTQESMEKTVAKIAELKERYKRIKVKDSSSVFNTDLLYKI
ELGYILDVAQSISSSAVERKESRGAHQRLDYVERDDVNYLKHTLAFYNADGTPTIKYS
DVKITKSQPAKRVYGAEAEAQEAAAKKE"
Desired output:
gene complement(8972..9094)
/locus_tag="HAPS_0004"
/db_xref="GeneID:7278619"
CDS complement(8972..9094)
/locus_tag="HAPS_0004"
/codon_start=1
/transl_table=11
/product="hypothetical protein"
/protein_id="YP_002474657.1"
/db_xref="GI:219870282"
/db_xref="GeneID:7278619"
gene 9632..11416
/gene="frdA"
/locus_tag="HAPS_0005"
/db_xref="GeneID:7278620"
CDS 9632..11416
/gene="frdA"
/locus_tag="HAPS_0005"
/note="part of four member fumarate reductase enzyme
complex FrdABCD which catalyzes the reduction of fumarate
to succinate during anaerobic respiration; FrdAB are the
catalytic subcomplex consisting of a flavoprotein subunit
and an iron-sulfur subunit, respectively; FrdCD are the
membrane components which interact with quinone and are
involved in electron transfer; the catalytic subunits are
similar to succinate dehydrogenase SdhAB"
/codon_start=1
/transl_table=11
/product="fumarate reductase flavoprotein subunit"
/protein_id="YP_002474658.1"
/db_xref="GI:219870283"
/db_xref="GeneID:7278620"
I tried using pcregrep --multiline .*'START-SEARCH-TERM.*(\n|.)*END-SEARCH-TERM' path/to/SOURCE-FILE
. I installed pcregrep (grep utility that uses Perl-style regexps) in Ubuntu with sudo apt install pcregrep
. I also installed Biopython with sudo apt install python3-biopython
and ran the Simple GenBank parsing example from Biopython Tutorial and Cookbook.