Repository: dizak/prwlr

Issue:
prwlr.read_profiles
The highest-level convenience method ```prwlr.read_profiles``` should be implemented in ```prwlr.core``` analogous to ```prwlr.core.read_sga```. There should be also be ```prwlr.save_profiles``` avilable.

Discussion:

Patch:
diff --git a/prwlr/core.py b/prwlr/core.py
index e0b5859..146c0bd 100644
--- a/prwlr/core.py
+++ b/prwlr/core.py
@@ -1,7 +1,17 @@
 import pandas as _pd
 import numpy as _np
 from . import databases
+from . import profiles as _profiles
 
+class Columns:
+    """
+    Container for the columns names defined in this module.
+    """
+    SPLIT_SUF = '_SPLIT'
+    REF = 'REF'
+    QRY = 'QRY'
+    REF_SPLIT = '{}{}'.format(REF, SPLIT_SUF)
+    QRY_SPLIT = '{}{}'.format(QRY, SPLIT_SUF)
 
 def get_IDs_names(
     species,
@@ -98,6 +108,55 @@ def read_sga(
     sga.parse(filename=filename)
     return sga.sga
 
+def read_profiles(
+    filename,
+    reference_column=Columns.REF,
+    query_column=Columns.QRY,
+    str_sep='|',
+    **kwargs
+):
+    """
+    Returns pandas.Series with prwlr.profiles.Profile objects from CSV.
+
+    Parameters
+    -------
+    filename: str, path
+        Filename of the CSV.
+    """
+    ref_qry_df = _pd.read_csv(filename, **kwargs)
+    ref_qry_df[Columns.REF_SPLIT] = ref_qry_df[Columns.REF].str.split(str_sep)
+    ref_qry_df[Columns.QRY_SPLIT] = ref_qry_df[Columns.QRY].str.split(str_sep)
+    return ref_qry_df[[Columns.REF_SPLIT, Columns.QRY_SPLIT]].apply(
+        lambda x: _profiles.Profile(
+            reference=x[Columns.REF_SPLIT],
+            query=x[Columns.QRY_SPLIT],
+        ),
+        axis=1,
+    )
+
+def save_profiles(
+    series,
+    filename,
+    reference_column=Columns.REF,
+    query_column=Columns.QRY,
+    str_sep='|',
+    **kwargs
+):
+    """
+    Writes pandas.Series with prwlr.profiles.Profile objects to CSV file.
+
+    Parameters
+    -------
+    Filename: str, path
+        Name of the file.
+    """
+    _pd.DataFrame(
+        {
+            Columns.REF: series.apply(lambda x: x.reference).str.join(str_sep),
+            Columns.QRY: series.apply(lambda x: x.query).str.join(str_sep),
+        },
+    ).to_csv(filename, **kwargs)
+
 def merge_sga_profiles(
     sga,
     profiles


Test patch:
diff --git a/test_data/CoreTests/test_profiles.csv b/test_data/CoreTests/test_profiles.csv
new file mode 100644
index 0000000..ce9fafc
--- /dev/null
+++ b/test_data/CoreTests/test_profiles.csv
@@ -0,0 +1,3 @@
+,QRY,REF
+0,a|c|d|f|h|i|k|l|o|s|t|u|z,b|c|e|f|g|h|i|j|k|l|m|n|p|r|s|t|u|w|x|y
+1,a|b|d|f|h|i|k|l|o|s|t|u|z,a|c|e|f|g|h|i|j|k|l|m|n|p|r|s|t|u|w|x|y
diff --git a/tests/tests.py b/tests/tests.py
index 315bd8d..b6b2a42 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -605,5 +605,76 @@ class StatsTests(unittest.TestCase):
                         650)
 
 
+class CoreTests(unittest.TestCase):
+    """
+    Tests of prwlr.core functions.
+    """
+    def setUp(self):
+        """
+        Sets up class level attributes for the tests.
+        """
+        import prwlr.core
+
+        self.ref_reference_1 = list('bcefghijklmnprstuwxy')
+        self.ref_query_1 = list('acdfhiklostuz')
+        self.ref_reference_2 = list('acefghijklmnprstuwxy')
+        self.ref_query_2 = list('abdfhiklostuz')
+        self.test_profiles_filename = 'test_data/CoreTests/test_profiles.csv'
+        self.test_saved_profiles_filename = 'test_data/CoreTests/test_save_profiles.csv'
+
+        self.ref_profile_1, self.ref_profile_2 = (
+            prwlr.profiles.Profile(
+                reference=self.ref_reference_1,
+                query=self.ref_query_1,
+            ),
+            prwlr.profiles.Profile(
+                reference=self.ref_reference_2,
+                query=self.ref_query_2,
+            ),
+        )
+        self.ref_profiles_srs = pd.Series(
+            [self.ref_profile_1, self.ref_profile_2],
+        )
+
+        self.test_profiles_srs = prwlr.core.read_profiles(
+            self.test_profiles_filename,
+            index_col=[0],
+        )
+
+        prwlr.core.save_profiles(
+            self.ref_profiles_srs,
+            self.test_saved_profiles_filename,
+        )
+        self.test_profiles_after_save_srs = prwlr.core.read_profiles(
+            self.test_saved_profiles_filename,
+            index_col=[0],
+        )
+
+    def tearDown(self):
+        """
+        Removes files created during the tests.
+        """
+        os.remove(self.test_saved_profiles_filename)
+
+    def test_read_profiles(self):
+        """
+        Tests if prwlr.core.read_profiles returns pandas.Series with correct
+        prwlr.profiles.Profile objects.
+        """
+        pd.testing.assert_series_equal(
+            self.ref_profiles_srs,
+            self.test_profiles_srs,
+        )
+
+    def test_save_profiles(self):
+        """
+        Tests if prwlr.core.save_profiles writes CSV file with correct values.
+        """
+        pd.testing.assert_series_equal(
+            self.ref_profiles_srs,
+            self.test_profiles_after_save_srs,
+        )
+
+
 if __name__ == '__main__':
     unittest.main()