Skip to content

Commit

Permalink
generate hashtable dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
dadegrande99 committed Mar 7, 2024
1 parent 0c1e4d8 commit 3c702b3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions alignmentfreegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, location: str = None, db_name: str = None, username: str = No
raise ValueError("k must be greater than 1")
self.k = k
self.compute_hashtable()
self.hashtable_df = None

def connect(self, location: str = None, db_name: str = None,
username: str = None, password: str = None, configuration: [dict, str] = None): # type: ignore
Expand Down Expand Up @@ -341,13 +342,27 @@ def remove_duplicates(self):
self.hashtable[el[0]].pop(el[1])

def hashtable_to_df(self):
"""
This method generate the hash-table of the graph as a pandas DataFrame
"""
import pandas as pd

rows = []
for key, value in self.hashtable.items():
for kmer, colors in value.items():
rows.append({'start': key, 'Kmer': kmer, 'colors': colors})

self.hashtable_df = pd.DataFrame(rows)

def get_hashtable_df(self):
"""
This method return the hash-table of the graph as a pandas DataFrame
:return: The hash-table of the graph as a pandas DataFrame
"""
import pandas as pd
return pd.DataFrame(self.hashtable)
if self.hashtable_df is None:
self.hashtable_to_df()
return self.hashtable_df

def export_hashtable(self, file_path: str):
"""
Expand All @@ -357,10 +372,10 @@ def export_hashtable(self, file_path: str):
"""

if file_path.endswith('.csv'):
self.hashtable_to_df().to_csv(file_path)
self.get_hashtable_df().to_csv(file_path)

elif file_path.endswith('.xlsx'):
self.hashtable_to_df().to_excel(file_path)
self.get_hashtable_df().to_excel(file_path)

else:
import json
Expand Down

0 comments on commit 3c702b3

Please sign in to comment.