<commit_before>'''It test that we can modify the names/accessions in different kind of files
like ace, caf or fasta'''

# Copyright 2009 <NAME>, <NAME>, COMAV-Univ. Politecnica Valencia
# This file is part of biolib.
# biolib is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# biolib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with biolib. If not, see <http://www.gnu.org/licenses/>.

import unittest
import os.path
from StringIO import StringIO
import biolib
from biolib.db.naming import (change_names_in_files,
                              create_naming_database, DbNamingSchema,
                              FileNamingSchema,
                              add_project_to_naming_database,
                              project_in_database)
from tempfile import NamedTemporaryFile
import sqlalchemy
from biolib.utils.misc_utils import DATA_DIR

EXAMPLES = {'fasta':('''
>hola
ATCGTAGTCAGTTCAGTCTATGCTAGT
>carcola
ATGCGTAGCTAGTCGTAGTCTAGTCAT''','''
>myES000001
ATCGTAGTCAGTTCAGTCTATGCTAGT
>myES000002
ATGCGTAGCTAGTCGTAGTCTAGTCAT'''),
'caf': ('''
Sequence : contig1
Is_contig
Assembled_from EST1 1 293 39 331

DNA : contig1
ACGTACTGTAG

BaseQuality : contig1

DNA : EST1
ATCGATCTGTAC

BaseQuality : EST1
Sequence : EST1
Is_read''',
'''
Sequence : 001
Is_contig
Assembled_from 001 1 293 39 331

DNA : 001
ACGTACTGTAG

BaseQuality : 001

DNA : 001
ATCGATCTGTAC

BaseQuality : 001
Sequence : 001
Is_read'''),
'ace':('''
CO Contig2 213 6 1 U
TCGTGGTGGTTCATCGACATT

BQ
96 97 95 95 97 93 96

AF eucalyptus0111 U 1

RD eucalyptus0111 173 0 0
TCGTGGTGGTTCATCGAC

QA 1 173 1 173
''', '''
CO 001 213 6 1 U
TCGTGGTGGTTCATCGACATT

BQ
96 97 95 95 97 93 96

AF 001 U 1

RD 001 173 0 0
TCGTGGTGGTTCATCGAC

QA 1 173 1 173
'''),
'library':('''
format-version:1
library_definition
    name: a
    type: library type:genomic
    organism:Cucumis melo
    cvterms: SO:0001, SO:0002
    properties: property type:strain:Oregon-R, property type:stage:adult male

library_definition
    name:b
    type: library type:genomic
    organism: Cucumis melo
    cvterms:SO:0003, SO:0004
    properties: SO:222, SO:3456
'''),
'fastq':('''
@MEES000003
AGATGGATGTTCCACAGGCTTTGATATGGTCCTCGACTTACTACGCAAATCGATGCCAGATCCTCCAATCCAGATATATT
CAGCAATAATCTTGTCGGTATATGGAGTGACGT
+
EEEEEEEEEA@@@@DDD@@444@@IFEC:;:;;AEEEDDEEEEEEA===EEEEEEEEEEEEEEDDDDDEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDA
@MEES000004
CTGTCTCTGCGAAGAAGAGCAAGGCATCCAGTAGTTCGGAGGAAGACTCATCTGAAGATGATTCTGATTCTGATGAGGAA
CCAAAGGGAAAGTTATTGCAGCAA
+
FFFFFFFFFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFBBBF;;>>78@FEEEEFFFFFF
''')
}

class DbNamingSchemaTest(unittest.TestCase):
    'We test the behaviour of a naming schema stored in a naming database'

    @staticmethod
    def test_basic_behaviour():
        'It tests that we can get names'
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
        create_naming_database(engine)
        add_project_to_naming_database(engine, name='my_project', code='my',
                                       description='a test project')
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'
        assert naming.get_uniquename(kind='EST') == 'myES000002'
        assert naming.get_uniquename(kind='transcribed_cluster') == 'myTC000001'
        naming.commit()

    @staticmethod
    def test_name_persistance():
        'The names are stored between db commits'
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
        create_naming_database(engine)
        add_project_to_naming_database(engine, name='my_project', code='my',
                                       description='a test project')
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'
        naming.commit()
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000002'

    @staticmethod
    def test_rollback():
        "If we don't commit we lose the changes"
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
        create_naming_database(engine)
        add_project_to_naming_database(engine, name='my_project', code='my',
                                       description='a test project')
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'

        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'

    @staticmethod
    def test_project_in_database():
        'It test the ability tpo know if the databse is already added'
        project = 'test'
        engine = sqlalchemy.create_engine('sqlite:///:memory:')<commit_msg>fix typo<commit_after>'''It test that we can modify the names/accessions in different kind of files
like ace, caf or fasta'''

