提交:初始化

This commit is contained in:
longfellowJian 2025-03-27 14:51:37 +08:00
commit ceffeef30f
14 changed files with 1094 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
.idea/*
*.
__pycache__/*
build/*
dist/*
*.egg-info/*

127
LICENSE Normal file
View File

@ -0,0 +1,127 @@
木兰宽松许可证, 第2版
木兰宽松许可证, 第2版
2020年1月 http://license.coscl.org.cn/MulanPSL2
您对“软件”的复制、使用、修改及分发受木兰宽松许可证第2版“本许可证”的如下条款的约束
0. 定义
“软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
“贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
“贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
“法人实体”是指提交贡献的机构及其“关联实体”。
“关联实体”是指对“本许可证”下的行为方而言控制、受控制或与其共同受控制的机构此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
1. 授予版权许可
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
2. 授予专利许可
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
3. 无商标许可
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可但您为满足第4条规定的声明义务而必须使用除外。
4. 分发限制
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
5. 免责声明与责任限制
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
6. 语言
“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。
条款结束
如何将木兰宽松许可证第2版应用到您的软件
如果您希望将木兰宽松许可证第2版应用到您的新软件为了方便接收者查阅建议您完成如下三步
1 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
2 请您在软件包的一级目录下创建以“LICENSE”为名的文件将整个许可证文本放入该文件中
3 请将如下声明文本放入每个源文件的头部注释中。
Copyright (c) [Year] [name of copyright holder]
[Software Name] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
Mulan Permissive Software LicenseVersion 2
Mulan Permissive Software LicenseVersion 2 (Mulan PSL v2)
January 2020 http://license.coscl.org.cn/MulanPSL2
Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions:
0. Definition
Software means the program and related documents which are licensed under this License and comprise all Contribution(s).
Contribution means the copyrightable work licensed by a particular Contributor under this License.
Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
Legal Entity means the entity making a Contribution and all its Affiliates.
Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, control means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
1. Grant of Copyright License
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
2. Grant of Patent License
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
3. No Trademark License
No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4.
4. Distribution Restriction
You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
5. Disclaimer of Warranty and Limitation of Liability
THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW ITS CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
6. Language
THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL.
END OF THE TERMS AND CONDITIONS
How to Apply the Mulan Permissive Software LicenseVersion 2 (Mulan PSL v2) to Your Software
To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps:
i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
iii Attach the statement to the appropriate annotated syntax at the beginning of each source file.
Copyright (c) [Year] [name of copyright holder]
[Software Name] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.

53
README.md Normal file
View File

@ -0,0 +1,53 @@
# tbwk-opener
A python package to open and read tbwk files generated from the NanoDrop 2000 software.
Testing: ![Python application](https://github.com/Gillingham-Lab/tbwk-opener/workflows/Python%20application/badge.svg)
## Features
This library has so far only been tested with Nucleic Acid worksheets.
- Can import tbwk files to read measurements contained within the file
- Successfully reads y- and x-values of a recorded spectrum
- Also reports on tabled data contained in the worksheet (such as A260 for nucleic acids,
or direct nucleic concentration as chosen by the method)
A protein worksheet (no example provided) showed similar behaviour and should work without any issues.
## Installation
Make sure you've installed numpy and scipy before attempting to install tbwk-opener from pip, then use:
```shell script
pip install tbwk-opener
```
## Usage
Get all measurements from a worksheet and report on concentration using the absorption at 260 nm:
```python
from tbwk import Worksheet
worksheet = Worksheet.import_worksheet("examples/nanodrop-dna-measurements-01.twbk")
factor = (2.05 + 2.30)/2 # μM per absorption unit
for measurement in worksheet:
print(f"{measurement.title:20}{measurement.get_absorption_at(260)*factor:.2f} μM")
```
```text
wash 0.18 μM
blank 0.02 μM
BSD01 61.47 μM
BSD01 61.11 μM
BSD01 cntl A1 37.87 μM
wash 0.57 μM
BSD01 cntl A2 33.94 μM
wash 0.27 μM
BSD01 cntl A3 44.39 μM
BSD01 cntl A3 0.35 μM
BSD01 cntl A4 40.00 μM
wash 0.05 μM
wash 0.02 μM
```

Binary file not shown.

Binary file not shown.

32
setup.py Normal file
View File

@ -0,0 +1,32 @@
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="tbwk-opener", # Replace with your own username
version="0.1.0",
author="Basilius Sauter",
author_email="basilius.sauter@gmail.com",
description="An tool to open tbwk files generated by NanoDrop 2000",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Gillingham-Lab/tbwk-opener",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering :: Chemistry",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Utilities",
],
python_requires='>=3.6',
install_requires=[
"numpy",
"scipy",
"defusedxml",
]
)

224
tbwk/Measurement.py Normal file
View File

@ -0,0 +1,224 @@
import numpy as np
from scipy.interpolate import interp1d
from tbwk.RawOpener import Block
from tbwk.Properties import PropertyBag
"""
Block structure
(all is little endian)
The header of each block consists of block type (4 bytes), block size (4 bytes) and 4 empty bytes.
A measurement block (151) contains 3 nested blocks. The blocks content start at 520.
- 152, Contains a description and the measurement name:
12 bytes (?)
1 int8 (length), usually 28
n char, usually "Thermo Scientific DataCarton"
8 bytes, usually 00 00 00 00 00 f0 3f. Might be float64 for "1.0"
1 int8 (length)
n char, contains the measurement name
12 bytes, usually 3 int32: [1, 1, 0]
- 920, a wrapper for another block.
12 bytes (?)
Then another subblock starts with FE FF FF FF, with the content starting at offset 520 again.
- 921
12 bytes
1 int8 (length), usually 29
n char, usually "Thermo Scientific UV Spectrum"
8 bytes, usually 00 00 00 00 00 f0 3f.
- 922
12 bytes
\n
4 bytes
- 930
12 bytes
Then another subblock starts with FE FF FF FF, offset at 520
- 931
12 bytes
1 int8 (length), usually 30
n char, usually "Thermo Scientific Data-Vector " (sic)
1 int8 (length), usually 29
n char, usually "SpectrumFileFormat.UVSpectrum"
1 int8 (length)
n char, measurement name again.
8 bytes, windows filetime, eg 97 dc af d7 2c 46 d6 01 = 2020-06-19 13:29:06.5140375 (+2)
- 932
y values.
y label at offset 59, containing axis label in long and short:
1 int8
n char
1 int8
n char
8 bytes
21 bytes
1 int32 (4 bytes) indicating number of floats
n float64 containing y values
- 932
x values
y label at offset 59, containing axis label in long and short:
1 int8
n char
1 int8 (usually 0)
n char (empty)
8 bytes
21 bytes
1 int32 (4 bytes) indicating number of floats
n float64 containing y values
- 990 (10X?)
- All contain XML data.
- 62, content starts commonly at 520 after the block header.
12 bytes (?)
n char, XML content
"""
class Measurement:
"""
Represents a single measurement on a NanoDrop 2000.
Use the methods of this object for the most often used parameters, or
access the PropertyBag to access the tabular values as set by the measurement method.
"""
title: str = None
x_values: np.ndarray = None
x_label: str = None
y_values: np.ndarray = None
y_label: str = None
properties: PropertyBag = None
def __init__(self,
title: str,
x_values: np.ndarray,
x_label: str,
y_values: np.ndarray,
y_label: str,
properties: PropertyBag = None,
):
"""
:param title: Title of the measurement
:param x_values: numpy array containing x values
:param x_label: label for x values
:param y_values: numpy array containing y values (must be equal size)
:param y_label: label for y values
:param properties: A property bag
"""
assert len(x_values) == len(y_values)
self.title = title
self.x_values = x_values
self.x_label = x_label
self.y_values = y_values
self.y_label = y_label
self.properties = properties
def __repr__(self) -> str:
return f"<Measurement[{self.title}], {self.properties.get_method_title()}>"
def get_title(self) -> str:
"""
Returns the given sample name (title) of the measurement
"""
return self.title
def get_method_title(self) -> str:
"""
Returns the title of the method
"""
return self.properties.get_method_title()
def get_method_description(self) -> str:
"""
Returns the description of the method.
"""
return self.properties.get_method_description()
def get_x(self) -> np.ndarray:
"""
Returns x values as a numpy array, float64
Should usually contain the measured wavelengths.
"""
return self.x_values
def get_x_label(self) -> str:
"""
Returns the saved label for the x-axis.
"""
return self.x_label
def get_y(self) -> np.ndarray:
"""
Returns y values as a numpy array, float64
Should usually contain the measured absorption.
:return:
"""
return self.y_values
def get_y_label(self) -> str:
"""
Returns the saved label for the y-axis.
"""
return self.y_label
def get_property_bag(self) -> PropertyBag:
"""
Returns the property bag.
"""
return self.properties
def get_absorption_at(self, wavelength: float, from_spectrum=False) -> float:
""" Returns the absorption at a given wavelength.
If from_spectrum is set to true, the value comes always from the spectrum. If set to False, the measured
values are tried first. """
if from_spectrum is False:
wavelength_id = f"A{wavelength:.0f}"
if self.properties.has_property(wavelength_id):
value = self.properties.get_property(wavelength_id)
return value.get_value().get_value()
# Try if we find the measured value exactly
f = np.isin(self.x_values, wavelength, assume_unique=True)
result = self.y_values[f]
if len(result) == 1:
return result.item()
# If this does not work, we need to intrapolate
f = interp1d(self.x_values, self.y_values, kind="cubic")
return f(wavelength)
@classmethod
def from_block(cls, block):
assert block.type == Block.Measurement
# Create a property bag
properties = PropertyBag.from_xml(block.parsed_content[2].parsed_content)
# Create new measurement classes
ret = cls(
title=block.parsed_content[0].parsed_content[1].decode("utf8"),
x_values=block.parsed_content[1].parsed_content[2].parsed_content[2].parsed_content[3],
x_label=block.parsed_content[1].parsed_content[2].parsed_content[2].parsed_content[0].decode("utf8"),
y_values=block.parsed_content[1].parsed_content[2].parsed_content[1].parsed_content[3],
y_label=block.parsed_content[1].parsed_content[2].parsed_content[1].parsed_content[0].decode("utf8"),
properties=properties,
)
return ret

255
tbwk/Properties.py Normal file
View File

@ -0,0 +1,255 @@
import numpy as np
from typing import Dict, Optional, Tuple
class PropertyBag:
""" A container for tabled properties from a measurement. """
method_title: str
method_description: str
method_filename: str
_properties: Dict[str, "Property"]
def __init__(self):
self._properties = {}
def __repr__(self) -> str:
return f"<PropertyBag: n={len(self._properties)}>"
def get_method_title(self) -> str:
"""Returns the method title."""
return self.method_title
def get_method_description(self) -> str:
"""Returns the method description."""
return self.method_description
def add_property(self, property: "Property") -> None:
""" Adds a property.
:param property: A property
:return:
"""
self._properties[property.get_id()] = property
def has_property(self, id: str) -> bool:
return True if id in self._properties else False
def get_property(self, id: str) -> "Property":
return self._properties[id]
@classmethod
def from_xml(cls, xml_tree) -> "PropertyBag":
""" Creates a property bag from the corresponding xml tree. """
properties = cls()
assert xml_tree.tag == "PARAMOBJ"
# Get the spectrum results
spectrum_results = list(list(xml_tree)[0])[0]
assert spectrum_results.attrib["TYPE"] == "ParamProperties.SpectrumResults"
# Iterate over all var elements
for element in spectrum_results:
# Skip non-var elements
if element.tag != "VAR":
continue
var_name = element.attrib["NAME"]
if var_name == "m_MethodFilename":
properties.method_filename = element.text.strip()
elif var_name == "m_MethodTitle":
properties.method_title = element.text.strip()
elif var_name == "m_MethodDescription":
properties.method_description = element.text.strip()
elif var_name == "m_QuantGroups":
# m_QuantGroups contains all tabled measurements
for p in element:
if p.tag != "PARAM":
continue
property = Property.from_xml(p)
if property is not None:
properties.add_property(property)
return properties
class Property:
_id: str
_type: str
_value: "Value"
_raw: Optional["Value"]
def __init__(self,
id: str,
type: str,
value: "Value",
raw: Optional["Value"] = None,
):
"""
:param id: Identifier, also known as the property title
:param type: Property type, commonly equal to the identifier
:param value: Property value
:param raw: Raw property value if found.
"""
self._id = id
self._type = type
self._value = value
self._raw = raw
def __repr__(self) -> str:
return f"<Property[{self._id}]: {self._value}>"
def get_id(self) -> str:
return self._id
def get_type(self) -> str:
return self._type
def get_value(self) -> "Value":
return self._value
def get_raw_value(self) -> Optional["Value"]:
return self._raw
@classmethod
def from_xml(cls, xml_tree) -> Optional["Property"]:
property_title = None
property_type = None
property_value = None
property_raw = None
for var in xml_tree:
if var.tag != "VAR":
continue
if var.attrib["NAME"] == "m_Title":
property_title = var.text.strip()
elif var.attrib["NAME"] == "m_ResultType":
property_type = var.text.strip()
elif var.attrib["NAME"] == "m_QuantElements":
# m_QuantElements contains multiple param tags with the actual values
property_value, property_raw = Value.from_xml(var, property_type)
if property_value is not None:
property = cls(property_title, property_type, property_value, property_raw)
else:
property = None
return property
class Value:
""" Represents a value """
_title: str
_digits: int
_value: float
_unit: Optional[str]
_factor: Optional[float]
def __init__(self,
title: str,
digits: int,
value: float,
unit: Optional[str] = None,
factor: Optional[float] = None
):
self._title = title
self._digits = digits
self._value = value
self._unit = unit
self._factor = factor
def get_title(self) -> str:
return self._title
def get_digits(self) -> int:
return self._digits
def get_value(self) -> float:
return self._value
def has_unit(self) -> bool:
return self._unit is not None
def get_unit(self) -> Optional[str]:
return self._unit
def get_factor(self) -> Optional[float]:
return self._factor
def get_formatted_value(self) -> str:
""" Returns the value with the set number of digits and, if given, the unit. """
if self.has_unit():
return f"{self._value:.{self._digits}f} {self._unit}"
else:
return f"{self._value:.{self._digits}f}"
def __repr__(self):
return f"<Value[{self._title}]: {self._value:.{self._digits}f}>"
@classmethod
def from_xml(cls, xml_tree, type) -> Tuple[Optional["Value"], Optional["Value"]]:
value = None
raw_value = None
title = None
raw_title = None
num_digits = None
raw_num_digits = None
unit = None
factor = None
params = {}
for param in xml_tree:
if param.tag != "PARAM":
continue
element = {}
for var in param:
if var.tag != "VAR":
continue
converted_value = var.text.strip()
if var.attrib["TYPE"] == "System.Double":
converted_value = float(converted_value)
elif var.attrib["TYPE"] == "System.Int32":
converted_value = int(converted_value)
element[var.attrib["NAME"]] = converted_value
params[element["m_ResultType"]] = element
if type in params:
value = params[type]["m_Value"]
num_digits = params[type]["m_NumDigits"]
title = params[type]["m_Title"]
if type + "Unit" in params:
unit = params[type + "Unit"]["m_Value"]
if type + "Factor" in params:
factor = params[type + "Factor"]["m_Value"]
if type + "Raw" in params:
raw_value = params[type + "Raw"]["m_Value"]
raw_num_digits = params[type + "Raw"]["m_NumDigits"]
raw_title = params[type + "Raw"]["m_Title"] + "Raw"
if value is not None:
tabled_value = cls(title, num_digits, value, unit, factor)
else:
tabled_value = None
if raw_title is not None:
raw_value = cls(raw_title, raw_num_digits, raw_value)
else:
raw_value = None
return tabled_value, raw_value

216
tbwk/RawOpener.py Normal file
View File

@ -0,0 +1,216 @@
try:
import defusedxml.cElementTree as ET
except ImportError:
try:
import defusedxml.ElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import datetime
import numpy as np
class Block:
""" Represents a "Block" in a tbwk file."""
Measurement = 151
head = None
type = None
parts = None
content = None
offset = None
def __init__(self, head, type, size, content, offset):
self.head = head
self.type = type
self.size = size
self.content = content
self.offset = offset
if type == 62:
self.parse_xml()
elif type == 63:
self.parse_xml()
elif type == 150:
self.parse_150()
elif type == 151:
self.parse_subblock()
elif type == 152:
self.parse_152()
elif type == 920:
self.parse_subblock()
elif type == 921:
self.parse_921()
elif type == 922:
self.parsed_content = None
elif type == 930:
self.parse_subblock()
elif type == 931:
self.parse_931()
elif type == 932:
self.parse_uv()
elif type == 990:
self.parse_xml()
elif type == 991:
self.parse_991()
elif type < 10000:
raise Exception(f"No clue what block {type} is.")
def __repr__(self):
# alt_types = (int.from_bytes(self.head[:1], "little"), int.from_bytes(self.head[1:2], "little"))
return f"<TBWK-Block {self.type} ({self.describe()}) at offset {self.offset} with size {self.size}>"
def describe(self):
if self.type == 63:
return "Main XML"
elif self.type == 991:
return self.parsed_content[0]
elif self.type == 151:
return self.parsed_content[0].parsed_content[1]
def parse_xml(self):
xml = ET.fromstring(self.content[12:])
self.parsed_content = xml
def parse_subblock(self):
content = self.content[12:]
self.parsed_content = unpack(content)
def parse_uv(self):
content = self.content[59:]
offset = 0
length1, label_long = unpack_string(content)
length2, label_short = unpack_string(content[offset + length1 + 1:])
offset += 1 + length1 + 1 + length2
offset += 8 # 8 empty bytes
offset += 21 # 4 indicating dimension again, followed by int8, then by 2x float64.
# First of these floats was 1 for 0 for y-axis and 1 for x-axis
# Second was in both cases 0. Needs more "figuring out".
number_of_values = int.from_bytes(content[offset:offset + 4], "little")
values = content[offset + 4:]
values = np.frombuffer(values, dtype="<f8")
self.parsed_content = (label_long, label_short, number_of_values, values)
def parse_921(self):
length, title = unpack_string(self.content[12:])
self.parsed_content = title
def parse_931(self):
offset = 12
length, blocktype = unpack_string(self.content[offset:])
offset += 1 + length
length, fileformat = unpack_string(self.content[offset:])
offset += 1 + length
length, samplename = unpack_string(self.content[offset:])
offset += 1 + length
sampletime = unpack_datetime(self.content[offset:])
self.parsed_content = (blocktype, fileformat, samplename, sampletime)
def parse_991(self):
stringLength = int.from_bytes(self.content[12:13], "little")
objectType = self.content[13:13 + stringLength]
xml = ET.fromstring(self.content[13 + stringLength:])
self.parsed_content = (objectType, xml)
def parse_150(self):
content = self.content[12:]
offset = 0
length, workbook = unpack_string(content)
offset += length + 1 + 8
length, workbook_type = unpack_string(content[offset:])
offset += length + 1 + 12
length, source = unpack_string(content[offset:])
offset += length + 1
length, measurement_type = unpack_string(content[offset:])
offset += length + 1
length, application_name = unpack_string(content[offset:])
offset += length + 1
length, application_version = unpack_string(content[offset:])
offset += length + 1
self.parsed_content = (workbook, workbook_type, source, measurement_type, application_name, application_version)
def parse_152(self):
content = self.content[12:]
length, blocktype = unpack_string(content)
length, samplename = unpack_string(content[length + 1 + 8:])
self.parsed_content = (blocktype, samplename)
def unpack_string(content):
length = content[0]
string = content[1:1 + length]
return length, string
def unpack_datetime(filetime_bin):
""" Converts windows file time to a datetime object.
Windows file time is a 64bit integer noted in "100 ns since January 1, 1601 UTC"
For conversion, we need to adjust the epoch first and then convert to seconds.
Source:
https://support.microsoft.com/en-za/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime
"""
assert len(filetime_bin) == 8
filetime_int = int.from_bytes(filetime_bin, "little")
unixtime_in_100ns = filetime_int - 116444736000000000 # Shift the epoch to January 1, 1970 UTC
unixtime_in_s = unixtime_in_100ns * 0.000_000_1 # 0.000_000_1 s/100 ns
return datetime.datetime.fromtimestamp(unixtime_in_s)
def unpack(content):
""" Unpacks a file or subfile and returns the individual blocks."""
assert content[0:4] == b"\xfe\xff\xff\xff"
header_size = int.from_bytes(content[32:36], "little")
# Unpack blocks
blocks = []
offset = 40 + header_size
while True:
block_head = content[offset:offset + 12]
block_type = int.from_bytes(content[offset:offset + 4], "little")
block_size = int.from_bytes(content[offset + 4:offset + 8], "little")
block_none = int.from_bytes(content[offset + 8:offset + 12], "little")
block = content[offset + 12:offset + 12 + block_size]
blocks.append(Block(block_head, block_type, block_size, block, offset))
offset += 12 + block_size
if offset == len(content):
break
return blocks

60
tbwk/Worksheet.py Normal file
View File

@ -0,0 +1,60 @@
from typing import List
from tbwk.RawOpener import unpack, Block
from tbwk.Measurement import Measurement
class Worksheet:
measurements: List[Measurement] = None
def __init__(self):
self.measurements = []
def __len__(self) -> int:
""" Returns the number of measurements within the worksheet. """
return len(self.measurements)
def __iter__(self):
for measurement in self.measurements:
yield measurement
def add_measurement(self, measurement: Measurement) -> None:
"""
Adds a measurement to the worksheet
:param measurement:
:return:
"""
self.measurements.append(measurement)
def import_worksheet(filename: str) -> Worksheet:
"""
imports a given filename and creates a tbwk.Worksheet object.
:param filename:
:return:
"""
with open(filename, "rb") as fh:
content = fh.read()
if content is None:
raise FileNotFoundError(f"File {filename} was not found.")
# Unpack the file
blocks = unpack(content)
# Start loading the worksheet with found data.
worksheet = Worksheet()
# Add measurements
for block in blocks:
if block.type == Block.Measurement:
# Add a measurement block
measurement = Measurement.from_block(block)
worksheet.add_measurement(measurement)
# ToDo: Import data from other blocks, too.
return worksheet

0
tbwk/__init__.py Normal file
View File

8
test.py Normal file
View File

@ -0,0 +1,8 @@
from tbwk import Worksheet
worksheet = Worksheet.import_worksheet("examples/nanodrop-dna-measurements-01.twbk")
factor = (2.05 + 2.30)/2 # μM per absorption unit
for measurement in worksheet:
print(f"{measurement.title:20}{measurement.get_absorption_at(260)*factor:.2f} μM")

View File

@ -0,0 +1,75 @@
import unittest
from tbwk import Worksheet
class NucleicAcidWorksheetTestCase(unittest.TestCase):
def setUp(self):
filenames = [
"examples/nanodrop-dna-measurements-01.twbk",
"examples/nanodrop-dna-measurements-02.twbk",
]
worksheets = []
for file in filenames:
worksheets.append(Worksheet.import_worksheet(file))
self.worksheets = worksheets
def test_number_of_measurements(self):
number_of_experiments = [
13, 6,
]
for i in range(len(self.worksheets)):
should = number_of_experiments[i]
actual = len(self.worksheets[i])
self.assertEqual(should, actual)
def test_measurements_titles(self):
measurement_titles = [
["wash", "blank", "BSD01", "BSD01", "BSD01 cntl A1", "wash", "BSD01 cntl A2",
"wash", "BSD01 cntl A3", "BSD01 cntl A3", "BSD01 cntl A4", "wash", "wash"],
["blank", "blank", "CF2", "CF1", "wash", "wash"],
]
for i in range(len(self.worksheets)):
worksheet = self.worksheets[i]
for j in range(len(worksheet)):
should = measurement_titles[i][j]
actual = worksheet.measurements[j].title
self.assertEqual(should, actual)
def test_measurements_axes(self):
x_should = "Wavelength (nm)"
y_should = "10mm Absorbance"
for i in range(len(self.worksheets)):
worksheet = self.worksheets[i]
for j in range(len(worksheet)):
x_actual = worksheet.measurements[j].x_label
y_actual = worksheet.measurements[j].y_label
self.assertEqual(x_should, x_actual, msg="X-Axis label not matching.")
self.assertEqual(y_should, y_actual, msg="Y-Axis label not matching.")
def test_absorption_at_wavelength(self):
measurement_values = [
[0.08149, 0.00837, 28.260080, 28.09851, 17.41371, 0.26109, 15.60493, 0.12195, 20.41074, 0.15920,
18.39145, 0.024534, 0.01116],
[0.87528, 0.00378, 7.41706, 6.30852, 0.047512, 0.03308],
]
for i in range(len(self.worksheets)):
worksheet = self.worksheets[i]
for j in range(len(worksheet)):
actual = worksheet.measurements[j].get_absorption_at(260)
should = measurement_values[i][j]
self.assertAlmostEqual(actual, should, 4)

View File

@ -0,0 +1,36 @@
import unittest
from tbwk.Properties import Value
class PropertiesValueTestCase(unittest.TestCase):
test_parameters = [
{"title": "A title", "digits": 3, "value": 1.26391, "unit": "mg/mL", "factor": 33.0,}
]
def test_getters(self):
for i in range(len(self.test_parameters)):
parameters = self.test_parameters[i]
with self.subTest(i=i, parameters=parameters):
value = Value(**parameters)
self.assertEqual(parameters["title"], value.get_title())
self.assertEqual(parameters["digits"], value.get_digits())
self.assertAlmostEqual(parameters["value"], value.get_value(), value.get_digits())
self.assertEqual(parameters["unit"], value.get_unit())
self.assertAlmostEqual(parameters["factor"], value.get_factor(), 3)
def test_if_value_gets_formatted_as_expected(self):
expected = [
"1.264 mg/mL",
]
for i in range(len(self.test_parameters)):
parameters = self.test_parameters[i]
should = expected[i]
with self.subTest(i=i, parameters=parameters):
value = Value(**parameters)
actual = value.get_formatted_value()
self.assertEqual(should, actual)