Skip to content

Add MWTabFile.download_results_file() for programmatic results file download#15

Closed
sagarbasnet74 wants to merge 1 commit into
MoseleyBioinformaticsLab:mainfrom
sagarbasnet74:main
Closed

Add MWTabFile.download_results_file() for programmatic results file download#15
sagarbasnet74 wants to merge 1 commit into
MoseleyBioinformaticsLab:mainfrom
sagarbasnet74:main

Conversation

@sagarbasnet74

Copy link
Copy Markdown

Summary

The CLI supports --results-files but MWTabFile has no programmatic way to download results files. This PR adds MWTabFile.download_results_file() to fill that gap.

Changes

  • Added download_results_file() to MWTabFile class in src/mwtab/mwtab.py
  • Uses existing study_id and analysis_id managed properties
  • Supports both MS_RESULTS_FILE and NMR_RESULTS_FILE sections
  • Allows optional requests.Session for connection pooling
  • Falls back to default filename if results dict lacks filename key

Tests

  • 8 new test cases in tests/test_download_results_file.py
  • All 330 tests pass (322 original + 8 new)

Usage

from mwtab import mwtab as mt
from mwtab import fileio, mwrest

url = mwrest.GenericMWURL({
    "context": "study",
    "input_item": "study_id", 
    "input_value": "ST000001",
    "output_item": "mwtab"
}).url

mwfile = next(fileio.read_with_class(url, mt.MWTabFile, {}))
path = mwfile.download_results_file(output_dir=".")

@hunter-moseley

Copy link
Copy Markdown
Member

Thank you for this pull request.
Due to your prodding, we are looking to add the results file download functionality into the API.
However, we will probably implement it through the mwrest submodule.

@sagarbasnet74

Copy link
Copy Markdown
Author

Thank you for the feedback, Dr. Moseley! That makes sense — keeping REST operations in mwrest.py maintains cleaner separation of concerns.

@sagarbasnet74

sagarbasnet74 commented Jun 9, 2026 via email

Copy link
Copy Markdown
Author

@ptth222

ptth222 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

I ended up moving the results file downloading into the MWRESTFile class, so it is now available there and through the read_mwrest function in the fileio module. I only had it in the CLI because I couldn't really imagine a use case where it would be needed in the API. I would expect that anyone wanting to work with these files would just use the CLI to grab them and then manipulate them programmatically. Any code written to download anything programmatically is just duplicating what the CLI already does, so it just seems unlikely to me that someone would reinvent the wheel for that.

Even though we won't be using it, I do have some feedback on the code you wrote if you are interested. None of this is meant to criticize or be negative.

  1. You can't assume Study and/or Analysis IDs exist. You need error checking on them. The "download_and_save_mwrest_file" function in cli.py has it as an example.
  2. You can ignore finding the filename. The filename should always be of the form "STXXXXXX_ANXXXXXX_Results.txt". Filenames that don't look like that are a mistake and that name doesn't appear when you get the file from the Workbench site.
  3. You can ignore finding the RESULT_FILE key. You don't need the RESULT_FILE key because it is notorious for being malformed and might not get read in correctly. It's probably better to check the response status when trying to download or looking for "RESULTS_FILE" in the raw lines like "download_and_save_mwrest_file" in cli.py does. I can pretty much guarantee that as it is right now it would not get the same results as using the --results-files option.
  4. "base_url" should instead use the "RESULTS_FILE_BASE_URL" from cli.py. This would likely need to be moved to mwtab.py and then imported into cli.py.
  5. Instead of using the requests package you can reuse code from fileio.py. You can look at "download_and_save_mwrest_file" in cli.py for how it does it.

Note that after my next commit "download_and_save_mwrest_file" changes a lot since I moved the results file stuff to MWRESTFile.

@ptth222 ptth222 closed this in 1b76e41 Jun 11, 2026
@hunter-moseley

Copy link
Copy Markdown
Member

Sagar,

Let's move the rest of your discussion off this issue and into email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants