org.ensembl.gti.seqstore.database.HybridEnaCramSeqStore.java Source code

Java tutorial

Introduction

Here is the source code for org.ensembl.gti.seqstore.database.HybridEnaCramSeqStore.java

Source

/*
 * Copyright 2015 EMBL-European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ensembl.gti.seqstore.database;

import javax.sql.DataSource;

import org.ensembl.gti.seqstore.GenomeSequence;
import org.ensembl.gti.seqstore.database.cramstore.EnaCramSubmissionHandler;
import org.ensembl.gti.seqstore.utils.SeqStoreHashUtils;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

/**
 * Class for storing genome sequences in ENA but all other sequences in MySQL
 * @author dstaines
 *
 */
public class HybridEnaCramSeqStore extends JdbcSeqStore {

    private final EnaCramSubmissionHandler submitter;

    public HybridEnaCramSeqStore(DataSource dataSource, EnaCramSubmissionHandler submitter) {
        super(dataSource);
        this.submitter = submitter;
    }

    @Override
    public void storeGenomeSequence(long sessionId, GenomeSequence genomeSequence) {
        log.info("Storing genome sequence  " + genomeSequence.getStableId());
        transactionTemplate.execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                String chk = storeSequenceEna(sessionId, genomeSequence.getSequence());
                template.update(STORE_GENOME_SEQUENCE_SQL, genomeSequence.getGenomeId(),
                        genomeSequence.getStableId(), chk, sessionId);
                return null;
            }
        });
    }

    protected String storeSequenceEna(long sessionId, String sequence) {
        String chk = SeqStoreHashUtils.hashSequence(sequence);
        log.info("Storing sequence with hash " + chk + " in session " + sessionId);
        submitter.addSequence(sequence);
        log.debug("Completed toring sequence with hash " + chk + " in session " + sessionId);
        return chk;
    }

}