Exemple #1
0
 def test_basic_behaviour(self):
     'It tests that we can get names using a FileNamingSchema'
     fhand = NamedTemporaryFile()
     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',
                             feature_kind='EST')
     naming = FileNamingSchema(fhand, naming)
     assert naming.get_uniquename(name='hola') == 'myES000001'
     assert naming.get_uniquename(name='hola') == 'myES000001'
     assert naming.get_uniquename(name='caracol') == 'myES000002'
     naming.commit()
     fhand.seek(0)
     naming = FileNamingSchema(fhand)
     naming.kind = 'EST'
     assert naming.get_uniquename(name='hola') == 'myES000001'
     assert naming.get_uniquename(name='caracol') == 'myES000002'
     try:
         assert naming.get_uniquename(name='pascual')
         self.fail()
         #pylint: disable-msg=W0704
     except ValueError:
         pass
Exemple #2
0
    def test_revert_last_name():
        '''It test if we can revert the database to its last stste for a kind
        and project'''
        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',
                                feature_kind='EST')
        naming.get_uniquename()
        naming.commit()
        naming.get_uniquename()
        naming.commit()
        names = list(naming.get_names_from_db())
        assert names[0]['name'] == 'myES000001'
        assert names[-1]['project'] == 'my_project'
        assert names[-1]['name'] == 'myES000002'
        assert len(names) == 2
        naming.revert_last_name()

        naming = DbNamingSchema(engine, project='my_project',
                                feature_kind='EST')
        names = list(naming.get_names_from_db())
        assert len(names) == 1
        assert names[-1]['name'] == 'myES000001'
Exemple #3
0
 def xtest_basic_behaviour2():
     'It tests that we can get names'
     engine = sqlalchemy.create_engine('sqlite:////home/jope/test/naming_db')
     create_naming_database(engine)
     add_project_to_naming_database(engine, name='Melo', code='ME',
                                    description='Melonomics Ests')
     naming = DbNamingSchema(engine, project='Melo', feature_kind='EST')
     assert naming.get_uniquename() == 'MEES000001'
     assert naming.get_uniquename() == 'MEES000002'
     naming.commit(description='Initial')
Exemple #4
0
 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:')
     create_naming_database(engine)
     assert not project_in_database(engine, project)
     add_project_to_naming_database(engine, name=project, code='my',
                                    description='a test project')
     assert project_in_database(engine, project)
     assert not project_in_database(engine, 'test2')
Exemple #5
0
 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',
                             feature_kind='EST')
     assert naming.get_uniquename() == 'myES000001'
     assert naming.get_uniquename() == 'myES000002'
     naming.kind = 'transcribed_cluster'
     assert naming.get_uniquename() == 'myTC000001'
     naming.commit()
Exemple #6
0
    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',
                                feature_kind='EST')
        assert naming.get_uniquename() == 'myES000001'
        naming.commit()
        naming = DbNamingSchema(engine, project='my_project',
                                feature_kind='EST')

        assert naming.get_uniquename() == 'myES000002'
Exemple #7
0
    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',
                                feature_kind='EST')
        assert naming.get_uniquename() == 'myES000001'

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

        from franklin.db.naming import _CodeGenerator
Exemple #8
0
    def test_fastq():
        'It test that we can change the name in the fasta files.'
        fhand_in  = open(os.path.join(TEST_DATA_DIR, 'solexa.fastq'))
        fhand_out = NamedTemporaryFile(suffix='.fasta')
        engine    = sqlalchemy.create_engine('sqlite:///:memory:')

        create_naming_database(engine)
        add_project_to_naming_database(engine, name='test_project', code='my',
                                       description='a test project')
        naming    = DbNamingSchema(engine, 'test_project', feature_kind='EST')

        change_names_in_files(fhand_in, fhand_out, naming, 'fastq')
        output = open(fhand_out.name).read()
        assert "@myES000001" in output
        assert "@myES000003" in output

        assert open(fhand_out.name).read()[:8] == '@myES000'
