Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cli #32

Merged
merged 45 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
087dea3
add click arguments and options to main function
kdp-cloud Mar 29, 2024
ca0c459
Generate settings file on install
kdp-cloud Mar 29, 2024
161017f
Move external references logic to separate file
kdp-cloud Mar 29, 2024
193e506
change entry point to `cli`
kdp-cloud Mar 29, 2024
8fd8525
Don't overwrite config file by default when reinstalling
kdp-cloud Mar 29, 2024
0c536e3
Add logging
kdp-cloud Mar 29, 2024
9464bc2
Add constraints on the size of the files and the number of older log …
kdp-cloud Mar 29, 2024
5513c67
Add logging documentation
kdp-cloud Mar 29, 2024
4d9cfd5
Add ISA JSON models for validation
kdp-cloud Mar 29, 2024
b4b4b53
Add validator for assay comments
kdp-cloud Mar 31, 2024
e7b8570
Exclude all `__init__.py` files from coverage
kdp-cloud Mar 31, 2024
9e2a90b
Add log files
kdp-cloud Mar 31, 2024
6002c8c
Make model validation more strict
kdp-cloud Apr 15, 2024
8f5a752
Remove unused variables
kdp-cloud Apr 15, 2024
92fd587
linting
kdp-cloud Apr 15, 2024
908bb4e
Use pydantic classes instead of dictionaries
kdp-cloud Apr 15, 2024
9eb0604
Migrate from `@validator` to `@field_validator`
kdp-cloud Apr 15, 2024
35de605
Move TargetRepository and TARGET_REPO_KEY to shared file
kdp-cloud Apr 15, 2024
a55b4bf
Add EVA to TargetRepository
kdp-cloud Apr 15, 2024
e49a453
Remove empty unused validation.py
kdp-cloud Apr 15, 2024
f37bacf
update target repo validator
kdp-cloud Apr 16, 2024
96826b9
add validator tests
kdp-cloud Apr 16, 2024
6120d68
linting
kdp-cloud Apr 16, 2024
01f4e1a
Modify function to write different levels
kdp-cloud Apr 16, 2024
839f71b
Fix import statements
kdp-cloud Apr 16, 2024
1c44629
Add context to CLI
kdp-cloud Apr 16, 2024
81f04fe
Add URLs to the config file
kdp-cloud Apr 16, 2024
906568c
Change settings dir dependend of environmental variable.
kdp-cloud Apr 19, 2024
e851e4d
Print to std err when error is logged
kdp-cloud Apr 19, 2024
51f176e
Add health check command
kdp-cloud Apr 19, 2024
44e7ad9
Add IsaJson wrapper around Investigation
kdp-cloud Apr 19, 2024
e474ea1
Formatting
kdp-cloud Apr 19, 2024
4c85ac1
Invalid material type according to the specs:
kdp-cloud Apr 19, 2024
359c502
Formatting
kdp-cloud Apr 19, 2024
cc88350
move model to mars_lib
kdp-cloud Apr 19, 2024
e97220a
Add validation command to CLI
kdp-cloud Apr 19, 2024
48cd972
Remove phone nr validator
kdp-cloud Apr 19, 2024
52445f0
Fix for ARC
kdp-cloud Apr 19, 2024
e57ce82
Add option for ISA JSON's when investigation is root
kdp-cloud Apr 22, 2024
eb5fb0b
Update tests
kdp-cloud Apr 22, 2024
d2238c0
Update README
kdp-cloud Apr 22, 2024
b987593
Fix failing tests
kdp-cloud Apr 22, 2024
cd6a77e
Add pydantic to requirements
kdp-cloud Apr 22, 2024
919fc1d
Fix typo
kdp-cloud Apr 24, 2024
f2861a8
add l click
bedroesb May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cover/
*.pot

# Django stuff:
*.log
**.log
local_settings.py
db.sqlite3
db.sqlite3-journal
Expand Down
195 changes: 194 additions & 1 deletion mars-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Installing the mars-cli

This installation procedure describes a typical Linux installation. This application can perfectly work on Windows and MacOS but some of the steps might be different.

Installing the mars-cli from source:

```sh
cd mars-cli # Assuming you are in the root folder
cd mars-cli # Assuming you are in the root folder of this project
pip install .
```