# Copyright 2009 <NAME>, <NAME>, COMAV-Univ. Politecnica Valencia
# This file is part of biolib.
# biolib is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# biolib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with biolib. If not, see <http://www.gnu.org/licenses/>.

import unittest
import os.path
from StringIO import StringIO
import biolib
from biolib.db.naming import (change_names_in_files,
                              create_naming_database, DbNamingSchema,
                              FileNamingSchema,
                              add_project_to_naming_database,
                              project_in_database)
from tempfile import NamedTemporaryFile
import sqlalchemy
from biolib.utils.misc_utils import DATA_DIR

EXAMPLES = {'fasta':('''
>hola
ATCGTAGTCAGTTCAGTCTATGCTAGT
>carcola
ATGCGTAGCTAGTCGTAGTCTAGTCAT''','''
>myES000001
ATCGTAGTCAGTTCAGTCTATGCTAGT
>myES000002
ATGCGTAGCTAGTCGTAGTCTAGTCAT'''),
'caf': ('''
Sequence : contig1
Is_contig
Assembled_from EST1 1 293 39 331

DNA : contig1
ACGTACTGTAG

BaseQuality : contig1

DNA : EST1
ATCGATCTGTAC

BaseQuality : EST1
Sequence : EST1
Is_read''',
'''
Sequence : 001
Is_contig
Assembled_from 001 1 293 39 331

DNA : 001
ACGTACTGTAG

BaseQuality : 001

DNA : 001
ATCGATCTGTAC

BaseQuality : 001
Sequence : 001
Is_read'''),
'ace':('''
CO Contig2 213 6 1 U
TCGTGGTGGTTCATCGACATT

BQ
96 97 95 95 97 93 96

AF eucalyptus0111 U 1

RD eucalyptus0111 173 0 0
TCGTGGTGGTTCATCGAC

QA 1 173 1 173
''', '''
CO 001 213 6 1 U
TCGTGGTGGTTCATCGACATT

BQ
96 97 95 95 97 93 96

AF 001 U 1

RD 001 173 0 0
TCGTGGTGGTTCATCGAC

QA 1 173 1 173
'''),
'library':('''
format-version:1
library_definition
    name: a
    type: library type:genomic
    organism:Cucumis melo
    cvterms: SO:0001, SO:0002
    properties: property type:strain:Oregon-R, property type:stage:adult male

library_definition
    name:b
    type: library type:genomic
    organism: Cucumis melo
    cvterms:SO:0003, SO:0004
    properties: SO:222, SO:3456
'''),
'fastq':('''
@MEES000003
AGATGGATGTTCCACAGGCTTTGATATGGTCCTCGACTTACTACGCAAATCGATGCCAGATCCTCCAATCCAGATATATT
CAGCAATAATCTTGTCGGTATATGGAGTGACGT
+
EEEEEEEEEA@@@@DDD@@444@@IFEC:;:;;AEEEDDEEEEEEA===EEEEEEEEEEEEEEDDDDDEEEEEEEEEEEE
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEDA
@MEES000004
CTGTCTCTGCGAAGAAGAGCAAGGCATCCAGTAGTTCGGAGGAAGACTCATCTGAAGATGATTCTGATTCTGATGAGGAA
CCAAAGGGAAAGTTATTGCAGCAA
+
FFFFFFFFFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFBBBF;;>>78@FEEEEFFFFFF
''')
}

class DbNamingSchemaTest(unittest.TestCase):
    'We test the behaviour of a naming schema stored in a naming database'

    @staticmethod
    def test_basic_behaviour():
        'It tests that we can get names'
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
        create_naming_database(engine)
        add_project_to_naming_database(engine, name='my_project', code='my',
                                       description='a test project')
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'
        assert naming.get_uniquename(kind='EST') == 'myES000002'
        assert naming.get_uniquename(kind='transcribed_cluster') == 'myTC000001'
        naming.commit()

    @staticmethod
    def test_name_persistance():
        'The names are stored between db commits'
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
        create_naming_database(engine)
        add_project_to_naming_database(engine, name='my_project', code='my',
                                       description='a test project')
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'
        naming.commit()
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000002'

    @staticmethod
    def test_rollback():
        "If we don't commit we lose the changes"
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
        create_naming_database(engine)
        add_project_to_naming_database(engine, name='my_project', code='my',
                                       description='a test project')
        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'

        naming = DbNamingSchema(engine, project='my_project')
        assert naming.get_uniquename(kind='EST') == 'myES000001'

    @staticmethod
    def test_project_in_database():
        'It test the ability to know if the databse is already added'
        project = 'test'
        engine = sqlalchemy.create_engine('sqlite:///:memory:')
{'fraction_non_alphanumeric': 0.08759262777883337, 'fraction_numerical': 0.04370131103933118, 'mean_word_length': 3.682829181494662, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 14, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810329', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3401, 'n_words': 896}