annotate cpt_renumber_gbk/BIO_FIX_TOPO.py @ 0:8cac332dbc77 draft default tip

Uploaded
author cpt
date Fri, 17 Jun 2022 13:13:47 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
1 import Bio.GenBank
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
2
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
3
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
4 def record_end(self, content):
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
5 """Clean up when we've finished the record.
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
6 """
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
7 #from Bio import Alphabet
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
8 #from Bio.Alphabet import IUPAC
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
9 from Bio.Seq import Seq, UnknownSeq
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
10
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
11 # Try and append the version number to the accession for the full id
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
12 if not self.data.id:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
13 assert "accessions" not in self.data.annotations, self.data.annotations[
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
14 "accessions"
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
15 ]
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
16 self.data.id = self.data.name # Good fall back?
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
17 elif self.data.id.count(".") == 0:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
18 try:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
19 self.data.id += ".%i" % self.data.annotations["sequence_version"]
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
20 except KeyError:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
21 pass
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
22
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
23 # add the sequence information
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
24 # first, determine the alphabet
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
25 # we default to an generic alphabet if we don't have a
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
26 # seq type or have strange sequence information.
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
27
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
28 #seq_alphabet = Alphabet.generic_alphabet
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
29
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
30 # now set the sequence
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
31 sequence = "".join(self._seq_data)
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
32
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
33 if (
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
34 self._expected_size is not None
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
35 and len(sequence) != 0
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
36 and self._expected_size != len(sequence)
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
37 ):
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
38 import warnings
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
39 from Bio import BiopythonParserWarning
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
40
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
41 warnings.warn(
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
42 "Expected sequence length %i, found %i (%s)."
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
43 % (self._expected_size, len(sequence), self.data.id),
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
44 BiopythonParserWarning,
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
45 )
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
46 """
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
47 if self._seq_type:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
48 # mRNA is really also DNA, since it is actually cDNA
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
49 if "DNA" in self._seq_type.upper() or "MRNA" in self._seq_type.upper():
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
50 seq_alphabet = IUPAC.ambiguous_dna
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
51 # are there ever really RNA sequences in GenBank?
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
52 elif "RNA" in self._seq_type.upper():
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
53 # Even for data which was from RNA, the sequence string
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
54 # is usually given as DNA (T not U). Bug 2408
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
55 if "T" in sequence and "U" not in sequence:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
56 seq_alphabet = IUPAC.ambiguous_dna
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
57 else:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
58 seq_alphabet = IUPAC.ambiguous_rna
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
59 elif (
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
60 "PROTEIN" in self._seq_type.upper() or self._seq_type == "PRT"
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
61 ): # PRT is used in EMBL-bank for patents
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
62 seq_alphabet = IUPAC.protein # or extended protein?
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
63 # work around ugly GenBank records which have circular or
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
64 # linear but no indication of sequence type
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
65 elif self._seq_type in ["circular", "linear", "unspecified"]:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
66 pass
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
67 # we have a bug if we get here
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
68 else:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
69 raise ValueError(
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
70 "Could not determine alphabet for seq_type %s" % self._seq_type
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
71 )
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
72
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
73 # Also save the chomosome layout
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
74 if "circular" in self._seq_type.lower():
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
75 self.data.annotations["topology"] = "circular"
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
76 elif "linear" in self._seq_type.lower():
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
77 self.data.annotations["topology"] = "linear"
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
78 """
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
79 if not sequence and self.__expected_size:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
80 self.data.seq = UnknownSeq(self._expected_size)#, seq_alphabet)
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
81 else:
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
82 self.data.seq = Seq(sequence)#, seq_alphabet)
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
83
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
84
8cac332dbc77 Uploaded
cpt
parents:
diff changeset
85 Bio.GenBank._FeatureConsumer.record_end = record_end