/
InsdcIO.py
1480 lines (1318 loc) · 55 KB
/
InsdcIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2007-2016 by Peter Cock. All rights reserved.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.
"""Bio.SeqIO support for the "genbank" and "embl" file formats.
You are expected to use this module via the Bio.SeqIO functions.
Note that internally this module calls Bio.GenBank to do the actual
parsing of GenBank, EMBL and IMGT files.
See Also:
International Nucleotide Sequence Database Collaboration
http://www.insdc.org/
GenBank
http://www.ncbi.nlm.nih.gov/Genbank/
EMBL Nucleotide Sequence Database
http://www.ebi.ac.uk/embl/
DDBJ (DNA Data Bank of Japan)
http://www.ddbj.nig.ac.jp/
IMGT (use a variant of EMBL format with longer feature indents)
http://imgt.cines.fr/download/LIGM-DB/userman_doc.html
http://imgt.cines.fr/download/LIGM-DB/ftable_doc.html
http://www.ebi.ac.uk/imgt/hla/docs/manual.html
"""
import warnings
from datetime import datetime
from Bio import BiopythonWarning
from Bio.Seq import UnknownSeq
from Bio.GenBank.Scanner import GenBankScanner, EmblScanner, _ImgtScanner
from Bio import SeqIO
from Bio import SeqFeature
from .Interfaces import SequenceIterator, SequenceWriter
# NOTE
# ====
# The "brains" for parsing GenBank, EMBL and IMGT files (and any
# other flat file variants from the INSDC in future) is in
# Bio.GenBank.Scanner (plus the _FeatureConsumer in Bio.GenBank)
# However, all the writing code is in this file.
class GenBankIterator(SequenceIterator):
"""Parser for GenBank files."""
def __init__(self, source):
"""Break up a Genbank file into SeqRecord objects.
Argument source is a file-like object opened in text mode or a path to a file.
Every section from the LOCUS line to the terminating // becomes
a single SeqRecord with associated annotation and features.
Note that for genomes or chromosomes, there is typically only
one record.
This gets called internally by Bio.SeqIO for the GenBank file format:
>>> from Bio import SeqIO
>>> for record in SeqIO.parse("GenBank/cor6_6.gb", "gb"):
... print(record.id)
...
X55053.1
X62281.1
M81224.1
AJ237582.1
L31939.1
AF297471.1
Equivalently,
>>> with open("GenBank/cor6_6.gb") as handle:
... for record in GenBankIterator(handle):
... print(record.id)
...
X55053.1
X62281.1
M81224.1
AJ237582.1
L31939.1
AF297471.1
"""
super().__init__(source, mode="t", fmt="GenBank")
def parse(self, handle):
"""Start parsing the file, and return a SeqRecord generator."""
records = GenBankScanner(debug=0).parse_records(handle)
return records
class EmblIterator(SequenceIterator):
"""Parser for EMBL files."""
def __init__(self, source):
"""Break up an EMBL file into SeqRecord objects.
Argument source is a file-like object opened in text mode or a path to a file.
Every section from the LOCUS line to the terminating // becomes
a single SeqRecord with associated annotation and features.
Note that for genomes or chromosomes, there is typically only
one record.
This gets called internally by Bio.SeqIO for the EMBL file format:
>>> from Bio import SeqIO
>>> for record in SeqIO.parse("EMBL/epo_prt_selection.embl", "embl"):
... print(record.id)
...
A00022.1
A00028.1
A00031.1
A00034.1
A00060.1
A00071.1
A00072.1
A00078.1
CQ797900.1
Equivalently,
>>> with open("EMBL/epo_prt_selection.embl") as handle:
... for record in EmblIterator(handle):
... print(record.id)
...
A00022.1
A00028.1
A00031.1
A00034.1
A00060.1
A00071.1
A00072.1
A00078.1
CQ797900.1
"""
super().__init__(source, mode="t", fmt="EMBL")
def parse(self, handle):
"""Start parsing the file, and return a SeqRecord generator."""
records = EmblScanner(debug=0).parse_records(handle)
return records
class ImgtIterator(SequenceIterator):
"""Parser for IMGT files."""
def __init__(self, source):
"""Break up an IMGT file into SeqRecord objects.
Argument source is a file-like object opened in text mode or a path to a file.
Every section from the LOCUS line to the terminating // becomes
a single SeqRecord with associated annotation and features.
Note that for genomes or chromosomes, there is typically only
one record.
"""
super().__init__(source, mode="t", fmt="IMGT")
def parse(self, handle):
"""Start parsing the file, and return a SeqRecord generator."""
records = _ImgtScanner(debug=0).parse_records(handle)
return records
class GenBankCdsFeatureIterator(SequenceIterator):
"""Parser for GenBank files, creating a SeqRecord for each CDS feature."""
def __init__(self, source):
"""Break up a Genbank file into SeqRecord objects for each CDS feature.
Argument source is a file-like object opened in text mode or a path to a file.
Every section from the LOCUS line to the terminating // can contain
many CDS features. These are returned as with the stated amino acid
translation sequence (if given).
"""
super().__init__(source, mode="t", fmt="GenBank")
def parse(self, handle):
"""Start parsing the file, and return a SeqRecord generator."""
return GenBankScanner(debug=0).parse_cds_features(handle)
class EmblCdsFeatureIterator(SequenceIterator):
"""Parser for EMBL files, creating a SeqRecord for each CDS feature."""
def __init__(self, source):
"""Break up a EMBL file into SeqRecord objects for each CDS feature.
Argument source is a file-like object opened in text mode or a path to a file.
Every section from the LOCUS line to the terminating // can contain
many CDS features. These are returned as with the stated amino acid
translation sequence (if given).
"""
super().__init__(source, mode="t", fmt="EMBL")
def parse(self, handle):
"""Start parsing the file, and return a SeqRecord generator."""
return EmblScanner(debug=0).parse_cds_features(handle)
def _insdc_feature_position_string(pos, offset=0):
"""Build a GenBank/EMBL position string (PRIVATE).
Use offset=1 to add one to convert a start position from python counting.
"""
if isinstance(pos, SeqFeature.ExactPosition):
return "%i" % (pos.position + offset)
elif isinstance(pos, SeqFeature.WithinPosition):
return "(%i.%i)" % (
pos.position + offset,
pos.position + pos.extension + offset,
)
elif isinstance(pos, SeqFeature.BetweenPosition):
return "(%i^%i)" % (
pos.position + offset,
pos.position + pos.extension + offset,
)
elif isinstance(pos, SeqFeature.BeforePosition):
return "<%i" % (pos.position + offset)
elif isinstance(pos, SeqFeature.AfterPosition):
return ">%i" % (pos.position + offset)
elif isinstance(pos, SeqFeature.OneOfPosition):
return "one-of(%s)" % ",".join(
_insdc_feature_position_string(p, offset) for p in pos.position_choices
)
elif isinstance(pos, SeqFeature.AbstractPosition):
raise NotImplementedError("Please report this as a bug in Biopython.")
else:
raise ValueError("Expected a SeqFeature position object.")
def _insdc_location_string_ignoring_strand_and_subfeatures(location, rec_length):
if location.ref:
ref = "%s:" % location.ref
else:
ref = ""
assert not location.ref_db
if (
isinstance(location.start, SeqFeature.ExactPosition)
and isinstance(location.end, SeqFeature.ExactPosition)
and location.start.position == location.end.position
):
# Special case, for 12:12 return 12^13
# (a zero length slice, meaning the point between two letters)
if location.end.position == rec_length:
# Very special case, for a between position at the end of a
# sequence (used on some circular genomes, Bug 3098) we have
# N:N so return N^1
return "%s%i^1" % (ref, rec_length)
else:
return "%s%i^%i" % (ref, location.end.position, location.end.position + 1)
if (
isinstance(location.start, SeqFeature.ExactPosition)
and isinstance(location.end, SeqFeature.ExactPosition)
and location.start.position + 1 == location.end.position
):
# Special case, for 11:12 return 12 rather than 12..12
# (a length one slice, meaning a single letter)
return "%s%i" % (ref, location.end.position)
elif isinstance(location.start, SeqFeature.UnknownPosition) or isinstance(
location.end, SeqFeature.UnknownPosition
):
# Special case for features from SwissProt/UniProt files
if isinstance(location.start, SeqFeature.UnknownPosition) and isinstance(
location.end, SeqFeature.UnknownPosition
):
# warnings.warn("Feature with unknown location", BiopythonWarning)
# return "?"
raise ValueError("Feature with unknown location")
elif isinstance(location.start, SeqFeature.UnknownPosition):
# Treat the unknown start position as a BeforePosition
return "%s<%i..%s" % (
ref,
location.nofuzzy_end,
_insdc_feature_position_string(location.end),
)
else:
# Treat the unknown end position as an AfterPosition
return "%s%s..>%i" % (
ref,
_insdc_feature_position_string(location.start, +1),
location.nofuzzy_start + 1,
)
else:
# Typical case, e.g. 12..15 gets mapped to 11:15
return (
ref
+ _insdc_feature_position_string(location.start, +1)
+ ".."
+ _insdc_feature_position_string(location.end)
)
def _insdc_location_string(location, rec_length):
"""Build a GenBank/EMBL location from a (Compound) FeatureLocation (PRIVATE).
There is a choice of how to show joins on the reverse complement strand,
GenBank used "complement(join(1,10),(20,100))" while EMBL used to use
"join(complement(20,100),complement(1,10))" instead (but appears to have
now adopted the GenBank convention). Notice that the order of the entries
is reversed! This function therefore uses the first form. In this situation
we expect the CompoundFeatureLocation and its parts to all be marked as
strand == -1, and to be in the order 19:100 then 0:10.
"""
try:
parts = location.parts
# CompoundFeatureLocation
if location.strand == -1:
# Special case, put complement outside the join/order/... and reverse order
return "complement(%s(%s))" % (
location.operator,
",".join(
_insdc_location_string_ignoring_strand_and_subfeatures(
p, rec_length
)
for p in parts[::-1]
),
)
else:
return "%s(%s)" % (
location.operator,
",".join(_insdc_location_string(p, rec_length) for p in parts),
)
except AttributeError:
# Simple FeatureLocation
loc = _insdc_location_string_ignoring_strand_and_subfeatures(
location, rec_length
)
if location.strand == -1:
return "complement(%s)" % loc
else:
return loc
class _InsdcWriter(SequenceWriter):
"""Base class for GenBank and EMBL writers (PRIVATE)."""
MAX_WIDTH = 80
QUALIFIER_INDENT = 21
QUALIFIER_INDENT_STR = " " * QUALIFIER_INDENT
QUALIFIER_INDENT_TMP = " %s " # 21 if %s is empty
FTQUAL_NO_QUOTE = (
"anticodon",
"citation",
"codon_start",
"compare",
"direction",
"estimated_length",
"mod_base",
"number",
"rpt_type",
"rpt_unit_range",
"tag_peptide",
"transl_except",
"transl_table",
)
def _write_feature_qualifier(self, key, value=None, quote=None):
if value is None:
# Value-less entry like /pseudo
self.handle.write("%s/%s\n" % (self.QUALIFIER_INDENT_STR, key))
return
if type(value) == str:
value = value.replace(
'"', '""'
) # NCBI says escape " as "" in qualifier values
# Quick hack with no line wrapping, may be useful for testing:
# self.handle.write('%s/%s="%s"\n' % (self.QUALIFIER_INDENT_STR, key, value))
if quote is None:
# Try to mimic unwritten rules about when quotes can be left out:
if isinstance(value, int) or key in self.FTQUAL_NO_QUOTE:
quote = False
else:
quote = True
if quote:
line = '%s/%s="%s"' % (self.QUALIFIER_INDENT_STR, key, value)
else:
line = "%s/%s=%s" % (self.QUALIFIER_INDENT_STR, key, value)
if len(line) <= self.MAX_WIDTH:
self.handle.write(line + "\n")
return
while line.lstrip():
if len(line) <= self.MAX_WIDTH:
self.handle.write(line + "\n")
return
# Insert line break...
for index in range(
min(len(line) - 1, self.MAX_WIDTH), self.QUALIFIER_INDENT + 1, -1
):
if line[index] == " ":
break
if line[index] != " ":
# No nice place to break...
index = self.MAX_WIDTH
assert index <= self.MAX_WIDTH
self.handle.write(line[:index] + "\n")
line = self.QUALIFIER_INDENT_STR + line[index:].lstrip()
def _wrap_location(self, location):
"""Split a feature location into lines (break at commas) (PRIVATE)."""
# TODO - Rewrite this not to recurse!
length = self.MAX_WIDTH - self.QUALIFIER_INDENT
if len(location) <= length:
return location
index = location[:length].rfind(",")
if index == -1:
# No good place to split (!)
warnings.warn("Couldn't split location:\n%s" % location, BiopythonWarning)
return location
return (
location[: index + 1]
+ "\n"
+ self.QUALIFIER_INDENT_STR
+ self._wrap_location(location[index + 1 :])
)
def _write_feature(self, feature, record_length):
"""Write a single SeqFeature object to features table (PRIVATE)."""
assert feature.type, feature
location = _insdc_location_string(feature.location, record_length)
f_type = feature.type.replace(" ", "_")
line = (
(self.QUALIFIER_INDENT_TMP % f_type)[: self.QUALIFIER_INDENT]
+ self._wrap_location(location)
+ "\n"
)
self.handle.write(line)
# Now the qualifiers...
# Note as of Biopython 1.69, this is an ordered-dict, don't sort it:
for key, values in feature.qualifiers.items():
if isinstance(values, (list, tuple)):
for value in values:
self._write_feature_qualifier(key, value)
else:
# String, int, etc - or None for a /pseudo tpy entry
self._write_feature_qualifier(key, values)
@staticmethod
def _get_annotation_str(record, key, default=".", just_first=False):
"""Get an annotation dictionary entry (as a string) (PRIVATE).
Some entries are lists, in which case if just_first=True the first entry
is returned. If just_first=False (default) this verifies there is only
one entry before returning it.
"""
try:
answer = record.annotations[key]
except KeyError:
return default
if isinstance(answer, list):
if not just_first:
assert len(answer) == 1
return str(answer[0])
else:
return str(answer)
@staticmethod
def _split_multi_line(text, max_len):
"""Return a list of strings (PRIVATE).
Any single words which are too long get returned as a whole line
(e.g. URLs) without an exception or warning.
"""
# TODO - Do the line splitting while preserving white space?
text = text.strip()
if len(text) <= max_len:
return [text]
words = text.split()
text = ""
while words and len(text) + 1 + len(words[0]) <= max_len:
text += " " + words.pop(0)
text = text.strip()
# assert len(text) <= max_len
answer = [text]
while words:
text = words.pop(0)
while words and len(text) + 1 + len(words[0]) <= max_len:
text += " " + words.pop(0)
text = text.strip()
# assert len(text) <= max_len
answer.append(text)
assert not words
return answer
def _split_contig(self, record, max_len):
"""Return a list of strings, splits on commas (PRIVATE)."""
# TODO - Merge this with _write_multi_line method?
# It would need the addition of the comma splitting logic...
# are there any other cases where that would be sensible?
contig = record.annotations.get("contig", "")
if isinstance(contig, (list, tuple)):
contig = "".join(contig)
contig = self.clean(contig)
answer = []
while contig:
if len(contig) > max_len:
# Split lines at the commas
pos = contig[: max_len - 1].rfind(",")
if pos == -1:
raise ValueError("Could not break up CONTIG")
text, contig = contig[: pos + 1], contig[pos + 1 :]
else:
text, contig = contig, ""
answer.append(text)
return answer
class GenBankWriter(_InsdcWriter):
"""GenBank writer."""
HEADER_WIDTH = 12
QUALIFIER_INDENT = 21
STRUCTURED_COMMENT_START = "-START##"
STRUCTURED_COMMENT_END = "-END##"
STRUCTURED_COMMENT_DELIM = " :: "
LETTERS_PER_LINE = 60
SEQUENCE_INDENT = 9
def _write_single_line(self, tag, text):
"""Write single line in each GenBank record (PRIVATE).
Used in the 'header' of each GenBank record.
"""
assert len(tag) < self.HEADER_WIDTH
if len(text) > self.MAX_WIDTH - self.HEADER_WIDTH:
if tag:
warnings.warn(
"Annotation %r too long for %r line" % (text, tag), BiopythonWarning
)
else:
# Can't give such a precise warning
warnings.warn("Annotation %r too long" % text, BiopythonWarning)
self.handle.write(
"%s%s\n" % (tag.ljust(self.HEADER_WIDTH), text.replace("\n", " "))
)
def _write_multi_line(self, tag, text):
"""Write multiple lines in each GenBank record (PRIVATE).
Used in the 'header' of each GenBank record.
"""
# TODO - Do the line splitting while preserving white space?
max_len = self.MAX_WIDTH - self.HEADER_WIDTH
lines = self._split_multi_line(text, max_len)
self._write_single_line(tag, lines[0])
for line in lines[1:]:
self._write_single_line("", line)
def _write_multi_entries(self, tag, text_list):
# used for DBLINK and any similar later line types.
# If the list of strings is empty, nothing is written.
for i, text in enumerate(text_list):
if i == 0:
self._write_single_line(tag, text)
else:
self._write_single_line("", text)
@staticmethod
def _get_date(record):
default = "01-JAN-1980"
try:
date = record.annotations["date"]
except KeyError:
return default
# Cope with a list of one string:
if isinstance(date, list) and len(date) == 1:
date = date[0]
if isinstance(date, datetime):
date = date.strftime("%d-%b-%Y").upper()
months = [
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC",
]
if not isinstance(date, str) or len(date) != 11:
return default
try:
datetime(int(date[-4:]), months.index(date[3:6]) + 1, int(date[0:2]))
except ValueError:
date = default
return date
@staticmethod
def _get_data_division(record):
try:
division = record.annotations["data_file_division"]
except KeyError:
division = "UNK"
if division in [
"PRI",
"ROD",
"MAM",
"VRT",
"INV",
"PLN",
"BCT",
"VRL",
"PHG",
"SYN",
"UNA",
"EST",
"PAT",
"STS",
"GSS",
"HTG",
"HTC",
"ENV",
"CON",
"TSA",
]:
# Good, already GenBank style
# PRI - primate sequences
# ROD - rodent sequences
# MAM - other mammalian sequences
# VRT - other vertebrate sequences
# INV - invertebrate sequences
# PLN - plant, fungal, and algal sequences
# BCT - bacterial sequences [plus archaea]
# VRL - viral sequences
# PHG - bacteriophage sequences
# SYN - synthetic sequences
# UNA - unannotated sequences
# EST - EST sequences (expressed sequence tags)
# PAT - patent sequences
# STS - STS sequences (sequence tagged sites)
# GSS - GSS sequences (genome survey sequences)
# HTG - HTGS sequences (high throughput genomic sequences)
# HTC - HTC sequences (high throughput cDNA sequences)
# ENV - Environmental sampling sequences
# CON - Constructed sequences
# TSA - Transcriptome Shotgun Assembly
#
# (plus UNK for unknown)
pass
else:
# See if this is in EMBL style:
# Division Code
# ----------------- ----
# Bacteriophage PHG - common
# Environmental Sample ENV - common
# Fungal FUN - map to PLN (plants + fungal)
# Human HUM - map to PRI (primates)
# Invertebrate INV - common
# Other Mammal MAM - common
# Other Vertebrate VRT - common
# Mus musculus MUS - map to ROD (rodent)
# Plant PLN - common
# Prokaryote PRO - map to BCT (poor name)
# Other Rodent ROD - common
# Synthetic SYN - common
# Transgenic TGN - ??? map to SYN ???
# Unclassified UNC - map to UNK
# Viral VRL - common
#
# (plus XXX for submitting which we can map to UNK)
embl_to_gbk = {
"FUN": "PLN",
"HUM": "PRI",
"MUS": "ROD",
"PRO": "BCT",
"UNC": "UNK",
"XXX": "UNK",
}
try:
division = embl_to_gbk[division]
except KeyError:
division = "UNK"
assert len(division) == 3
return division
def _get_topology(self, record):
"""Set the topology to 'circular', 'linear' if defined (PRIVATE)."""
max_topology_len = len("circular")
topology = self._get_annotation_str(record, "topology", default="")
if topology and len(topology) <= max_topology_len:
return topology.ljust(max_topology_len)
else:
return " " * max_topology_len
def _write_the_first_line(self, record):
"""Write the LOCUS line (PRIVATE)."""
locus = record.name
if not locus or locus == "<unknown name>":
locus = record.id
if not locus or locus == "<unknown id>":
locus = self._get_annotation_str(record, "accession", just_first=True)
if len(locus) > 16:
if len(locus) + 1 + len(str(len(record))) > 28:
# Locus name and record length to long to squeeze in.
# Per updated GenBank standard (Dec 15, 2018) 229.0
# the Locus identifier can be any length, and a space
# is added after the identifier to keep the identifier
# and length fields separated
warnings.warn(
"Increasing length of locus line to allow "
"long name. This will result in fields that "
"are not in usual positions.",
BiopythonWarning,
)
if len(locus.split()) > 1:
raise ValueError("Invalid whitespace in %r for LOCUS line" % locus)
if len(record) > 99999999999:
# As of the GenBank release notes 229.0, the locus line can be
# any length. However, long locus lines may not be compatible
# with all software.
warnings.warn(
"The sequence length is very long. The LOCUS "
"line will be increased in length to compensate. "
"This may cause unexpected behavior.",
BiopythonWarning,
)
# Get the molecule type
mol_type = self._get_annotation_str(record, "molecule_type", None)
if mol_type is None:
raise ValueError("missing molecule_type in annotations")
if mol_type and len(mol_type) > 7:
# Deal with common cases from EMBL to GenBank
mol_type = mol_type.replace("unassigned ", "").replace("genomic ", "")
if len(mol_type) > 7:
warnings.warn("Molecule type %r too long" % mol_type, BiopythonWarning)
mol_type = "DNA"
if mol_type in ["protein", "PROTEIN"]:
mol_type = ""
if mol_type == "":
units = "aa"
else:
units = "bp"
topology = self._get_topology(record)
division = self._get_data_division(record)
# Accommodate longer header, with long accessions and lengths
if len(locus) > 16 and len(str(len(record))) > (11 - (len(locus) - 16)):
name_length = locus + " " + str(len(record))
# This is the older, standard 80 position header
else:
name_length = str(len(record)).rjust(28)
name_length = locus + name_length[len(locus) :]
assert len(name_length) == 28, name_length
assert " " in name_length, name_length
assert len(units) == 2
assert len(division) == 3
line = "LOCUS %s %s %s %s %s %s\n" % (
name_length,
units,
mol_type.ljust(7),
topology,
division,
self._get_date(record),
)
# Extra long header
if len(line) > 80:
splitline = line.split()
if splitline[3] not in ["bp", "aa"]:
raise ValueError(
"LOCUS line does not contain size units at "
"expected position:\n" + line
)
if not (
splitline[4].strip() == ""
or "DNA" in splitline[4].strip().upper()
or "RNA" in splitline[4].strip().upper()
):
raise ValueError(
"LOCUS line does not contain valid "
"sequence type (DNA, RNA, ...):\n" + line
)
self.handle.write(line)
# 80 position header
else:
assert len(line) == 79 + 1, repr(line) # plus one for new line
# We're bending the rules to allow an identifier over 16 characters
# if we can steal spaces from the length field:
# assert line[12:28].rstrip() == locus, \
# 'LOCUS line does not contain the locus at the expected position:\n' + line
# assert line[28:29] == " "
# assert line[29:40].lstrip() == str(len(record)), \
# 'LOCUS line does not contain the length at the expected position:\n' + line
assert line[12:40].split() == [locus, str(len(record))], line
# Tests copied from Bio.GenBank.Scanner
if line[40:44] not in [" bp ", " aa "]:
raise ValueError(
"LOCUS line does not contain size units at "
"expected position:\n" + line
)
if line[44:47] not in [" ", "ss-", "ds-", "ms-"]:
raise ValueError(
"LOCUS line does not have valid strand "
"type (Single stranded, ...):\n" + line
)
if not (
line[47:54].strip() == ""
or "DNA" in line[47:54].strip().upper()
or "RNA" in line[47:54].strip().upper()
):
raise ValueError(
"LOCUS line does not contain valid "
"sequence type (DNA, RNA, ...):\n" + line
)
if line[54:55] != " ":
raise ValueError(
"LOCUS line does not contain space at position 55:\n" + line
)
if line[55:63].strip() not in ["", "linear", "circular"]:
raise ValueError(
"LOCUS line does not contain valid "
"entry (linear, circular, ...):\n" + line
)
if line[63:64] != " ":
raise ValueError(
"LOCUS line does not contain space at position 64:\n" + line
)
if line[67:68] != " ":
raise ValueError(
"LOCUS line does not contain space at position 68:\n" + line
)
if line[70:71] != "-":
raise ValueError(
"LOCUS line does not contain - at position 71 in date:\n" + line
)
if line[74:75] != "-":
raise ValueError(
"LOCUS line does not contain - at position 75 in date:\n" + line
)
self.handle.write(line)
def _write_references(self, record):
number = 0
for ref in record.annotations["references"]:
if not isinstance(ref, SeqFeature.Reference):
continue
number += 1
data = str(number)
# TODO - support more complex record reference locations?
if ref.location and len(ref.location) == 1:
molecule_type = record.annotations.get("molecule_type")
if molecule_type and "protein" in molecule_type:
units = "residues"
else:
units = "bases"
data += " (%s %i to %i)" % (
units,
ref.location[0].nofuzzy_start + 1,
ref.location[0].nofuzzy_end,
)
self._write_single_line("REFERENCE", data)
if ref.authors:
# We store the AUTHORS data as a single string
self._write_multi_line(" AUTHORS", ref.authors)
if ref.consrtm:
# We store the consortium as a single string
self._write_multi_line(" CONSRTM", ref.consrtm)
if ref.title:
# We store the title as a single string
self._write_multi_line(" TITLE", ref.title)
if ref.journal:
# We store this as a single string - holds the journal name,
# volume, year, and page numbers of the citation
self._write_multi_line(" JOURNAL", ref.journal)
if ref.medline_id:
# This line type is obsolete and was removed from the GenBank
# flatfile format in April 2005. Should we write it?
# Note this has a two space indent:
self._write_multi_line(" MEDLINE", ref.medline_id)
if ref.pubmed_id:
# Note this has a THREE space indent:
self._write_multi_line(" PUBMED", ref.pubmed_id)
if ref.comment:
self._write_multi_line(" REMARK", ref.comment)
def _write_comment(self, record):
# This is a bit complicated due to the range of possible
# ways people might have done their annotation...
# Currently the parser uses a single string with newlines.
# A list of lines is also reasonable.
# A single (long) string is perhaps the most natural of all.
# This means we may need to deal with line wrapping.
lines = []
if "structured_comment" in record.annotations:
comment = record.annotations["structured_comment"]
# Find max length of keys for equal padded printing
padding = 0
for key, data in comment.items():
for subkey, subdata in data.items():
padding = len(subkey) if len(subkey) > padding else padding
# Construct output
for key, data in comment.items():
lines.append(f"##{key}{self.STRUCTURED_COMMENT_START}")
for subkey, subdata in data.items():
spaces = " " * (padding - len(subkey))
lines.append(
f"{subkey}{spaces}{self.STRUCTURED_COMMENT_DELIM}{subdata}"
)
lines.append(f"##{key}{self.STRUCTURED_COMMENT_END}")
if "comment" in record.annotations:
comment = record.annotations["comment"]
if isinstance(comment, str):
lines += comment.split("\n")
elif isinstance(comment, (list, tuple)):
lines += list(comment)
else:
raise ValueError("Could not understand comment annotation")
self._write_multi_line("COMMENT", lines[0])
for line in lines[1:]:
self._write_multi_line("", line)
def _write_contig(self, record):
max_len = self.MAX_WIDTH - self.HEADER_WIDTH
lines = self._split_contig(record, max_len)
self._write_single_line("CONTIG", lines[0])
for text in lines[1:]:
self._write_single_line("", text)
def _write_sequence(self, record):
# Loosely based on code from Howard Salis
# TODO - Force lower case?
if isinstance(record.seq, UnknownSeq):
# We have already recorded the length, and there is no need
# to record a long sequence of NNNNNNN...NNN or whatever.
if "contig" in record.annotations:
self._write_contig(record)
else:
self.handle.write("ORIGIN\n")
return
# Catches sequence being None:
data = self._get_seq_string(record).lower()
seq_len = len(data)
self.handle.write("ORIGIN\n")
for line_number in range(0, seq_len, self.LETTERS_PER_LINE):
self.handle.write(str(line_number + 1).rjust(self.SEQUENCE_INDENT))
for words in range(
line_number, min(line_number + self.LETTERS_PER_LINE, seq_len), 10
):
self.handle.write(" %s" % data[words : words + 10])
self.handle.write("\n")
def write_record(self, record):
"""Write a single record to the output file."""
handle = self.handle
self._write_the_first_line(record)
default = record.id
if default.count(".") == 1 and default[default.index(".") + 1 :].isdigit():
# Good, looks like accession.version and not something
# else like identifier.start-end
default = record.id.split(".", 1)[0]
accession = self._get_annotation_str(
record, "accession", default, just_first=True
)
acc_with_version = accession
if record.id.startswith(accession + "."):
try:
acc_with_version = "%s.%i" % (
accession,
int(record.id.split(".", 1)[1]),
)
except ValueError:
pass
gi = self._get_annotation_str(record, "gi", just_first=True)