Skip to content

Commit

Permalink
Correctiong imports for module pgGraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
dubssieg committed Nov 29, 2023
1 parent 17c96f2 commit 214ad52
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 53 deletions.
1 change: 0 additions & 1 deletion pgGraphs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
from .abstractions import GFALine, GFAFormat, Orientation
from .gfaparser import GFAParser
from .graph import Graph
from .io import GFAIO
from .networkx import GFANetwork
8 changes: 4 additions & 4 deletions pgGraphs/abstractions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"Abstractions over GFA formats"
from enum import Enum, auto
from enum import Enum


class Orientation(Enum):
"Describes the way a node is read"
FORWARD = '+'
REVERSE = '-'
ANY = '?' | auto()
ANY = '?'


class GFAFormat(Enum):
Expand All @@ -16,7 +16,7 @@ class GFAFormat(Enum):
GFA1_1 = 'GFA1.1'
GFA1_2 = 'GFA1.2'
GFA2 = 'GFA2'
ANY = 'unknown' | auto()
ANY = 'unknown'


class GFALine(Enum):
Expand All @@ -26,4 +26,4 @@ class GFALine(Enum):
WALK = 'W'
PATH = 'P'
HEADER = 'H'
ANY = auto()
ANY = '?'
41 changes: 40 additions & 1 deletion pgGraphs/gfaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from typing import Callable
from json import loads, dumps
from os import path, stat
from abstractions import Orientation, GFALine
from tharospytools.path_tools import path_allocator
from pgGraphs.abstractions import Orientation, GFALine, GFAFormat


class GFAParser:
Expand Down Expand Up @@ -208,3 +209,41 @@ def read_gfa_line(datas: list[str], load_sequence_in_memory: bool = True) -> tup
return (datas[1], line_type, {**line_datas, **GFAParser.supplementary_datas(datas, 7)})
case GFALine.HEADER | GFALine.ANY:
return (None, line_type, GFAParser.supplementary_datas(datas, 1))

@staticmethod
def save_graph(graph, output_path: str) -> None:
"""Given a gfa Graph object, saves to a valid gfa file the Graph.
Args:
output_path (str): a path on disk where to save
output_format (GfaStyle): a format to choose for output.
if None, default graph format will be used.
"""
line_number: int = 0
with open(path_allocator(output_path), 'w', encoding='utf-8') as gfa_writer:
if graph.headers:
for header in graph.headers:
gfa_writer.write(
"H\t"+'\t'.join([f"{key}:{GFAParser.get_python_type(value)}:{value}" if not key.startswith('ARG') else str(value) for key, value in header.items()])+"\n")
if graph.segments:
for segment_name, segment_datas in graph.segments.items():
gfa_writer.write("S\t"+f"{segment_name}\t{segment_datas['seq'] if 'seq' in segment_datas else 'N'*segment_datas['length']}\t" + '\t'.join(
[f"{key}:{GFAParser.get_python_type(value)}:{value}" if not key.startswith('ARG') else str(value) for key, value in segment_datas.items() if key not in ['length', 'seq']])+"\n")
if graph.lines:
for line in graph.lines:
ori1, ori2 = line['orientation'].split('/')
gfa_writer.write(f"L\t"+f"{line['start']}\t{ori1}\t{line['end']}\t{ori2}\t" + '\t'.join(
[f"{key}:{GFAParser.get_python_type(value)}:{value}" if not key.startswith('ARG') else str(value) for key, value in line.items() if key not in ['orientation', 'start', 'end']])+"\n")
if graph.paths:
for path_name, path_datas in graph.paths.items():
if graph.metadata['version'] == GFAFormat.GFA1: # P-line
gfa_writer.write(
f"P\t{path_name}\t{','.join([node_name+'+' if orient == Orientation.FORWARD else node_name+'-' for node_name, orient in path_datas['path']])}\t*")
else:
# W-line
offset_start: int | str = path_datas['start_offset'] if 'start_offset' in path_datas else '?'
offset_stop: int | str = path_datas['stop_offset'] if 'stop_offset' in path_datas else '?'
strpath: str = ''.join(
[f"{'>' if orient == Orientation.FORWARD else '<'}{node_name}" for node_name, orient in path_datas['path']])
return f"W\t{path_name}\t{path_datas['origin'] if 'origin' in path_datas else line_number}\t{path_datas['name']}\t{offset_start}\t{offset_stop}\t{strpath}\t*\n"
line_number += 1
3 changes: 1 addition & 2 deletions pgGraphs/graph.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"Modelizes a graph object"
from pgGraphs.abstractions import GFALine, Orientation
from pgGraphs.gfaparser import GFAParser
from pgGraphs.io import GFAIO


class Graph():
Expand Down Expand Up @@ -72,7 +71,7 @@ def save_graph(self, output_file: str) -> None:
Args:
output_file (str): path where to output the graph
"""
GFAIO.save_graph(self, output_path=output_file)
GFAParser.save_graph(self, output_path=output_file)

################################################# ADD ELEMNTS TO GRAPH #################################################

Expand Down
45 changes: 0 additions & 45 deletions pgGraphs/io.py

This file was deleted.

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
networkx
tharos-pytools
5 changes: 5 additions & 0 deletions test_pggraphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pgGraphs import Graph


def test_init_empty_graph() -> None:
graph: Graph = Graph()

0 comments on commit 214ad52

Please sign in to comment.