Beispiel #1
0
def _get_restr_type(name):
    '''Gets RestrictionType from name.'''
    types = [
        x for _, (x, y) in Restriction_Dictionary.typedict.items() if name in y
    ][0]

    enz_types = tuple(getattr(Restriction, typ) for typ in types)

    return Restriction.RestrictionType(str(name), enz_types,
                                       Restriction_Dictionary.rest_dict[name])
Beispiel #2
0
def get_enzyme_instance_ipython(enz_name):
    """
    An alternative method to fetch an instance of a given restriction enzyme by its
    name using a work-around which avoids exception with getattr() in iPython.

    Ackn: StackOverflow: user xbello.
    See: http://stackoverflow.com/questions/20381912/type-object-restrictiontype-has-no-attribute-size

    :param enz_name: the case-sensitive name of the enzyme
    :return: RestrictionType the enzyme instance
    """
    r_type_names = [rt for tid, (rt, rn) in typedict.iteritems() if enz_name in rn][0]
    r_clz = tuple(getattr(Restriction, rt) for rt in r_type_names)
    return Restriction.AbstractCut(enz_name, r_clz, rest_dict[enz_name])
Beispiel #3
0
def censor_record(
    record,
    record_id="censored",
    label_generator="feature_%d",
    keep_topology=False,
    anonymise_features=True,
    preserve_sites=None,
):
    """Return a record with random sequence and censored annotations/features.

    Useful for creating example files or anonymising sequences for bug reports.


    Parameters
    ----------

    record
      The record to be anonymized.

    record_id
      ID of the new record.

    label_generator
      Recipe to change feature labels. Either ``"feature_%d"`` or ``None`` (no label)
      of a function (i, feature)=>label.

    keep_topology
      Whether to keep the record topology or not.

    anonymise_features
      Whether to replace feature labels and ID/name, or not.

    preserve_sites
      List of enzyme sites to keep. Example: ``["BsmBI", "BsaI"]``. Preserves the
      sequence around cut sites of the specified enzymes.
    """
    # Anonymise
    if anonymise_features:
        new_record = anonymized_record(
            record, record_id=record_id, label_generator=label_generator
        )
    else:
        new_record = deepcopy(record)

    if keep_topology:
        try:
            new_record.annotations["topology"] = record.annotations["topology"]
        except KeyError:  # input may not have topology set
            pass

    # Randomise
    new_seq = random_dna_sequence(
        len(new_record), gc_share=None, probas=None, seed=None
    )

    if preserve_sites:
        restriction_batch = Restriction.RestrictionBatch(preserve_sites)
        # Destroy random new enzyme sites:
        analysis = Restriction.Analysis(restriction_batch, sequence=Seq(new_seq))
        analysis_results = analysis.full()
        for enzyme, hits in analysis_results.items():
            for hit in hits:
                # 10 bp up- and downstream destroys the site whichever strand it is on:
                if hit - 10 < 0:  # handle edge cases
                    start = 0
                    upstream = "A" * hit
                else:
                    start = hit - 10
                    upstream = "A" * 10
                if hit + 10 > len(new_seq):
                    end = len(new_seq)
                    downstream = "A" * (len(new_seq) - hit)
                else:
                    end = hit + 10
                    downstream = "A" * 10
                replacement = upstream + downstream
                new_seq = easy_dna.replace_segment(new_seq, start, end, replacement)

        # Add original sites:
        analysis = Restriction.Analysis(restriction_batch, sequence=record.seq)
        analysis_results = analysis.full()
        original_seq = str(record.seq)
        for enzyme, hits in analysis_results.items():
            for hit in hits:
                # keep 12 bp surrounding the cut site, to capture enzyme site:
                if hit - 12 < 0:  # handle edge cases
                    start = 0
                else:
                    start = hit - 12
                if hit + 12 > len(new_seq):
                    end = len(new_seq)
                else:
                    end = hit + 12
                original_segment = original_seq[start:end]
                new_seq = easy_dna.replace_segment(
                    new_seq, start, end, original_segment
                )

    censored_record = record_with_different_sequence(new_record, new_seq)

    return censored_record
Beispiel #4
0
 def test_init(self):
     """Check for error during __init__."""
     with self.assertRaises(ValueError) as ve:
         Restriction.OneCut('bla-me', (Restriction.RestrictionType, ),
                            dict())
         self.assertIn('hyphen', str(ve.exception))
Beispiel #5
0
 def createAnalysis(self, seq_str, batch_ary):
     """Restriction.Analysis creation helper method."""
     rb = Restriction.RestrictionBatch(batch_ary)
     seq = Seq(seq_str)
     return Restriction.Analysis(rb, seq)
Beispiel #6
0
 def test_init(self):
     """Check for error during __init__."""
     with self.assertRaises(ValueError) as ve:
         Restriction.OneCut("bla-me", (Restriction.RestrictionType, ), {})
         self.assertIn("hyphen", str(ve.exception))