/usr/lib/python2.7/dist-packages/cogent/db/ensembl/related_region.py is in python-cogent 1.9-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from pprint import pprint
import sqlalchemy as sql
from cogent import DNA
from cogent.core.alignment import SequenceCollection, Alignment, Aligned
from cogent.parse import cigar
from cogent.db.ensembl.util import LazyRecord, asserted_one, NoItemError
from cogent.db.ensembl.assembly import location_query
from cogent.db.ensembl.species import Species
__author__ = "Gavin Huttley"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Gavin Huttley"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Gavin Huttley"
__email__ = "Gavin.Huttley@anu.edu.au"
__status__ = "alpha"
class _RelatedRegions(LazyRecord):
# a basic related region, capable of providing the sequences
# obtaining SyntenicRegions -- for getting aligned blocks -- is delegated
# to compara
Type = None
def __init__(self):
super(_RelatedRegions, self).__init__()
def __str__(self):
# temporary string method, just to demo correct assembly
# TODO StableID, Species and Description
my_type = self.__class__.__name__
data = map(repr, self.Members)
data.insert(0, '%s(' % my_type)
data.append(')')
return "\n\t".join(data)
def getSeqCollection(self, feature_types=None, where_feature=None):
"""returns a SequenceCollection instance of the unaligned sequences"""
seqs = []
for member in self.Members:
if feature_types:
seq = member.getAnnotatedSeq(feature_types,where_feature)
else:
seq = member.Seq
if seq is None:
continue
seqs.append((seq.Name, seq))
return SequenceCollection(data=seqs, MolType=DNA)
def getSeqLengths(self):
"""returns a vector of lengths"""
return [len(member) for member in self.Members]
def getSpeciesSet(self):
"""returns the latin names of self.Member species as a set"""
return set([m.Location.Species for m in self.Members])
class RelatedGenes(_RelatedRegions):
Type = 'related_genes'
def __init__(self, compara, Members, Relationships):
super(RelatedGenes, self).__init__()
self.compara = compara
self.Members = tuple(Members)
self.Relationships = Relationships
def __str__(self):
my_type = self.__class__.__name__
display = ['%s:' % my_type,
' Relationships=%s' % self.Relationships]
display += [' %s' % m for m in self.Members]
return '\n'.join(display)
def __repr__(self):
return self.__str__()
def getMaxCdsLengths(self):
"""returns the vector of maximum Cds lengths from member transcripts"""
return [max(member.getCdsLengths()) for member in self.Members]
class SyntenicRegion(LazyRecord):
"""a class that takes the genome, compara instances and is used to build
Aligned sequences for Ensembl multiple alignments"""
def __init__(self, parent, genome, identifiers_values, am_ref_member,
Location=None):
# create with method_link_species_set_id, at least, in
# identifiers_values
super(SyntenicRegion, self).__init__()
self.parent = parent
self.compara = parent.compara
self.genome = genome
self.am_ref_member = am_ref_member
self.aln_map = None
self.aln_loc = None
self._make_map_func = [self._make_map_from_ref,
self._make_ref_map][am_ref_member]
if Location is not None:
if hasattr(Location, 'Location'): # likely to be a feature region
region = Location
else:
region = genome.getRegion(region=Location)
self._cached['Region'] = region
for identifier, value in dict(identifiers_values).items():
self._cached[identifier] = value
def __len__(self):
return len(self._get_cached_value('Region', self._make_map_func))
def _get_location(self):
region = self._get_cached_value('Region', self._make_map_func)
return region.Location
Location = property(_get_location)
def _get_region(self):
region = self._get_cached_value('Region', self._make_map_func)
return region
Region = property(_get_region)
def _get_cigar_record(self):
genomic_align_table = \
self.parent.compara.ComparaDb.getTable('genomic_align')
query = sql.select([genomic_align_table.c.cigar_line],
genomic_align_table.c.genomic_align_id == \
self._cached['genomic_align_id'])
record = asserted_one(query.execute())
self._cached['cigar_line'] = record['cigar_line']
return record
def _get_cigar_line(self):
return self._get_cached_value('cigar_line', self._get_cigar_record)
cigar_line = property(_get_cigar_line)
def _make_ref_map(self):
if self.aln_map and self.aln_loc is not None:
return
ref_record = self._cached
record_start = ref_record['dnafrag_start']
record_end = ref_record['dnafrag_end']
record_strand = ref_record['dnafrag_strand']
block_loc = self.genome.makeLocation(CoordName=ref_record['name'],
Start=record_start,
End=record_end,
Strand=record_strand,
ensembl_coord=True)
ref_location = self.parent.ref_location
relative_start = ref_location.Start-block_loc.Start
relative_end = relative_start + len(ref_location)
if block_loc.Strand != 1:
relative_start = len(block_loc) - relative_end
relative_end = relative_start + len(ref_location)
aln_map, aln_loc = cigar.slice_cigar(self.cigar_line, relative_start,
relative_end, by_align = False)
self.aln_map = aln_map
self.aln_loc = aln_loc
region_loc = ref_location.copy()
region_loc.Strand = block_loc.Strand
region = self.genome.getRegion(region=region_loc)
self._cached['Region'] = region
def _make_map_from_ref(self):
# this is the 'other' species
if self.aln_loc and self.aln_map is not None:
return
record = self._cached
try:
aln_map, aln_loc = cigar.slice_cigar(self.cigar_line,
self.parent.CigarStart,
self.parent.CigarEnd,
by_align=True)
self.aln_map = aln_map
self.aln_loc = aln_loc # probably unnecesary to store??
# we make a loc for the aligned region
block_loc = self.genome.makeLocation(CoordName=record['name'],
Start=record['dnafrag_start'],
End = record['dnafrag_end'],
Strand=record['dnafrag_strand'],
ensembl_coord=True)
relative_start = aln_loc[0]
relative_end = aln_loc[1]
# new location with correct length
loc = block_loc.copy()
loc.End = loc.Start+(relative_end-relative_start)
if block_loc.Strand != 1:
shift = len(block_loc) - relative_end
else:
shift = relative_start
loc = loc.shifted(shift)
region = self.genome.getRegion(region=loc)
except IndexError: # TODO ask Hua where these index errors occur
region = None
self._cached['Region'] = region
def _make_aligned(self, feature_types = None, where_feature=None):
if self.aln_loc is None or self.aln_map is None: # is this required?
self._make_map_func()
region = self._cached['Region']
if region is None:
self._cached['AlignedSeq'] = None
return
if feature_types:
seq = region.getAnnotatedSeq(feature_types, where_feature)
else:
seq = region.Seq
# we get the Seq objects to allow for copying of their annotations
gapped_seq = Aligned(self.aln_map, seq)
self._cached['AlignedSeq'] = gapped_seq
def _get_aligned_seq(self):
aligned = self._get_cached_value('AlignedSeq', self._make_aligned)
return aligned
AlignedSeq = property(_get_aligned_seq)
def getAnnotatedAligned(self, feature_types, where_feature=None):
"""returns aligned seq annotated for the specified feature types"""
region = self._get_cached_value('Region', self._make_map_func)
if region is None:
return None
self._make_aligned(feature_types=feature_types,
where_feature=where_feature)
return self.AlignedSeq
class SyntenicRegions(_RelatedRegions):
Type = 'syntenic_regions'
def __init__(self, compara, Members, ref_location):
super(SyntenicRegions, self).__init__()
self.compara = compara
members = []
ref_member = None
self.ref_location = ref_location
for genome, data in Members:
if genome is ref_location.genome:
ref_member = SyntenicRegion(self, genome, dict(data),
am_ref_member=True, Location=ref_location)
else:
members += [SyntenicRegion(self, genome, dict(data),
am_ref_member=False)]
assert ref_member is not None, "Can't match a member to ref_location"
self.ref_member = ref_member
self.Members = tuple([ref_member] + members)
self.NumMembers = len(self.Members)
self.aln_loc = None
self._do_rc = None
def __str__(self):
my_type = self.__class__.__name__
display = ['%s:' % my_type]
display += [' %r' % m.Location for m in self.Members \
if m.Region is not None]
return '\n'.join(display)
def __repr__(self):
return self.__str__()
def _populate_ref(self):
"""near (don't actually get the sequence) completes construction of
ref sequence"""
self.ref_member._make_map_func()
self._cached['CigarStart'] = self.ref_member.aln_loc[0]
self._cached['CigarEnd'] = self.ref_member.aln_loc[1]
def _get_rc_state(self):
"""determines whether the ref_member strand is the same as that from
the align block, if they diff we will rc the alignment, seqs,
seq_names"""
if self._do_rc is not None:
return self._do_rc
self._populate_ref()
inferred = self.ref_member._cached['Region'].Location.Strand
self._do_rc = self.ref_location.Strand != inferred
return self._do_rc
_rc = property(fget=_get_rc_state)
def __len__(self):
return self.CigarEnd - self.CigarStart
def _get_ref_start(self):
return self._get_cached_value('CigarStart', self._populate_ref)
CigarStart = property(_get_ref_start)
def _get_ref_end(self):
return self._get_cached_value('CigarEnd', self._populate_ref)
CigarEnd = property(_get_ref_end)
def getAlignment(self, feature_types=None, where_feature=None,
omit_redundant=True):
"""Arguments:
- feature_types: annotations to be applied to the returned
sequences
- omit_redundant: exclude redundant gap positions"""
seqs = []
annotations = {}
for member in self.Members:
if feature_types:
seq = member.getAnnotatedAligned(feature_types, where_feature)
else:
seq = member.AlignedSeq
if seq is None:
continue
name = seq.Name
if self._rc: # names should reflect change to strand
loc = member.Location.copy()
loc.Strand *= -1
name = str(loc)
annotations[name] = seq.data.annotations
seq.Name = seq.data.Name = name
seqs += [(name, seq)]
if seqs is None:
return None
aln = Alignment(data=seqs, MolType=DNA)
if self._rc:
aln = aln.rc()
if omit_redundant:
aln = aln.filtered(lambda x: set(x) != set('-'))
return aln
|