1
+ import os
2
+ from datetime import datetime
1
3
from io import TextIOWrapper
2
4
import requests
3
5
import json
11
13
input_json_schema_filepath ,
12
14
)
13
15
from mars_lib .credential import CredentialManager
14
- from mars_lib .isa_json import load_isa_json
16
+ from mars_lib .isa_json import (
17
+ load_isa_json ,
18
+ reduce_isa_json_for_target_repo ,
19
+ update_isa_json ,
20
+ )
15
21
from mars_lib .models .isa_json import IsaJson
22
+ from mars_lib .models .repository_response import RepositoryResponse
16
23
from mars_lib .target_repo import TargetRepository
17
24
from mars_lib .logging import print_and_log
18
25
from pydantic import ValidationError
22
29
from typing import List
23
30
24
31
32
+ def save_step_to_file (time_stamp : float , filename : str , isa_json : IsaJson ):
33
+ dir_path = f"tmp/{ datetime .now ().strftime ('%Y-%m-%dT%H:%M:%S' )} "
34
+ os .makedirs (dir_path , exist_ok = True )
35
+
36
+ with open (f"{ dir_path } /{ filename } .json" , "w" ) as f :
37
+ f .write (isa_json .model_dump_json (by_alias = True , exclude_none = True ))
38
+
39
+
40
+ DEBUG = os .getenv ("MARS_DEBUG" ) in ["1" , 1 ]
41
+
42
+
25
43
def submission (
26
44
credential_service_name : str ,
27
45
username_credentials : str ,
@@ -31,8 +49,9 @@ def submission(
31
49
investigation_is_root : bool ,
32
50
urls : dict [str , Any ],
33
51
file_transfer : str ,
52
+ output : str ,
34
53
data_file_paths = None ,
35
- ):
54
+ ) -> None :
36
55
# If credential manager info found:
37
56
# Get password from the credential manager
38
57
# Else:
@@ -59,6 +78,37 @@ def submission(
59
78
f"ISA JSON with investigation '{ isa_json .investigation .title } ' is valid."
60
79
)
61
80
81
+ time_stamp = datetime .timestamp (datetime .now ())
82
+
83
+ if DEBUG :
84
+ save_step_to_file (time_stamp , "0_Initial_ISA_JSON_in_model" , isa_json )
85
+
86
+ if all (
87
+ repo not in TargetRepository .available_repositories ()
88
+ for repo in target_repositories
89
+ ):
90
+ raise ValueError ("No target repository selected." )
91
+
92
+ if TargetRepository .BIOSAMPLES in target_repositories :
93
+ # Submit to Biosamples
94
+ biosamples_result = submit_to_biosamples (
95
+ isa_json = isa_json ,
96
+ biosamples_credentials = user_credentials ,
97
+ biosamples_url = urls ["BIOSAMPLES" ]["SUBMISSION" ],
98
+ webin_token_url = urls ["WEBIN" ]["TOKEN" ],
99
+ )
100
+ print_and_log (
101
+ f"Submission to { TargetRepository .BIOSAMPLES } was successful. Result:\n { biosamples_result .json ()} " ,
102
+ level = "info" ,
103
+ )
104
+ # Update `isa_json`, based on the receipt returned
105
+ bs_mars_receipt = RepositoryResponse .model_validate (
106
+ json .loads (biosamples_result .content )
107
+ )
108
+ isa_json = update_isa_json (isa_json , bs_mars_receipt )
109
+ if DEBUG :
110
+ save_step_to_file (time_stamp , "1_after_biosamples" , isa_json )
111
+
62
112
if TargetRepository .ENA in target_repositories :
63
113
# Step 1 : upload data if file paths are provided
64
114
if data_file_paths and file_transfer :
@@ -68,8 +118,8 @@ def submission(
68
118
submission_url = urls ["ENA" ]["DATA-SUBMISSION" ],
69
119
file_transfer = file_transfer ,
70
120
)
121
+
71
122
# Step 2 : submit isa-json to ena
72
- # TODO: Filter out other assays
73
123
ena_result = submit_to_ena (
74
124
isa_json = isa_json ,
75
125
user_credentials = user_credentials ,
@@ -78,40 +128,32 @@ def submission(
78
128
print_and_log (
79
129
f"Submission to { TargetRepository .ENA } was successful. Result:\n { ena_result .json ()} "
80
130
)
81
- # TODO: Update `isa_json`, based on the receipt returned
131
+ # Update `isa_json`, based on the receipt returned
132
+ ena_mars_receipt = RepositoryResponse .from_json (str (ena_result .content ))
133
+ isa_json = update_isa_json (isa_json , ena_mars_receipt )
134
+ if DEBUG :
135
+ save_step_to_file (time_stamp , "2_after_ena" , isa_json )
82
136
83
- elif TargetRepository .BIOSAMPLES in target_repositories :
84
- # Submit to Biosamples
85
- biosamples_result = submit_to_biosamples (
86
- isa_json = isa_json ,
87
- biosamples_credentials = user_credentials ,
88
- biosamples_url = urls ["BIOSAMPLES" ]["SUBMISSION" ],
89
- webin_token_url = urls ["WEBIN" ]["TOKEN" ],
90
- )
91
- print_and_log (
92
- f"Submission to { TargetRepository .BIOSAMPLES } was successful. Result:\n { biosamples_result .json ()} " ,
93
- level = "info" ,
94
- )
95
- # TODO: Update `isa_json`, based on the receipt returned
96
- elif TargetRepository .METABOLIGHTS in target_repositories :
137
+ if TargetRepository .METABOLIGHTS in target_repositories :
97
138
# Submit to MetaboLights
98
139
# TODO: Filter out other assays
99
140
print_and_log (
100
141
f"Submission to { TargetRepository .METABOLIGHTS } was successful" ,
101
142
level = "info" ,
102
143
)
103
144
# TODO: Update `isa_json`, based on the receipt returned
104
- elif TargetRepository .EVA in target_repositories :
145
+
146
+ if TargetRepository .EVA in target_repositories :
105
147
# Submit to EVA
106
148
# TODO: Filter out other assays
107
149
print_and_log (
108
150
f"Submission to { TargetRepository .EVA } was successful" , level = "info"
109
151
)
110
152
# TODO: Update `isa_json`, based on the receipt returned
111
- else :
112
- raise ValueError ("No target repository selected." )
113
153
114
- # TODO: Return the updated ISA JSON
154
+ # Return the updated ISA JSON
155
+ with open (f"{ output } .json" , "w" ) as f :
156
+ f .write (isa_json .model_dump_json (by_alias = True , exclude_none = True ))
115
157
116
158
117
159
def submit_to_biosamples (
@@ -130,7 +172,9 @@ def submit_to_biosamples(
130
172
biosamples_url ,
131
173
headers = headers ,
132
174
params = params ,
133
- json = isa_json .model_dump (by_alias = True , exclude_none = True ),
175
+ json = reduce_isa_json_for_target_repo (
176
+ isa_json , TargetRepository .BIOSAMPLES
177
+ ).model_dump (by_alias = True , exclude_none = True ),
134
178
)
135
179
136
180
if result .status_code != 200 :
@@ -158,7 +202,9 @@ def submit_to_ena(
158
202
submission_url ,
159
203
headers = headers ,
160
204
params = params ,
161
- json = isa_json .model_dump (by_alias = True , exclude_none = True ),
205
+ json = reduce_isa_json_for_target_repo (isa_json , TargetRepository .ENA ).model_dump (
206
+ by_alias = True , exclude_none = True
207
+ ),
162
208
)
163
209
164
210
if result .status_code != 200 :
0 commit comments