Repository: biopython/biopython

Issue:
After slice a SeqRecord, the description of the new SeqRecord cannot be change in genbank format
### Setup

I am reporting a problem with Biopython version, Python version, and operating
system as follows:

3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0]
CPython
Linux-4.19.104-microsoft-standard-x86_64-with-debian-bullseye-sid
1.78

### Expected behaviour

```
LOCUS       NC_005816                500 bp    DNA              UNK 01-JAN-1980
DEFINITION  Yersinia pestis biovar Microtus str. 91001 plasmid pPCP1, partial.
ACCESSION   NC_005816
VERSION     NC_005816.1
KEYWORDS    .
SOURCE      .
  ORGANISM  .
            .
FEATURES             Location/Qualifiers
     gene            43..480
                     /db_xref="GeneID:2767712"
                     /gene="pim"
                     /locus_tag="YP_pPCP05"
     CDS             43..480
                     /codon_start=1
                     /db_xref="GI:45478716"
                     /db_xref="GeneID:2767712"
                     /gene="pim"
                     /locus_tag="YP_pPCP05"
                     /note="similar to many previously sequenced pesticin
                     immunity protein entries of Yersinia pestis plasmid pPCP,
                     e.g. gi| 16082683|,ref|NP_395230.1| (NC_003132) ,
                     gi|1200166|emb|CAA90861.1| (Z54145 ) , gi|1488655|
                     emb|CAA63439.1| (X92856) , gi|2996219|gb|AAC62543.1|
                     (AF053945) , and gi|5763814|emb|CAB531 67.1| (AL109969)"
                     /product="pesticin immunity protein"
                     /protein_id="NP_995571.1"
                     /transl_table=11
                     /translation="MGGGMISKLFCLALIFLSSSGLAEKNTYTAKDILQNLELNTFGNS
                     LSHGIYGKQTTFKQTEFTNIKSNTKKHIALINKDNSWMISLKILGIKRDEYTVCFEDFS
                     LIRPPTYVAIHPLLIKKVKSGNFIVVKEIKKSIPGCTVYYH"
ORIGIN
        1 ataaatagat tattccaaat aatttattta tgtaagaaca ggatgggagg gggaatgatc
       61 tcaaagttat tttgcttggc tctcatattt ttatcatcaa gtggccttgc agaaaaaaac
      121 acatatacag caaaagacat cttgcaaaac ctagaattaa atacctttgg caattcattg
      181 tctcatggca tctatgggaa acagacaacc ttcaagcaaa ccgagtttac aaatattaaa
      241 agcaacacca aaaaacacat tgcacttatc aataaagaca actcatggat gatatcatta
      301 aaaatactag gaattaagag agatgagtat actgtctgtt ttgaagattt ctctctaata
      361 agaccgccaa catatgtagc catacatcct ctacttataa aaaaagtaaa atctggaaac
      421 tttatagtag tgaaagaaat aaagaaatct atccctggtt gcactgtata ttatcattaa
      481 tagcaagccc ctcattatta
//
```

### Actual behaviour

```
ValueError                                Traceback (most recent call last)
<ipython-input-54-eb747d757b4f> in <module>
      1 sub_record.description = "Yersinia pestis biovar Microtus str. 91001 plasmid pPCP1, partial."
----> 2 print(sub_record.format("genbank"))

~/anaconda3/lib/python3.7/site-packages/Bio/SeqRecord.py in format(self, format)
    717         # See also the __format__ method
    718         # See also the Bio.Align.Generic.Alignment class and its format()
--> 719         return self.__format__(format)
    720 
    721     def __format__(self, format_spec):

~/anaconda3/lib/python3.7/site-packages/Bio/SeqRecord.py in __format__(self, format_spec)
    755         handle = StringIO()
    756         try:
--> 757             SeqIO.write(self, handle, format_spec)
    758         except StreamModeError:
    759             raise ValueError(

~/anaconda3/lib/python3.7/site-packages/Bio/SeqIO/__init__.py in write(sequences, handle, format)
    528     writer_class = _FormatToWriter.get(format)
    529     if writer_class is not None:
--> 530         count = writer_class(handle).write_file(sequences)
    531         if not isinstance(count, int):
    532             raise RuntimeError(

~/anaconda3/lib/python3.7/site-packages/Bio/SeqIO/Interfaces.py in write_file(self, records, mincount, maxcount)
    242         try:
    243             self.write_header()
--> 244             count = self.write_records(records, maxcount)
    245             self.write_footer()
    246         finally:

~/anaconda3/lib/python3.7/site-packages/Bio/SeqIO/Interfaces.py in write_records(self, records, maxcount)
    216         if maxcount is None:
    217             for record in records:
--> 218                 self.write_record(record)
    219                 count += 1
    220         else:

~/anaconda3/lib/python3.7/site-packages/Bio/SeqIO/InsdcIO.py in write_record(self, record)
    979         """Write a single record to the output file."""
    980         handle = self.handle
--> 981         self._write_the_first_line(record)
    982 
    983         default = record.id

~/anaconda3/lib/python3.7/site-packages/Bio/SeqIO/InsdcIO.py in _write_the_first_line(self, record)
    742         mol_type = self._get_annotation_str(record, "molecule_type", None)
    743         if mol_type is None:
--> 744             raise ValueError("missing molecule_type in annotations")
    745         if mol_type and len(mol_type) > 7:
    746             # Deal with common cases from EMBL to GenBank

ValueError: missing molecule_type in annotations
```

### Steps to reproduce

final part of section 4.7: view the sub-record as a reduced GenBank file using the format method.

In this code

```python
sub_record.description = "Yersinia pestis biovar Microtus str. 91001 plasmid pPCP1, partial."
print(sub_record.format("genbank"))
```

### Additional info

When we substitute the parameter "genbank" for "fasta" it does give us a result, however, it does not fulfill its purpose, which is to modify genbank files


Discussion:
This is an overlooked side effect from the removal of alphabets in Biopython 1.78, and the GenBank writer using ``.annotations['molecule_type']`` instead. The example needs to be updated to add this:

```python
sub_record.annotations['molecule_type'] = record.annotations['molecule_type']
```

Resolving patch:
diff --git a/Bio/SeqRecord.py b/Bio/SeqRecord.py
--- a/Bio/SeqRecord.py
+++ b/Bio/SeqRecord.py
@@ -355,8 +355,9 @@ def __getitem__(self, index):
           adjusted accordingly). If you want to preserve any truncated
           features (e.g. GenBank/EMBL source features), you must
           explicitly add them to the new SeqRecord yourself.
-        * The annotations dictionary and the dbxrefs list are not used
-          for the new SeqRecord, as in general they may not apply to the
+        * With the exception of any molecule type, the annotations
+          dictionary and the dbxrefs list are not used for the new
+          SeqRecord, as in general they may not apply to the
           subsequence. If you want to preserve them, you must explicitly
           copy them to the new SeqRecord yourself.
 
@@ -489,6 +490,10 @@ def __getitem__(self, index):
             # answer.dbxrefs = self.dbxrefs[:]
             # TODO - Review this in light of adding SeqRecord objects?
 
+            if "molecule_type" in self.annotations:
+                # This will still apply, and we need it for GenBank/EMBL etc output
+                answer.annotations["molecule_type"] = self.annotations["molecule_type"]
+
             # TODO - Cope with strides by generating ambiguous locations?
             start, stop, step = index.indices(parent_length)
             if step == 1:


Test patch: