Skip to content

Commit

Permalink
update file metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
ypriverol committed Oct 14, 2024
1 parent 510b8e6 commit 6ebb973
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
19 changes: 15 additions & 4 deletions quantmsio/commands/attach_file_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
@click.option(
"--category",
type=click.Choice(
["sdrf_file","feature_file", "psm_file", "differential_file", "absolute_file"],
["sdrf_file", "feature_file", "psm_file", "differential_file", "absolute_file"],
case_sensitive=False,
),
help="The type of file that will be registered",
required=True,
)
@click.option("--is_folder", help="A boolean value that indicates if the file is a folder or not", is_flag=True)
@click.option("--partition_fields", multiple=True, type=str, help="The fields that are used to partition the data in the file. This is used to optimize the data retrieval and filtering of the data. This field is optional", required=False)
@click.option(
"--partition_fields",
multiple=True,
type=str,
help="The fields that are used to partition the data in the file. This is used to optimize the data retrieval and filtering of the data. This field is optional",
required=False,
)
@click.option("--replace_existing", help="Whether to delete old files", is_flag=True)
def attach_file_to_json(project_file, attach_file, category, is_folder, partition_fields, replace_existing):
"""
Expand All @@ -31,6 +37,11 @@ def attach_file_to_json(project_file, attach_file, category, is_folder, partitio
:param replace_existing: Whether to delete old files
"""
register = ProjectHandler(project_json_file=project_file)
register.register_file(attach_file, category, is_folder=is_folder, partition_fields=list(partition_fields), replace_existing=replace_existing)
register.register_file(
attach_file,
category,
is_folder=is_folder,
partition_fields=list(partition_fields),
replace_existing=replace_existing,
)
register.save_updated_project_info(output_file_name=project_file)

28 changes: 18 additions & 10 deletions quantmsio/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(self, project_accession: str = None, project_json_file: str = None)
else:
self.load_project_info(project_json_file)
self.project_accession = self.project.project_info["project_accession"]
self.extensions = ["sdrf_file","psm_file","feature_file","differential_file","absolute_file"]
self.extensions = ["sdrf_file", "psm_file", "feature_file", "differential_file", "absolute_file"]

def load_project_info(self, project_json_file: str = None):
"""
Expand Down Expand Up @@ -145,7 +145,9 @@ def add_software_provider(self, sortware_name="", sortware_version=""):
self.project.project_info["software_provider"]["name"] = sortware_name
self.project.project_info["software_provider"]["version"] = sortware_version

def add_quantms_path(self, path_name: str, file_category: str, is_folder=False, partition_fields=None, replace_existing=None):
def add_quantms_path(
self, path_name: str, file_category: str, is_folder=False, partition_fields=None, replace_existing=None
):
"""
Add a quantms file to the project information. The file name will be generated automatically. Read more about the
quantms file naming convention in the docs folder of this repository
Expand All @@ -158,21 +160,21 @@ def add_quantms_path(self, path_name: str, file_category: str, is_folder=False,
"""
if "quantms_files" not in self.project.project_info:
self.project.project_info["quantms_files"] = []

obj_index = None
for index, obj in enumerate(self.project.project_info["quantms_files"]):
if file_category in obj:
obj_index = index
if(obj_index is not None and replace_existing):
if obj_index is not None and replace_existing:
self.project.project_info["quantms_files"][obj_index][file_category] = []
path_name = path_name.replace('\\','/')
path_name = path_name.replace("\\", "/")
record = {
'path_name': path_name,
'is_folder': is_folder,
"path_name": path_name,
"is_folder": is_folder,
}
if partition_fields is not None and isinstance(partition_fields, list):
record['partition_fields'] = partition_fields
record["partition_fields"] = partition_fields

if obj_index is not None:
self.project.project_info["quantms_files"][obj_index][file_category].append(record)
else:
Expand All @@ -181,7 +183,13 @@ def add_quantms_path(self, path_name: str, file_category: str, is_folder=False,
def register_file(self, path_name, file_category, is_folder=False, partition_fields=None, replace_existing=None):
if file_category not in self.extensions:
raise Exception(f"The {file_category} not in {self.extensions}")
self.add_quantms_path(path_name, file_category, is_folder=is_folder, partition_fields=partition_fields, replace_existing=replace_existing)
self.add_quantms_path(
path_name,
file_category,
is_folder=is_folder,
partition_fields=partition_fields,
replace_existing=replace_existing,
)

def save_project_info(
self,
Expand Down
1 change: 0 additions & 1 deletion quantmsio/operate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,3 @@ def load_de_or_ae(path):
line = f.readline()
f.seek(pos - 1)
return pd.read_csv(f, sep="\t"), content

0 comments on commit 6ebb973

Please sign in to comment.