Example #1
0
    def __init__(self, span_cost=None, seqdb=None, name: str = ""):
        """

        :param span_cost:
        :type span_cost: SpanCost
        :param seqdb:
        :type seqdb: dict
        """
        self.name = name
        self.blast_factory = BioBlastFactory(config=BLAST_PENALTY_CONFIG)
        self.logger = logger(self)

        # graph by query_key
        if seqdb is None:
            seqdb = {}
        self._seqdb = seqdb  #: Sequence dict registry
        if span_cost is None:
            span_cost = cached_span_cost()
        self.span_cost = span_cost  #: span cost df
        self.graphs = {}  #: Dict[str, nx.DiGraph]
        self._results = {}  #: Dict[str, DesignResult]
        self.template_results = []
        self.fragment_results = []
        self.primer_results = []
        self.container_factory = AlignmentContainerFactory(self.seqdb)
        self._times = {}
        self._method_trace = {}
Example #2
0
 def __init__(
     self,
     directory=os.getcwd(),
     primers="primers.fasta",
     templates="templates/*.gb",
     fragments="fragments/*.gb",
     goals="goals/*.gb",
     verbose="v",
 ):
     """Initialize a new design."""
     self._directory = directory
     self._primers = os.path.join(self._directory, primers)
     self._templates = os.path.join(self._directory, templates)
     self._fragments = os.path.join(self._directory, fragments)
     self._goals = os.path.join(self._directory, goals)
     self._do_save = True
     self._logger = logger(self)
     if verbose == "v":
         self._logger.set_level("INFO")
     elif verbose == "vv":
         logger.set_level("INFO")
     elif verbose == "vvv":
         logger.set_level("DEBUG")
     elif verbose is None:
         pass
     else:
         raise ValueError("Verbose level '{}' not recognized. "
                          "Select from 'v', 'vv', or 'vvv'")
Example #3
0
    def __init__(
        self,
        nodes: List[AssemblyNode],
        container: AlignmentContainer,
        full_assembly_graph: nx.DiGraph,
        query_key: str,
        query: SeqRecord,
        seqdb,
        do_raise: bool = True,
    ):
        self.logger = logger(self)
        self._nodes = tuple(nodes)
        self._reactions = tuple()
        self.validate_input_nodes()
        self.seqdb = seqdb
        self.query_key = query_key
        self.query = query
        self._full_graph = full_assembly_graph

        self.container = container
        self.groups = container.groups()
        if len(self.groups) == 0:
            raise DasiDesignException("No groups were found in container.")
        self.graph = self._subgraph(self._full_graph, nodes, do_raise=do_raise)
        nx.freeze(self.graph)

        if do_raise:
            self.post_validate(do_raise)
Example #4
0
 def __init__(self, alignment_container: AlignmentContainer, span_cost=None):
     self.container = alignment_container
     if span_cost is None:
         self.span_cost = SpanCost.open()
     else:
         self.span_cost = span_cost
     self.G = None
     self.logger = logger(self)
    def __init__(self, seqdb: Dict[str, SeqRecord]):
        """Construct a new AlignmentContainer.

        :param seqdb: a sequence record database
        """
        self._alignments = (
            {}
        )  # dictionary of query_key to alignment; Dict[str, List[Alignment]]
        self._containers = None
        self.logger = logger(self)
        self.seqdb = seqdb
    def __init__(self, seqdb: Dict[str, SeqRecord], alignments=None):
        self._alignments = []
        self._frozen = False
        self._frozen_groups = None
        if alignments is not None:
            self.set_alignments(alignments)
        self.seqdb = seqdb
        self.logger = logger(self)

        self.group_tags = GroupTags()
        self.group_tags.new_tag(Constants.PCR_GROUP_TAG, self.pcr_constructor)
        self.group_tags.new_tag(Constants.SHARE_GROUP_TAG,
                                self.share_constructor)
