<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_GEOparse
----------------------------------

Tests for `GEOparse` module.
"""

from sys import path
from os.path import abspath, join, dirname
import unittest
from pandas import DataFrame, read_table
from pandas.util.testing import assert_frame_equal
from six import iteritems

path.append(join(abspath(__file__), ".."))
from GEOparse.GEOTypes import (GSE, GSM, GPL, GDS,
                               GDSSubset, GEODatabase,
                               DataIncompatibilityException,
                               NoMetadataException,
                               )
import GEOparse as GEO

download_geo = dirname(abspath(__file__))


class TestGSM(unittest.TestCase):
    """Test GSM class"""

    def setUp(self):
        self.header1 = ['a', 'b', 'c']
        self.header2 = ['a', 'b', 'd']
        self.columns_desc = [['first column'], ['second column'],
                             ['third column']]
        self.data = [[1, 2, 3],
                     [4, 5, 6]]
        self.table1 = DataFrame(self.data, columns=self.header1)
        self.table2 = DataFrame(self.data, columns=self.header2)
        self.columns_no_desc = DataFrame(self.columns_desc, self.header1)
        self.columns1 = DataFrame(self.columns_desc, self.header1,
                                  columns=['description'])
        self.columns2 = DataFrame(self.columns_desc, self.header2,
                                  columns=['description'])
        self.metadata = {'name': ['SAMPLE']}

    def test_creation_of_object(self):
        with self.assertRaises(ValueError):
            GSM(name='name', table=['a'], metadata=self.metadata,
                columns=self.columns1)
        with self.assertRaises(TypeError):
            GSM(name='name', table=self.table1, metadata=[],
                columns=self.columns1)
        with self.assertRaises(ValueError):
            GSM(name='name', table=self.table1, metadata=self.metadata,
                columns=[])
        with self.assertRaises(DataIncompatibilityException):
            GSM(name='name', table=self.table1, metadata=self.metadata,
                columns=self.columns2)
        with self.assertRaises(ValueError):
            GSM(name='name', table=self.table1, metadata=self.metadata,
                columns=self.columns_no_desc)
        GSM(name='name', table=self.table1, metadata=self.metadata,
            columns=self.columns1)

    def test_simple_data(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        self.assertEqual(gsm.table.ix[0, 'a'], 1)
        self.assertEqual(gsm.table.ix[1, 'b'], 5)

    def test_get_geo_and_data(self):
        gsm = GEO.get_GEO(geo="GSM11805", destdir=download_geo)
        self.assertTrue(isinstance(gsm, GSM))
        self.assertEqual(gsm.get_accession(), "GSM11805")
        self.assertEqual(len(gsm.table.index), 22283)
        self.assertEqual(len(gsm.columns), 3)
        self.assertEqual(len(gsm.metadata.keys()), 28)

    def test_get_metadata_attribute(self):
        metadata = {'name': 'SAMPLE', 'samples': ["sam1", "sam2"]}
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        gsm2 = GSM(name='name', table=self.table1, metadata=metadata,
                   columns=self.columns1)
        with self.assertRaises(TypeError):
            gsm2.get_metadata_attribute('name')
        self.assertEqual(gsm.get_metadata_attribute('name'), "SAMPLE")
        self.assertEqual(gsm2.get_metadata_attribute('samples'),
                         ["sam1", "sam2"])
        with self.assertRaises(NoMetadataException):
            gsm.get_metadata_attribute('dupa')

    def test_get_type(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        self.assertEqual(gsm.get_type(), None)

    def test_metadata_as_string(self):
        metadata = {'name': ['SAMPLE1'], 'data': ["Normal"]}
        metadata_soft = ("!Sample_data = Normal\n"
                         "!Sample_name = SAMPLE1")
        gsm = GSM(name='name', table=self.table1, metadata=metadata,
                  columns=self.columns1)
        self.assertIn("!Sample_data = Normal", gsm._get_metadata_as_string())
        self.assertIn("!Sample_name = SAMPLE1", gsm._get_metadata_as_string())

    def test_get_table_as_string(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        table = ("!sample_table_begin\n"
                 "a\tb\tc\n"
                 "1\t2\t3\n"
                 "4\t5\t6\n"
                 "!sample_table_end")
        self.assertEqual(gsm._get_table_as_string(), table)

    def test_get_columns_as_string(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        columns = ("#a = first column\n"
                   "#b = second column\n"
                   "#c = third column")
        self.assertEqual(gsm._get_columns_as_string(), columns)

    def test_to_soft(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        soft = ("^SAMPLE = name\n"
                "!Sample_name = SAMPLE\n"
                "#a = first column\n"
                "#b = second column\n"
                "#c = third column\n"
                "!sample_table_begin\n"
                "a\tb\tc\n"
                "1\t2\t3\n"
                "4\t5\t6\n"
                "!sample_table_end")
        self.assertEqual(gsm._get_object_as_soft(), soft)

    def test_annotate(self):
        gse = GEO.get_GEO(filepath=join(download_geo, "soft_ex_family.txt"),
                          geotype="GSE")
        gsm = gse.gsms[
            "Triple-Fusion Transfected Embryonic Stem Cells Replicate 1"]
        result = read_table(join(download_geo, "test_gsm_annotated.tab"))
        gpl = gse.gpls[next(iter(gse.gpls))]
        assert_frame_equal(result,
                           gsm.annotate(gpl, annotation_column="GB_ACC"))
        assert_frame_equal(result,
                           gsm.annotate(gpl.table, annotation_column="GB_ACC"))
        with self.assertRaises(TypeError):
            gsm.annotate("platform", annotation_column="GB_ACC")
        gsm.annotate(gpl.table, annotation_column="GB_ACC", in_place=True)
        assert_frame_equal(result, gsm.table)

    def test_head(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        try:
            gsm.head()
        except:
            self.fail("GSM.head() raised error!")

    def test_empty_line(self):
        try:
            gsm = GEO.get_GEO('GSM32878')
        except IndexError:
            self.fail("Empty line in the file causes an error.")

    def test_no_table(self):
        try:
            gsm = GEO.get_GEO('GSM2795971')
        except:
            self.fail("No data in the file error.")


class TestGPL(unittest.TestCase):
    """Test GPL class"""

    def setUp(self):
        self.header1 = ['a', 'b', 'c']<commit_msg>test: Be explicit about files<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_GEOparse
----------------------------------

Tests for `GEOparse` module.
"""