Expand All @@ -13,6 +15,197 @@ If you want to install the optional testing dependencies as well, useful when co
pip install .[test]
```

If you want to overwrite the `settings.ini` file when reinstalling, you need to set the environmental variable `OVERWRITE_SETTINGS` to `True`:

```sh
OVERWRITE_SETTINGS=True pip install .[test]
```
Installing the MARS-cli, will by default create a `.mars` directory in the home directory to store settings and log files.
If you wish to create the `.mars` directory in another place, you must specify the `MARS_SETTINGS_DIR` variable and set it to the desired path:

```sh
export MARS_SETTINGS_DIR=<path/to/parent_folder/containing/.mars>
```

If you want to make it permanent, you can run to following commands in the terminal.
Note: replace `.bashrc` by the config file of your shell.

```sh
echo '# Add MARS setting directory to PATH' >> $HOME/.bashrc
echo 'export MARS_SETTINGS_DIR=<path/to/parent_folder/containing/.mars>' >> $HOME/.bashrc
```

Once installed, the CLI application will be available from the terminal.

# Configuration

Installing this application will also generate a `settings.ini` file in `$HOME/.mars/`.

```
[logging]
log_level = ERROR
log_file = /my/logging/directory/.mars/app.log
log_max_size = 1024
log_max_files = 5
```

## Logging

The MARS-CLI will automatically log events to a `.log` file.

__log_level__: The verbosity of logging can be set to three different levels
- CRITICAL: Only critical messages will be logged. __Not recommended!__
- ERROR: Errors and critical messages will be logged.
- WARNING: Warnings, errors and critical messages will be logged.
- INFO: All events are logged.
- DEBUG: For debugging purpose only. __Not recommended as it might log more sensitive information!__
The default setting is ERROR. So only errors are logged!

__log_file__: The path to the log file. By default this will be in `$HOME/.mars/app.log`.

__log_max_size__: The maximum size in kB for the log file. By default the maximum size is set to 1024 kB or 1 MB.

__log_max_files__: The maximum number of old log files to keep. By default, this is set to 5

## Target respoistory settings
kdp-cloud marked this conversation as resolved.
Show resolved Hide resolved

Each of the target repositories have a set of settings:

- development-url: URL to the development server when performing a health-check
apriltuesday marked this conversation as resolved.
Show resolved Hide resolved
- development-submission-url: URL to the development server when performing a submission
- production-url: URL to the production server when performing a health-check
- production-submission-url: URL to the production server when performing a submissionW

# Using the MARS-CLI

If you wish to use a different location for the `.mars' folder:

```sh
export MARS_SETTINGS_DIR=<path/to/parent_folder/containing/.mars>
mars-cli [options] <command> ARGUMENT
```

## Help

The mars-cli's help text can be found from the command line as such:

```sh
mars-cli --help
```

Output:

```
➜ mars-cli --help
Usage: mars-cli [OPTIONS] COMMAND [ARGS]...

Options:
-d, --development Boolean indicating the usage of the development
environment of the target repositories. If not present,
the production instances will be used.
--help Show this message and exit.

Commands:
health-check Check the health of the target repositories.
submit Start a submission to the target repositories.
validate-isa-json Validate the ISA JSON file.
```

or for a specific command:

```sh
mars-cli submit --help
```

Output:

```
➜ mars-cli submit --help
############# Welcome to the MARS CLI. #############
Running in Production environment
Usage: mars-cli submit [OPTIONS] CREDENTIALS_FILE ISA_JSON_FILE

Start a submission to the target repositories.

Options:
--submit-to-ena BOOLEAN Submit to ENA.
--submit-to-metabolights BOOLEAN
Submit to Metabolights.
--investigation-is-root BOOLEAN
Boolean indicating if the investigation is
the root of the ISA JSON. Set this to True
if the ISA-JSON does not contain a
'investigation' field.
--help Show this message and exit.
```

## Development

By default the mars-CLI will try to submit the ISA-JSON's metadata towards the repositories' production servers. Passing the development flag will run it in development mode and substitute the production servers with the development servers.

## Health check repository services

You can check whether the supported repositories are healthy, prior to submission, by doing a health-check.

```sh
mars-cli health-check
```

Output:

```
➜ mars-cli health-check
############# Welcome to the MARS CLI. #############
Running in Production environment
Checking the health of the target repositories.
Checking production instances.
Webin (https://www.ebi.ac.uk/ena/submit/webin/auth) is healthy.
ENA (https://www.ebi.ac.uk/ena/submit/webin-v2/) is healthy.
Biosamples (https://www.ebi.ac.uk/biosamples/samples/) is healthy.
```

