Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/9c7963064511/allpy/sequence.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 07:43:39 2013
Кодировка:
allpy: 9c7963064511 allpy/sequence.py

allpy

view allpy/sequence.py @ 227:9c7963064511

repeats: universal input format parser
author boris <bnagaev@gmail.com>
date Sat, 27 Nov 2010 18:47:13 +0300
parents a016cad5f5a0
children 346a6cd4fc1d
line source
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 from monomer import AminoAcidType
5 from Bio.PDB import CaPPBuilder, PDBIO
6 from Bio.PDB.DSSP import make_dssp_dict
7 from allpy_pdb import std_id, pdb_id_parse, get_structure
8 import alignment
9 import sys
10 import config
11 import os.path
12 import urllib2
13 from tempfile import NamedTemporaryFile
14 import os
17 class Sequence(object):
18 """ Sequence of Monomers
20 Mandatory data:
21 * name -- str with the name of sequence
22 * description -- str with description of the sequence
23 * monomers -- list of monomer objects (aminoacids or nucleotides)
25 Optional (may be empty):
26 * pdb_chains -- list of Bio.PDB.Chain's
27 * pdb_files -- dictionary like {Bio.PDB.Chain: file_obj}
29 * pdb_residues -- dictionary like {Bio.PDB.Chain: {Monomer: Bio.PDB.Residue}}
30 * pdb_secstr -- dictionary like {Bio.PDB.Chain: {Monomer: 'Secondary structure'}}
31 Code Secondary structure
32 H alpha-helix
33 B Isolated beta-bridge residue
34 E Strand
35 G 3-10 helix
36 I pi-helix
37 T Turn
38 S Bend
39 - Other
42 ?TODO: global pdb_structures
43 """
44 def __init__(self, monomers=None, name='', description=""):
45 if not monomers:
46 monomers = []
47 self.name = name
48 self.description = description
49 self.monomers = monomers
50 self.pdb_chains = []
51 self.pdb_files = {}
52 self.pdb_residues = {}
53 self.pdb_secstr = {}
55 def __len__(self):
56 return len(self.monomers)
58 def __str__(self):
59 """ Returns sequence in one-letter code """
60 return ''.join([monomer.type.code1 for monomer in self.monomers])
62 def __eq__(self, other):
63 """ Returns if all corresponding monomers of this sequences are equal
65 If lengths of sequences are not equal, returns False
66 """
67 return len(self) == len(other) and \
68 all([a==b for a, b in zip(self.monomers, other.monomers)])
70 def __ne__(self, other):
71 return not (self == other)
73 def pdb_chain_add(self, pdb_file, pdb_id, pdb_chain, pdb_model=0):
74 """ Reads Pdb chain from file
76 and align each Monomer with PDB.Residue (TODO)
77 """
78 name = std_id(pdb_id, pdb_chain, pdb_model)
79 structure = get_structure(pdb_file, name)
80 chain = structure[pdb_model][pdb_chain]
81 self.pdb_chains.append(chain)
82 self.pdb_residues[chain] = {}
83 self.pdb_secstr[chain] = {}
84 pdb_sequence = Sequence.from_pdb_chain(chain)
85 a = alignment.Alignment.from_sequences(self, pdb_sequence)
86 a.muscle_align()
87 for monomer, pdb_monomer in a.column(sequence=pdb_sequence, original=self):
88 if pdb_sequence.pdb_has(chain, pdb_monomer):
89 residue = pdb_sequence.pdb_residues[chain][pdb_monomer]
90 self.pdb_residues[chain][monomer] = residue
91 self.pdb_files[chain] = pdb_file
93 def pdb_unload(self):
94 """ Delete all pdb-connected links """
95 #~ gc.get_referrers(self.pdb_chains[0])
96 self.pdb_chains = []
97 self.pdb_residues = {}
98 self.pdb_secstr = {} # FIXME
99 self.pdb_files = {} # FIXME
101 @staticmethod
102 def from_str(fasta_str, name='', description='', monomer_kind=AminoAcidType):
103 """ Import data from one-letter code
105 monomer_kind is class, inherited from MonomerType
106 """
107 monomers = [monomer_kind.from_code1(aa).instance() for aa in fasta_str]
108 return Sequence(monomers, name, description)
110 @staticmethod
111 def from_pdb_chain(chain):
112 """ Returns Sequence with Monomers with link to Bio.PDB.Residue
114 chain is Bio.PDB.Chain
115 """
116 cappbuilder = CaPPBuilder()
117 peptides = cappbuilder.build_peptides(chain)
118 sequence = Sequence()
119 sequence.pdb_chains = [chain]
120 sequence.pdb_residues[chain] = {}
121 sequence.pdb_secstr[chain] = {}
122 for peptide in peptides:
123 for ca_atom in peptide.get_ca_list():
124 residue = ca_atom.get_parent()
125 monomer = AminoAcidType.from_pdb_residue(residue).instance()
126 sequence.pdb_residues[chain][monomer] = residue
127 sequence.monomers.append(monomer)
128 return sequence
130 def pdb_auto_add(self, conformity_info=None, pdb_directory='./tmp'):
131 """ Adds pdb information to each monomer
133 Returns if information has been successfully added
134 TODO: conformity_file
136 id-format lava flow
137 """
138 if not conformity_info:
139 path = os.path.join(pdb_directory, self.name)
140 if os.path.exists(path) and os.path.getsize(path):
141 match = pdb_id_parse(self.name)
142 self.pdb_chain_add(open(path), match['code'