Skip to main content

genbank

Python code to work with Genbank files

This repo contains several classes to help work with Genbank files

The flow goes:

File -> Locus -> Feature

To use:

from genbank.file import File

file = File('infile.gbk')
for locus in file:
	print(llocus.name())
	for feature in locus:
		print(feature)

You can also build a Locus object from the ground up:

from genbank.locus import Locus
locus = Locus('test', 'actgactgatcgtagctagc')
# then add a feature by parsing text of a genbank feature
locus.read_feature('  CDS  1..9')
# or add manually by specifing the type,strand,location
locus.add_feature('CDS',+1,[['6','20']])
locus.write('genbank')

which gives:

LOCUS       test                      20 bp
FEATURES             Location/Qualifiers
     CDS             1..9
     CDS             6..20
ORIGIN
        1 actgactgat cgtagctagc
//

This package also allows you to perform various conversions on a given genome file:

$ genbank.py tests/phiX174.gbk -f tabular
'phiX174'	'CDS'	(('100', '627'),)	{'gene': "G"}
'phiX174'	'CDS'	(('636', '1622'),)	{'gene': "H"}
'phiX174'	'CDS'	(('1659', '3227'),)	{'gene': "A"}
'phiX174'	'CDS'	(('2780', '3142'),)	{'gene': "B"}
'phiX174'	'CDS'	(('3142', '3312'),)	{'gene': "K"}

$ genbank.py tests/phiX174.gbk -f fasta
>phiX174
gtgtgaggttataacgccgaagcggtaaaaattttaatttttgccgctgagggg
ttgaccaagcgaagcgcggtaggttttctgcttaggagtttaatcatgtttcag

$ genbank.py tests/phiX174.gbk -f fna
>phiX174_CDS_[100..627] [gene="G"]
atgtttcagacttttatttctcgccataattcaaactttttttctgataag
>phiX174_CDS_[636..1622] [gene="H"]
atgtttggtgctattgctggcggtattgcttctgctcttgctggtggcgcc
>phiX174_CDS_[1659..3227]

$ genbank.py tests/phiX174.gbk -f faa
>phiX174_CDS_[100..627] [gene="G"]
MFQTFISRHNSNFFSDKLVLTSVTPASSAPVLQTPKATSSTLYFDSLTVNA
>phiX174_CDS_[636..1622] [gene="H"]
MFGAIAGGIASALAGGAMSKLFGGGQKAASGGIQGDVLATDNNTVGMGDAG
>phiX174_CDS_[1659..3227] [gene="A"]

$ genbank.py tests/phiX174.gbk -f coverage
phiX174	1.1168

You can also slice the locus to a specified range and only the nucleotides and features that occur within the slice are kept. The command to take the first two hundred bases of the phiX174 genome is shown below.

$ genbank.py tests/phiX174.gbk --slice 1..200
LOCUS       phiX174                  200 bp    DNA             PHG
DEFINITION  phiX174
FEATURES             Location/Qualifiers
     rep_origin      13..56
     CDS             100..>200
                     /gene="G"
                     /note="merged"
ORIGIN
        1 gtgtgaggtt ataacgccga agcggtaaaa attttaattt ttgccgctga ggggttgacc
       61 aagcgaagcg cggtaggttt tctgcttagg agtttaatca tgtttcagac ttttatttct
      121 cgccataatt caaacttttt ttctgataag ctggttctca cttctgttac tccagcttct
      181 tcggcacctg ttttacagac
//

The slice arguement can be paired with all the other output format options:

$ genbank.py tests/phiX174.gbk --slice 1..200 -f coverage
0.51

You can also easily edit features by running multiple commands.

Print out the features of a specified key:tag into a file

$ genbank.py tests/phiX174.gbk -k CDS:gene > labels.tsv

Change the H of the second gene to something more informative: (ideally you will have columns from other sources, like excel)

perl -pi -e 's/H/Minor spike/' labels.tsv

Edit all the features of a specified key:tag with the updated labels:

$ genbank.py tests/phiX174.gbk -e CDS:gene < labels.tsv | head
LOCUS       phiX174                 5386 bp    DNA      PHG
FEATURES             Location/Qualifiers
     source          1..5386
     rep_origin      13..56
     CDS             100..627
                     /gene="G"
     CDS             636..1622
                     /gene="Minor spike"
     CDS             1659..3227

Metadata

Release files for genbank 0.121

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for genbank 0.121
File Size Uploaded
genbank-0.121.tar.gz 30.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for genbank 0.121
File Interpreter ABI Platform
genbank-0.121-py3-none-any.whl Python 3 none any Details

Total release size: 60.5 kB

Release files / genbank-0.121.tar.gz

Download URL genbank-0.121.tar.gz
Size 30.3 kB
Tags Source
SHA-256 checksum
How to use checksums
959df344018f8e104ef6d3c9d78f576228cb4997f4fa186050cfa69b231cb5af
BLAKE2b-256 checksum
How to use checksums
f7b851598260fadedfe04ad26195597148bd6c08f8e2f7178e73fc3de16683b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.17

Release files / genbank-0.121-py3-none-any.whl

Download URL genbank-0.121-py3-none-any.whl
Size 30.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2be75456a430c1d87bb3963906d36b085a221bea3184484845b1bc5e7b43513
BLAKE2b-256 checksum
How to use checksums
c39249e1c0ce719e85e1687dee47b1ed17ca6655514fc6106a4d3a95ec51996e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.121 This release

2 release files

0.120

2 release files

0.119

2 release files

0.118

2 release files

0.110

2 release files

0.109

2 release files

0.105

2 release files

0.103

2 release files

0.101

2 release files

0.100

2 release files

0.96

2 release files

0.81

2 release files

0.75

2 release files

0.69

2 release files

0.61

2 release files

0.60

2 release files

0.55

2 release files

0.53

2 release files

0.39

2 release files

0.38

2 release files

0.37

2 release files

0.33

2 release files

0.22

2 release files

0.12

2 release files

0.6

2 release files

0.5

2 release files

0.3

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page