-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: Added HPLC Reader (as tar archive) #102
Open
StarmanMartin
wants to merge
1
commit into
master
Choose a base branch
from
101-new-hplc-reader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import logging | ||
import os | ||
import shutil | ||
import tempfile | ||
import tarfile | ||
import hplc as ph | ||
from converter_app.readers.helper.base import Reader | ||
from converter_app.readers.helper.reader import Readers | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HplcReader(Reader): | ||
""" | ||
Reads tarballed hplc files with extension .tar.gz | ||
""" | ||
identifier = 'hplc_reader' | ||
priority = 5 | ||
|
||
def __init__(self, file): | ||
super().__init__(file) | ||
self.df = None | ||
self.temp_dir = None | ||
|
||
def check(self): | ||
""" | ||
:return: True if it fits | ||
""" | ||
result = self.file.name.endswith(".gz") or self.file.name.endswith(".xz") or self.file.name.endswith(".tar") | ||
if result: | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
self.temp_dir = temp_dir | ||
with tempfile.NamedTemporaryFile(delete=True) as temp_pdf: | ||
try: | ||
# Save the contents of FileStorage to the temporary file | ||
self.file.fp.save(temp_pdf.name) | ||
if self.file.name.endswith(".gz"): | ||
mode = "r:gz" | ||
elif self.file.name.endswith(".xz"): | ||
mode = "r:xz" | ||
elif self.file.name.endswith(".tar"): | ||
mode = "r:" | ||
else: | ||
return False | ||
with tarfile.open(temp_pdf.name, mode) as tar: | ||
tar.extractall(self.temp_dir) | ||
tar.close() | ||
|
||
for p in os.listdir(self.temp_dir): | ||
file_path = os.path.join(self.temp_dir, p) | ||
self.df = ph.read_chromatograms(file_path) | ||
break | ||
except ValueError: | ||
return False | ||
if not result and self.temp_dir is not None and os.path.exists(self.temp_dir) and os.path.isdir(self.temp_dir): | ||
shutil.rmtree(self.temp_dir) | ||
return result | ||
|
||
def prepare_tables(self): | ||
tables = [] | ||
|
||
keys = list(self.df.keys()) | ||
waves = [x for x in keys if x.startswith('Wave')] | ||
waves.sort() | ||
time = self.df['time'] | ||
for wave_key in waves: | ||
wave = self.df[wave_key] | ||
table = self.append_table(tables) | ||
kv = wave_key.split('_') | ||
table['metadata'][kv[0]] = str(kv[1]) | ||
table['metadata']['AllWaves'] = str(waves) | ||
for i, t in enumerate(time): | ||
table['rows'].append([t, float(wave[i])]) | ||
|
||
table['columns'] = [{ | ||
'key': str(idx), | ||
'name': f'{value}' | ||
} for idx, value in enumerate(['Time', 'Wavelength'])] | ||
return tables | ||
|
||
|
||
Readers.instance().register(HplcReader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this install the binary artifact from the repo or does this trigger the building from the source ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What dependencies are needed at runtime ? Does the Readme needs an update