from sys import path
from os.path import abspath, join, dirname
import unittest
from pandas import DataFrame, read_table
from pandas.util.testing import assert_frame_equal
from six import iteritems

path.append(join(abspath(__file__), ".."))
from GEOparse.GEOTypes import (GSE, GSM, GPL, GDS,
                               GDSSubset, GEODatabase,
                               DataIncompatibilityException,
                               NoMetadataException,
                               )
import GEOparse as GEO

download_geo = dirname(abspath(__file__))


class TestGSM(unittest.TestCase):
    """Test GSM class"""

    def setUp(self):
        self.header1 = ['a', 'b', 'c']
        self.header2 = ['a', 'b', 'd']
        self.columns_desc = [['first column'], ['second column'],
                             ['third column']]
        self.data = [[1, 2, 3],
                     [4, 5, 6]]
        self.table1 = DataFrame(self.data, columns=self.header1)
        self.table2 = DataFrame(self.data, columns=self.header2)
        self.columns_no_desc = DataFrame(self.columns_desc, self.header1)
        self.columns1 = DataFrame(self.columns_desc, self.header1,
                                  columns=['description'])
        self.columns2 = DataFrame(self.columns_desc, self.header2,
                                  columns=['description'])
        self.metadata = {'name': ['SAMPLE']}

    def test_creation_of_object(self):
        with self.assertRaises(ValueError):
            GSM(name='name', table=['a'], metadata=self.metadata,
                columns=self.columns1)
        with self.assertRaises(TypeError):
            GSM(name='name', table=self.table1, metadata=[],
                columns=self.columns1)
        with self.assertRaises(ValueError):
            GSM(name='name', table=self.table1, metadata=self.metadata,
                columns=[])
        with self.assertRaises(DataIncompatibilityException):
            GSM(name='name', table=self.table1, metadata=self.metadata,
                columns=self.columns2)
        with self.assertRaises(ValueError):
            GSM(name='name', table=self.table1, metadata=self.metadata,
                columns=self.columns_no_desc)
        GSM(name='name', table=self.table1, metadata=self.metadata,
            columns=self.columns1)

    def test_simple_data(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        self.assertEqual(gsm.table.ix[0, 'a'], 1)
        self.assertEqual(gsm.table.ix[1, 'b'], 5)

    def test_get_geo_and_data(self):
        gsm = GEO.get_GEO(geo="GSM11805", destdir=download_geo)
        self.assertTrue(isinstance(gsm, GSM))
        self.assertEqual(gsm.get_accession(), "GSM11805")
        self.assertEqual(len(gsm.table.index), 22283)
        self.assertEqual(len(gsm.columns), 3)
        self.assertEqual(len(gsm.metadata.keys()), 28)

    def test_get_metadata_attribute(self):
        metadata = {'name': 'SAMPLE', 'samples': ["sam1", "sam2"]}
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        gsm2 = GSM(name='name', table=self.table1, metadata=metadata,
                   columns=self.columns1)
        with self.assertRaises(TypeError):
            gsm2.get_metadata_attribute('name')
        self.assertEqual(gsm.get_metadata_attribute('name'), "SAMPLE")
        self.assertEqual(gsm2.get_metadata_attribute('samples'),
                         ["sam1", "sam2"])
        with self.assertRaises(NoMetadataException):
            gsm.get_metadata_attribute('dupa')

    def test_get_type(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        self.assertEqual(gsm.get_type(), None)

    def test_metadata_as_string(self):
        metadata = {'name': ['SAMPLE1'], 'data': ["Normal"]}
        metadata_soft = ("!Sample_data = Normal\n"
                         "!Sample_name = SAMPLE1")
        gsm = GSM(name='name', table=self.table1, metadata=metadata,
                  columns=self.columns1)
        self.assertIn("!Sample_data = Normal", gsm._get_metadata_as_string())
        self.assertIn("!Sample_name = SAMPLE1", gsm._get_metadata_as_string())

    def test_get_table_as_string(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        table = ("!sample_table_begin\n"
                 "a\tb\tc\n"
                 "1\t2\t3\n"
                 "4\t5\t6\n"
                 "!sample_table_end")
        self.assertEqual(gsm._get_table_as_string(), table)

    def test_get_columns_as_string(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        columns = ("#a = first column\n"
                   "#b = second column\n"
                   "#c = third column")
        self.assertEqual(gsm._get_columns_as_string(), columns)

    def test_to_soft(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        soft = ("^SAMPLE = name\n"
                "!Sample_name = SAMPLE\n"
                "#a = first column\n"
                "#b = second column\n"
                "#c = third column\n"
                "!sample_table_begin\n"
                "a\tb\tc\n"
                "1\t2\t3\n"
                "4\t5\t6\n"
                "!sample_table_end")
        self.assertEqual(gsm._get_object_as_soft(), soft)

    def test_annotate(self):
        gse = GEO.get_GEO(filepath=join(download_geo, "soft_ex_family.txt"),
                          geotype="GSE")
        gsm = gse.gsms[
            "Triple-Fusion Transfected Embryonic Stem Cells Replicate 1"]
        result = read_table(join(download_geo, "test_gsm_annotated.tab"))
        gpl = gse.gpls[next(iter(gse.gpls))]
        assert_frame_equal(result,
                           gsm.annotate(gpl, annotation_column="GB_ACC"))
        assert_frame_equal(result,
                           gsm.annotate(gpl.table, annotation_column="GB_ACC"))
        with self.assertRaises(TypeError):
            gsm.annotate("platform", annotation_column="GB_ACC")
        gsm.annotate(gpl.table, annotation_column="GB_ACC", in_place=True)
        assert_frame_equal(result, gsm.table)

    def test_head(self):
        gsm = GSM(name='name', table=self.table1, metadata=self.metadata,
                  columns=self.columns1)
        try:
            gsm.head()
        except:
            self.fail("GSM.head() raised error!")

    def test_empty_line(self):
        try:
            gsm = GEO.get_GEO(filepath=join(download_geo, 'GSM32878.txt'),
                              geotype='GSM')
        except IndexError:
            self.fail("Empty line in the file causes an error.")

    def test_no_table(self):
        try:
            gsm = GEO.get_GEO(filepath=join(download_geo, 'GSM2795971.txt'),
                              geotype='GSM')
        except:
            self.fail("No data in the file error.")


class TestGPL(unittest.TestCase):
    """Test GPL class"""

    def setUp(self):
        self.header1 = ['a', 'b', 'c']
{'fraction_non_alphanumeric': 0.13280185336603978, 'fraction_numerical': 0.014036522213137094, 'mean_word_length': 2.136781363539218, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4114151', 'n_tokens_mistral': 4652, 'n_tokens_neox': 4485, 'n_words': 755}