## Submitting to repository services

TODO

### Options

- `--submit-to-ena`: By default set to `True`. Will try submit ISA-JSON metadata towards ENA. Setting it to `False` will skip sending the ISA-JSON's metadata to ENA.

```sh
mars-cli submit --submit-to-ena False my-credentials my-isa-json.json
```

- `--submit-to-metabolights`: By default set to `True`. Will try submit ISA-JSON metadata towards Metabolights. Setting it to `False` will skip sending the ISA-JSON's metadata to Metabolights.

```sh
mars-cli submit --submit-to-metabolights False my-credentials my-isa-json.json
```

`--investigation-is-root`: By default this flag is set to false, maening the ISA-JSON should have the `investigation` key at the root level. In case the root level __IS__ the investigation (`investigation` level is omitted), you need set the flag `--investigation-is-root` to `True` in order to validate the ISA-JSON.

```sh
mars-cli submit --investigation-is-root True my-credentials my-isa-json.json
```

## Validation of the ISA JSON

You can perform a syntactic validation of the ISA-JSON, without submitting to the target repositories.

__Note:__ This does not take validation into account from the repository's side. This does not guarantee successful submission.

```sh
mars-cli validate-isa-json --investigation-is-root True ../test-data/biosamples-input-isa.json
```

### Options

`--investigation-is-root`: By default this flag is set to false, maening the ISA-JSON should have the `investigation` key at the root level. In case the root level __IS__ the investigation (`investigation` level is omitted), you need set the flag `--investigation-is-root` to `True` in order to validate the ISA-JSON.

```sh
mars-cli validate-isa-json my-isa-investigation.json
```

# Extending BioSamples' records
The Python script ``biosamples-externalReferences.py`` defines a class BiosamplesRecord for managing biosample records. This class is designed to interact with the BioSamples database, allowing operations like fetching, updating, and extending biosample records.
The script takes in a dictionary of BioSamples' accessions and their associated external references, and expands the former with the latter.
Expand Down
71 changes: 71 additions & 0 deletions mars-cli/generate_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import configparser
import pathlib


def create_settings_file(settings_dir):
"""
Create a settings file with the specified log path and settings path.

Args:
settings_path (str): The path to the settings file.

Returns:
None
"""
log_path = settings_dir / "app.log"
settings_path = settings_dir / "settings.ini"
config = configparser.ConfigParser()
config["logging"] = {
"log_level": "ERROR",
"log_file": log_path,
"log_max_size": "1024",
"log_max_files": "5",
}

config["webin"] = {
"development-url": "https://wwwdev.ebi.ac.uk/ena/submit/webin/auth",
"development-token-url": "https://wwwdev.ebi.ac.uk/ena/submit/webin/auth/token",
"production-url": "https://www.ebi.ac.uk/ena/submit/webin/auth",
"production-token-url": "https://www.ebi.ac.uk/ena/submit/webin/auth/token",
}

config["ena"] = {
"development-url": "https://wwwdev.ebi.ac.uk/ena/submit/webin-v2/",
"development-submission-url": "https://wwwdev.ebi.ac.uk/ena/submit/drop-box/submit/?auth=ENA",
"production-url": "https://www.ebi.ac.uk/ena/submit/webin-v2/",
"production-submission-url": "https://www.ebi.ac.uk/ena/submit/drop-box/submit/?auth=ENA",
}

config["biosamples"] = {
"development-url": "https://wwwdev.ebi.ac.uk/biosamples/samples/",
"development-submission-url": "https://wwwdev.ebi.ac.uk/biosamples/samples/",
"production-url": "https://www.ebi.ac.uk/biosamples/samples/",
"production-submission-url": "https://www.ebi.ac.uk/biosamples/samples/",
}

with open(settings_path, "w") as config_file:
config.write(config_file)


def generate_config(overwrite, mars_home_dir):
"""
Generate the configuration file for the MARS CLI.

Returns:
None
"""
settings_dir = (
pathlib.Path.home() / ".mars"
if mars_home_dir == "HOME"
else pathlib.Path(mars_home_dir) / ".mars"
)

if not settings_dir.exists():
settings_dir.mkdir()

settings_path = settings_dir / "settings.ini"

if settings_path.exists() and not overwrite:
return

create_settings_file(settings_dir)
Loading