RosettaCommons / PyRosetta.notebooks

Jupyter Notebooks for learning the PyRosetta platform for biomolecular structure prediction and design
https://rosettacommons.github.io/PyRosetta.notebooks/
MIT License
447 stars 145 forks source link

Clean up imports #58

Closed OLaprevote closed 1 year ago

OLaprevote commented 3 years ago

Replace wildcard imports with explicit imports (as discussed on Twitter with @jjgray)

A few tweaks to what was discussed: rosetta was actually never imported, instead core and protocols from pyrosetta.rosetta are explicitly imported if needed. This makes relatively short lines while being more understandable:

minmover = protocols.minimization_packing.MinMover()
design_metrics = protocols.analysis.simple_metrics.RunSimpleMetricsMover()

This permits to avoid two pitfalls:

a_lot_of_code()

minmover = MinMover() design_metrics = RunSimpleMetricsMover()


`from pyrosetta.rosetta import core, protocols` has the advantages of both: the programmer doesn't need to go back to the imports to edit them, and the location is not drowned in many imports statements and is clearly stated in the code, making it easily reused; meanwhile it is (relatively) shorter, hence more easily read and written. This style could already be found in notebook [14.00](https://github.com/RosettaCommons/PyRosetta.notebooks/blob/200a6d5489f2108999563ae38c7e3fcdabe8f5fe/notebooks/14.00-RNA-Basics.ipynb).
Imports also followed these guidelines:
- If several commands from one module were used extensively then the module may be imported explicitly (`from pyrosetta.rosetta.protocols import antibody, docking`).
- If, from a module, only one functionality was used more than two times, then it may be imported explicitly.

A few common imports were also generalized to all notebooks when indicated by the guidelines:
- `import pyrosetta.rosetta.core.select.residue_selector as selections` (found in notebook [06.02](https://github.com/RosettaCommons/PyRosetta.notebooks/blob/200a6d5489f2108999563ae38c7e3fcdabe8f5fe/notebooks/12.01-RosettaAntibody-Framework-and-SimpleMetrics.ipynb) and [12.01](https://github.com/RosettaCommons/PyRosetta.notebooks/blob/200a6d5489f2108999563ae38c7e3fcdabe8f5fe/notebooks/12.01-RosettaAntibody-Framework-and-SimpleMetrics.ipynb))
- `import pyrosetta.rosetta.core.simple_metrics as sm` (found in notebook 12.01 importing `core.simple_metrics.metrics as sm`. This seemed more appropriate and versatile as it shortens both `sm.metrics` and `sm.per_residue_metrics`)
- `from pyrosetta.rosetta.core.pack import task`. Some notebooks stated: `from pyrosetta.rosetta.core.pack.task import TaskFactory, operators`. Instead, only importing `task` permits to clearly label TaskOperations as `task.operation` and TaskFactory is easily called, while making the import statement briefer:
```python
tf = task.TaskFactory()
tf.push_back(task.operation.InitializeFromCommandline())

Duplicates of these notebooks were all tested manually in an effort to avoid as many metadata or output changes as possible. Issues found were also found in the notebooks prior to changes. Chapter 16 notebooks were the least tested, but given that there imports were just rearranged to be PEP8 compliant there shouldn't be any problem (hopefully).

Off-topic comment (sorry): I am currently unemployed and looking for a PhD or job related to protein design. I would be more than glad to help writing jupyter notebooks, develop python package, use PyRosetta, do wet lab or all these at once. These changes were not done to advertise myself, but I guess I should try my luck.