Skip to content

Commit

Permalink
feat: added internal annotation with multiple value
Browse files Browse the repository at this point in the history
  • Loading branch information
dmartmillan committed Aug 1, 2022
1 parent 230a762 commit 172222f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ For more details check our [Installation](https://openvariant.readthedocs.io/en/
## Examples

We offer a bunch of [Examples](examples) to we be able to understand how OpenVariant can be applied. Also, check
[Examples](./examples) section in OpenVariant's documentation.
[Examples](https://openvariant.readthedocs.io/en/latest/examples.html) section in OpenVariant's documentation.

## Contributing

Expand Down
1 change: 1 addition & 0 deletions annotation_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ annotation: # Columns to parse
# be extracted and parsed as values in the output
- type: 'internal'
field: string
value: string # Text to represent multiple fieldSource; optional
fieldSource: # Columns to look for in the input files; required
- string
- string
Expand Down
15 changes: 7 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from os import getcwd

from openvariant import find_files, Annotation, Variant
from openvariant import findfiles, Annotation, Variant

# where = "VAR != 4 AND (VAR != 5 OR VAR != 10)"
# where_clauses = parse_where(where)
Expand All @@ -14,11 +14,10 @@
#print(res)


annotation = Annotation(f"{getcwd()}/tests/data/dataset/dataset.yaml")
#annotation = Annotation(f"{getcwd()}/tests/data/dataset/dataset.yaml")

#for file, _ in find_files(f"{getcwd()}/tests/data/dataset/"):

result = Variant(f"{getcwd()}/tests/data/dataset/sample3/", annotation)
for line in result.read():
print(f"Line in a dict: {line}")
break
for file, ann in findfiles(f"{getcwd()}/tests/data/dataset/sample3"):
result = Variant(file, ann)
for line in result.read():
print(f"Line in a dict: {line}")
break
3 changes: 2 additions & 1 deletion openvariant/annotation/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def _static_builder(x: dict, base_path: str = None) -> StaticBuilder:
value = x[AnnotationKeys.VALUE.value]
except KeyError:
raise KeyError('Static annotation is wrong specified.')

return AnnotationTypes.STATIC.name, value


Expand All @@ -109,7 +110,7 @@ def _internal_builder(x: dict, base_path: str = None) -> InternalBuilder:
try:
value = x[AnnotationKeys.VALUE.value]
except KeyError:
value = float('nan')
value = None

return AnnotationTypes.INTERNAL.name, x[AnnotationKeys.FIELD_SOURCE.value], Builder("(lambda y: y)") \
if AnnotationKeys.FUNCTION.value not in x or x[AnnotationKeys.FUNCTION.value] is None or \
Expand Down
30 changes: 23 additions & 7 deletions openvariant/annotation/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from openvariant.plugins.context import Context

StaticProcess = Tuple[str, float or int or str, Callable]
InternalProcess = Tuple[str, Optional[int], Callable]
InternalProcess = Tuple[str, Tuple[dict, str], str]
FilenameProcess = Tuple[str, float or int or str, Callable]
DirnameProcess = Tuple[str, float or int or str, Callable]
PluginProcess = Tuple[str, Context, Callable]
Expand Down Expand Up @@ -59,18 +59,34 @@ def _internal_process(x: InternalBuilder, original_header: List = [] or None, fi
Callable
Function to execute on the fixed value
"""
field_pos = None
field_pos = {}
try:
for i, h in enumerate(original_header):
if h in set(x[1]):
field_pos = i
break
header_dict = {field: num for num, field in list(enumerate(original_header))}
for source in x[1]:
if isinstance(source, List):
for s in source:
try:
field_pos.update({s: header_dict[s]})
except KeyError:
field_pos = {}
pass
if len(field_pos) == len(source):
break
else:
field_pos = {}
else:
try:
field_pos = {source: header_dict[source]}
break
except KeyError:
pass

except TypeError:
raise TypeError(f'Unable to parser {x[0]} annotation')
except SyntaxError:
raise SyntaxError(f'Unable to parser function lambda on {x[0]} annotation')

return AnnotationTypes.INTERNAL.name, field_pos, x[2]
return AnnotationTypes.INTERNAL.name, (field_pos, x[3]), x[2]


def _filename_process(x: FilenameBuilder, original_header: List = [] or None, file_path: str = None,
Expand Down
10 changes: 9 additions & 1 deletion openvariant/variant/variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,15 @@ def _parser(self, file_path: str, annotation: Annotation, group_by: str, display
elif type_ann == AnnotationTypes.MAPPING.name:
mapping_values[head] = header[head]
elif type_ann == AnnotationTypes.INTERNAL.name:
value = line[value] if value is not None else None
if len(value[0]) == 1:
pos = list(value[0].values())[0]
value = line[pos] if value is not None else None
else:
pos = {}
for val, position in value[0].items():
pos.update({val: line[position]})
value = value[1].format(**pos)

line_dict[head] = _parse_field(value, func)
else:
line_dict[head] = _parse_field(value, func)
Expand Down
6 changes: 6 additions & 0 deletions tests/data/dataset/sample3/sample3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pattern:
format: tsv

columns:
- ID
- CHROMOSOME
- POSITION
- REF
Expand All @@ -12,6 +13,11 @@ columns:
- PLATFORM

annotation:
- type: internal
field: ID
value: '{Hugo_Symbol}_{NCBI_Build}'
fieldSource:
- ['Hugo_Symbol', 'NCBI_Build']
- type: internal
field: CHROMOSOME
fieldSource:
Expand Down

0 comments on commit 172222f

Please sign in to comment.