예제 #1
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
    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
예제 #4
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]