Example #7
0
 def __init__(
     self,
     graph: nx.DiGraph,
     query: SeqRecord,
     span_cost: SpanCost,
     seqdb: Dict[str, SeqRecord],
     container: AlignmentContainer,
     stats_repeat_window: Optional[int] = None,
     stats_window: Optional[int] = None,
     stats_hairpin_window: Optional[int] = None,
     edge_threshold: Optional[float] = None,
     stages: Optional[Tuple[str]] = None,
 ):
     if stats_repeat_window is None:
         stats_repeat_window = SequenceScoringConfig.stats_repeat_window
     if stats_window is None:
         stats_window = SequenceScoringConfig.stats_window
     if stats_hairpin_window is None:
         stats_hairpin_window = SequenceScoringConfig.stats_hairpin_window
     if stages is None:
         stages = SequenceScoringConfig.post_process_stages
     if edge_threshold is None:
         edge_threshold = SequenceScoringConfig.edge_threshold
     self.graph = graph
     self.graph_builder = AssemblyGraphBuilder(container, span_cost=span_cost)
     self.graph_builder.G = graph
     self.query = query
     self.seqdb = seqdb
     query_seq = str(query.seq)
     if is_circular(query):
         query_seq = query_seq + query_seq
     self.stats = DNAStats(
         query_seq,
         repeat_window=stats_repeat_window,
         stats_window=stats_window,
         hairpin_window=stats_hairpin_window,
     )
     self.stats_single = DNAStats(
         str(query.seq),
         repeat_window=stats_repeat_window,
         stats_window=stats_window,
         hairpin_window=stats_hairpin_window,
     )
     self.logged_msgs = []
     # TODO: make a more sophisticated complexity function?
     # TODO: expose this to input parameters
     self.COMPLEXITY_THRESHOLD = SequenceScoringConfig.complexity_threshold
     self.logger = logger(self)
     self.span_cost = span_cost
     self.stages = stages
     self.edge_threshold = edge_threshold
Example #8
0
def _design_primers(
    template: str,
    region: Region,
    lseq: Union[None, str],
    rseq: Union[None, str],
    left_overhang: Union[None, str] = None,
    right_overhang: Union[None, str] = None,
) -> Tuple[Dict[int, dict], Dict[str, Any]]:
    """Design primers flanking the specified.

    :class:`Region.<dasi.utils.Region>`. If the region is cyclic and spans the
    origin, this method will handle the appropriate manipulations to design
    primers around the origin and restore the locations of the resulting primer
    pairs.

    :param template: the template string to design primers
    :param region: region specified to design primers around. Regions are exclusive at
                    their end points (`.b` parameter)
    :param lseq: optionally provided left sequence
    :param rseq: optionally provided right sequence
    :param left_overhang: optionally provided left overhang sequence of the primer
    :param right_overhang: optionally provided right overhang sequence of the primer
    :return: tuple of pairs and the 'explain' dictionary.
    """
    design = primer3plus.new()
    design.logger = logger(design)
    design.settings.as_cloning_task()
    if region.direction == -1:
        region = region.flip()
        template = rc(template)

    if lseq and left_overhang:
        raise DasiSequenceDesignException
    if rseq and right_overhang:
        raise DasiSequenceDesignException

    if region.spans_origin():
        adjusted_template = region.get_slice(
            template) + region.invert()[0].get_slice(template)
        design.settings.template(adjusted_template)
        design.settings.included((0, len(region)))
        index = list(region) + list(region.invert()[0])
    else:
        design.settings.template(template)
        design.settings.included((region.a, len(region)))
        index = None
    if lseq:
        design.settings.left_sequence(lseq)
    if rseq:
        design.settings.right_sequence(rseq)

    if left_overhang is None:
        left_overhang = ""
    if right_overhang is None:
        right_overhang = ""

    design.settings.product_size((len(region), len(region)))
    design.settings.left_overhang(left_overhang)
    design.settings.right_overhang(right_overhang)
    design.PRIMER_PICK_ANYWAY = False
    design.PRIMER_MIN_ANNEAL_CHECK = Config.PRIMER3_MIN_ANNEAL_CHECK
    design.settings.use_overhangs()
    design.settings.long_ok()

    design.logger.set_level("INFO")

    # TODO: remove debugging code
    try:
        pairs, explain = design.run_and_optimize(Config.PRIMER3_N_RUNS,
                                                 pick_anyway=True)
    except Exception as e:
        import json

        print(json.dumps(dict(design.params.items()), indent=2))
        raise e
    if index is not None:
        for pair in pairs.values():
            loc = pair["LEFT"]["location"]
            pair["LEFT"]["location"] = (index[loc[0]], loc[1])

            loc = pair["RIGHT"]["location"]
            pair["RIGHT"]["location"] = (index[loc[0]], loc[1])
    return pairs, explain