def _change_names_in_file(io_options, database, filecache, project,
                          feature_kind, description):
    'It changes the name to a file giving a naming'
    # check if the database exits
    if database is not None:
        engine   = sqlalchemy.create_engine( 'sqlite:///%s'  % database)
        if not os.path.exists(database):
            create_naming_database(engine)
        # check if the project exists
        project_name = project['name']
        if not project_in_database(engine, project_name):
            if project['code']:
                add_project_to_naming_database(engine, name=project_name,
                                           code=project['code'],
                                           description=project['description'])
            else:
                msg  = 'To add a new project to the database the code should be'
                msg += ' given'
                raise ValueError(msg)

        # create a naming schema
        naming = DbNamingSchema(engine, project=project_name,
                                feature_kind=feature_kind)
    else:
        naming = None

    if filecache is not None:
        if os.path.exists(filecache):
            mode = 'a'
        else:
            mode = 'w'
        fhand = open(filecache, mode)
        naming = FileNamingSchema(fhand, naming)

    try:
        # change name to seqs in file
        infhand  = io_options['infhand']
        outfhand = io_options['outfhand']
        format   = io_options['format']
        change_names_in_files(infhand, outfhand, naming, format)
        naming.commit(description=description)
    except:
        #if we don't commit we lose the changes in the database
        raise
Exemple #10
0
    def test_get_all_names():
        'It test if we can get all names stored in teh database'
        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',
                                feature_kind='EST')
        naming.get_uniquename()
        naming.commit()
        naming.get_uniquename()
        naming.commit()

        names = list(naming.get_names_from_db())
        assert names[0]['project'] == 'my_project'
        assert len(names) == 2

        naming.project = 'my_project'
        name = naming.get_names_from_db().next()
        assert name['project'] == 'my_project'
Exemple #11
0
    def test_roolback(self):
        'If the changes are not commited they are ignored'
        fhand = NamedTemporaryFile()
        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',
                                feature_kind='EST')
        naming = FileNamingSchema(fhand, naming)
        assert naming.get_uniquename(name='hola') == 'myES000001'
        naming.commit()
        assert naming.get_uniquename(name='caracol') == 'myES000002'

        naming = FileNamingSchema(fhand, feature_kind='EST')
        try:
            assert naming.get_uniquename(name='caracol')
            self.fail()
            #pylint: disable-msg=W0704
        except ValueError:
            pass
    def test_contig_member():
        'It test that we can change the name in the fasta files.'
        fhand_in  = StringIO(EXAMPLES['contig_member'])

        fhand_out = StringIO('')
        engine    = sqlalchemy.create_engine('sqlite:///:memory:')
        filenaming_fhand = NamedTemporaryFile()
        filenaming_fhand.write('METC053253:seq1\nMETC053254:seq2\n')
        filenaming_fhand.flush()

        create_naming_database(engine)

        add_project_to_naming_database(engine, name='test_project', code='my',
                                       description='a test project')
        naming = FileNamingSchema(filenaming_fhand)


        change_names_in_files(fhand_in, fhand_out, naming, 'contig_member')

        output = fhand_out.getvalue()
        assert "seq1" in output
        assert "seq2" in output
Exemple #13
0
    def test_fasta():
        'It test that we can change the name in the fasta files.'
        fhand_in  = StringIO(EXAMPLES['fasta'][0])
        fhand_out = StringIO('')
        engine    = sqlalchemy.create_engine('sqlite:///:memory:')

        create_naming_database(engine)
        add_project_to_naming_database(engine, name='test_project', code='my',
                                       description='a test project')
        naming    = DbNamingSchema(engine, 'test_project', feature_kind='EST')

        change_names_in_files(fhand_in, fhand_out, naming, 'fasta')
        assert fhand_out.getvalue() == EXAMPLES['fasta'][1]

        # test it with file naming naming schema
        #filenaming_fhand = open('/tmp/ppp', 'w')
        filenaming_fhand = NamedTemporaryFile()
        fhand_in = StringIO(EXAMPLES['fasta'][0])
        fhand_out = StringIO('')
        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',
                                feature_kind='EST')
        naming = FileNamingSchema(filenaming_fhand, naming)
        change_names_in_files(fhand_in, fhand_out, naming, 'fasta')
        naming.commit()
        assert fhand_out.getvalue() == EXAMPLES['fasta'][1]


        fhand_in.seek(0)
        filenaming_fhand.seek(0)
        fhand_out = StringIO('')
        naming = FileNamingSchema(filenaming_fhand, feature_kind='EST')
        change_names_in_files(fhand_in, fhand_out, naming, 'fasta')
        assert fhand_out.getvalue() == EXAMPLES['fasta'][1]