Skip to content

Commit

Permalink
feat: add --direction option
Browse files Browse the repository at this point in the history
  • Loading branch information
breakthewall committed May 18, 2021
1 parent bc05eb0 commit 62f9e6e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 38 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release history

## 2.0.0
### 18 May, 2021
- feat: add --direction option to specify the direction of the transformation to complete

## 1.2.0
### 10 May, 2021
- fit to new release of rr_cache
Expand Down
10 changes: 10 additions & 0 deletions rxn_rebuild/Args.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def add_arguments(parser: ArgumentParser) -> ArgumentParser:
type=str,
help='Template (original) reaction identifier'
)
parser.add_argument(
'--direction',
type=str,
default='reverse',
choices=[
'forward', 'fwd',
'reverse', 'rev'
],
help='Direction of the transformation to complete (default: reverse)'
)
parser.add_argument(
'--version', '-v',
action='version',
Expand Down
1 change: 1 addition & 0 deletions rxn_rebuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def entry_point():
cache = cache,
rxn_rule_id = args.rxn_rule_id,
transfo = args.transfo,
direction = args.direction,
tmpl_rxn_id = args.tmpl_rxn_id,
logger = logger
)
Expand Down
2 changes: 1 addition & 1 deletion rxn_rebuild/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.1"
__version__ = "2.0.0"
79 changes: 42 additions & 37 deletions rxn_rebuild/rxn_rebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
def rebuild_rxn(
rxn_rule_id: str,
transfo: str,
direction: str = 'reverse',
tmpl_rxn_id: str = None,
cache: 'rrCache' = None,
logger: Logger = getLogger(__name__)
) -> str:

## INPUT TRANSFORMATION
trans_input = build_trans_input(transfo.replace(' ', '')) # remove whitespaces
# # If input transfo is provided in forward direction,
# # swap left and right sides
# if direction == 'forward' or direction == 'fwd':
# trans_input['left'], trans_input['right'] = trans_input['right'], trans_input['left']

## LOAD CACHE
if cache is None:
Expand All @@ -37,21 +42,25 @@ def rebuild_rxn(

## COMPLETE TRANSFORMATION
completed_transfos = {}
if tmpl_rxn_id is not None:
if not tmpl_rxn_id is None:
completed_transfos[tmpl_rxn_id] = complete_transfo(
trans_input,
cache.get('rr_reactions')[rxn_rule_id][tmpl_rxn_id],
tmpl_rxn_id,
cache,
trans_input=trans_input,
direction=direction,
rxn_rule=cache.get('rr_reactions')[rxn_rule_id][tmpl_rxn_id],
tmpl_rxn=cache.get('template_reactions')[tmpl_rxn_id],
tmpl_rxn_id=tmpl_rxn_id,
compounds=cache.get('cid_strc'),
logger=logger
)
else: # One completed transformation per template reaction
for tpl_rxn_id, rxn_rule in cache.get('rr_reactions')[rxn_rule_id].items():
completed_transfos[tpl_rxn_id] = complete_transfo(
trans_input,
rxn_rule,
tpl_rxn_id,
cache,
trans_input=trans_input,
direction=direction,
rxn_rule=rxn_rule,
tmpl_rxn=cache.get('template_reactions')[tpl_rxn_id],
tmpl_rxn_id=tpl_rxn_id,
compounds=cache.get('cid_strc'),
logger=logger
)

Expand All @@ -61,15 +70,24 @@ def rebuild_rxn(
def complete_transfo(
trans_input: Dict,
rxn_rule: Dict,
tmpl_rxn: Dict,
tmpl_rxn_id: str,
cache: 'rrCache',
compounds: Dict,
direction: str = 'reverse',
logger: Logger = getLogger(__name__)
) -> Dict:

completed_transfo = {}

logger.debug('REACTION RULE:'+str(dumps(rxn_rule, indent=4)))

# If input transfo is provided in forward direction,
# swap left and right sides of reaction rule, and
# change 'rel_direction'
if direction == 'forward' or direction == 'fwd':
rxn_rule['left'], rxn_rule['right'] = rxn_rule['right'], rxn_rule['left']
rxn_rule['rel_direction'] *= -1

## CHECK 1/2
# Check if the number of structures in the right part of SMILES of transformation to complete
# is equal to the number of products of the template reaction used to build the reaction rule.
Expand All @@ -84,18 +102,14 @@ def complete_transfo(
)

## TEMPLATE REACTION
tmpl_rxn = load_tmpl_rxn(
cache.get('template_reactions'),
tmpl_rxn_id,
rxn_rule['rel_direction'],
logger=logger
)
if rxn_rule['rel_direction'] == -1:
tmpl_rxn = swap_sides(tmpl_rxn, logger=logger)

## ADD MISSING COMPOUNDS TO THE FINAL TRANSFORMATION
missing_compounds = detect_missing_compounds(
tmpl_rxn,
rxn_rule,
cache.get('cid_strc'),
compounds,
logger=logger
)

Expand Down Expand Up @@ -214,38 +228,29 @@ def build_trans_input(
return trans_input


def load_tmpl_rxn(
tmplt_rxns: Dict,
rxn_id: str,
rr_direction: int,
def swap_sides(
rxn: Dict,
logger: Logger = getLogger(__file__)
) -> Dict:
"""
Seeks for the template reaction of ID rxn_id in the cache.
Swaps left and right sides.
Parameters
----------
tmplt_rxns: Dict
Template (original) reactions.
rxn_id: str
ID of the template reaction.
rr_direction: int
Direction of the reaction used to build the rule.
rxn: Dict
The reactions to swap sides of.
logger : Logger
The logger object.
Returns
-------
tmpl_rxn: Dict
template reaction looked for.
rxn: Dict
The reaction with swapped sides.
"""
rxn = tmplt_rxns[rxn_id]
if rr_direction == -1:
left = dict(rxn['left'])
rxn['left'] = rxn['right']
rxn['right'] = left
logger.debug('TEMPLATE REACTION:'+str(dumps(rxn, indent=4)))
return rxn
return {
'left': rxn['right'],
'right': rxn['left']
}


def detect_missing_compounds(
Expand Down
34 changes: 34 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,37 @@ def test_all_cmpds_ok_wocache(self):
}
}
)

def test_forward_direction(self):
rule_id = 'RR-02-a0cc0be463ff412f-16-F'
transfo = '[H]Oc1c([H])c([H])c([H])c([H])c1O[H].O=O>>[H]OC(=O)C([H])=C([H])C([H])=C([H])C(=O)O[H]'
direction = 'forward'
self.assertEqual(
rebuild_rxn(
rxn_rule_id = rule_id,
transfo = transfo,
direction = direction,
logger = self.logger
),
{
"MNXR96458": {
"full_transfo": "[H]Oc1c([H])c([H])c([H])c([H])c1O[H].O=O>>[H]OC(=O)C([H])=C([H])C([H])=C([H])C(=O)O[H].[H+].[H+]",
"added_cmpds": {
"left": {},
"right": {
"MNXM1": {
"stoichio": 2,
"formula": "H",
"smiles": "[H+]",
"inchi": "InChI=1S/p+1",
"inchikey": "GPRLSGONYQIRFK-UHFFFAOYSA-N",
"cid": "MNXM1",
"name": "H(+)"
}
},
"left_nostruct": {},
"right_nostruct": {}
}
}
}
)

0 comments on commit 62f9e6e

Please sign in to comment.