<commit_before>package org.flymine.postprocess;

/*
 * Copyright (C) 2002-2005 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.sql.*;
import java.util.Iterator;
import java.util.Collections;

import org.intermine.sql.Database;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.SingletonResults;
import org.intermine.util.DynamicUtil;

import org.flymine.model.genomic.Contig;
import org.flymine.model.genomic.Sequence;

/**
 * Fetch sequences from the ensembl human src db and store them via ObjectStore
 * Temp fix for ensembl human
 *
 * @author <NAME>
 * @author <NAME>
 */

public class StoreSequences
{

    protected ObjectStoreWriter osw;
    private Database db;


    /**
     * Create a new StoreSequences object from the given ObjectStoreWriter
     * @param osw writer on genomic ObjectStore
     * @param db database
     * @throws SQLException if there are any problems with getting db
     * @throws ClassNotFoundException if no db class be found
     */
    public StoreSequences (ObjectStoreWriter osw, Database db)
          throws SQLException, ClassNotFoundException {
          this.osw = osw;
          this.db = db;
    }


    /**
     * Iterator through all the contigs in the ObjectStore, fetch every sequence and store it
     * in the ObjectStore
     * @throws Exception if there are any problems
     */
    public void storeContigSequences() throws Exception {
        Query q = new Query();
        QueryClass qc = new QueryClass(Contig.class);
        q.addToSelect(qc);
        q.addFrom(qc);

        ObjectStore os = osw.getObjectStore();
        SingletonResults res = new SingletonResults(q, os, os.getSequence());
        Iterator resIter = res.iterator();
        while (resIter.hasNext()) {
            osw.beginTransaction();
            Contig contig = (Contig) PostProcessUtil.cloneInterMineObject((Contig) resIter.next());
            String sequence = getSequence(contig.getIdentifier());
            Sequence seq = (Sequence) DynamicUtil.createObject(
                             Collections.singleton(Sequence.class));
            seq.setResidues(sequence);
            contig.setSequence(seq);
            osw.store(contig);
            osw.store(seq);
            osw.commitTransaction();
        }
    }


    /**
     * Get contig sequences from ensembl human src db by contigId
     * @param contigId the id for the contig
     * @throws SQLException if there are any problems
     * @return a sequence for this contig
     */
    protected String getSequence(String contigId) throws SQLException {
        String sequence = null;

        Connection connection = db.getConnection();
        Statement statement = connection.createStatement();
        ResultSet rs = null;
        String query = null;
        query = "SELECT d.sequence FROM dna d, seq_region s "
              + "WHERE d.seq_region_id = s.seq_region_id and s.name = '"
              + contigId + "'";

        rs = statement.executeQuery(query);
        while (rs.next()) {
            sequence =  rs.getString("sequence");
        }
        rs.close();

        return sequence;
    }

}
<commit_msg>only create one connection to mysql database<commit_after>package org.flymine.postprocess;

/*
 * Copyright (C) 2002-2005 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.sql.*;
import java.util.Iterator;
import java.util.Collections;

import org.intermine.sql.Database;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.SingletonResults;
import org.intermine.util.DynamicUtil;

import org.flymine.model.genomic.Contig;
import org.flymine.model.genomic.Sequence;

/**
 * Fetch sequences from the ensembl human src db and store them via ObjectStore
 * Temp fix for ensembl human
 *
 * @author <NAME>
 * @author <NAME>
 */

public class StoreSequences
{

    protected ObjectStoreWriter osw;
    private Database db;


    /**
     * Create a new StoreSequences object from the given ObjectStoreWriter
     * @param osw writer on genomic ObjectStore
     * @param db database
     * @throws SQLException if there are any problems with getting db
     * @throws ClassNotFoundException if no db class be found
     */
    public StoreSequences (ObjectStoreWriter osw, Database db)
          throws SQLException, ClassNotFoundException {
          this.osw = osw;
          this.db = db;
    }


    /**
     * Iterator through all the contigs in the ObjectStore, fetch every sequence and store it
     * in the ObjectStore
     * @throws Exception if there are any problems
     */
    public void storeContigSequences() throws Exception {
        Query q = new Query();
        QueryClass qc = new QueryClass(Contig.class);
        q.addToSelect(qc);
        q.addFrom(qc);

        ObjectStore os = osw.getObjectStore();
        SingletonResults res = new SingletonResults(q, os, os.getSequence());

        Connection connection = db.getConnection();
        Iterator resIter = res.iterator();
        while (resIter.hasNext()) {
            osw.beginTransaction();
            Contig contig = (Contig) PostProcessUtil.cloneInterMineObject((Contig) resIter.next());

            String sequence = getSequence(connection, contig.getIdentifier());
            Sequence seq = (Sequence) DynamicUtil.createObject(
                             Collections.singleton(Sequence.class));
            seq.setResidues(sequence);
            contig.setSequence(seq);
            osw.store(contig);
            osw.store(seq);
            osw.commitTransaction();
        }
    }


    /**
     * Get contig sequences from ensembl human src db by contigId
     * @param contigId the id for the contig
     * @throws SQLException if there are any problems
     * @return a sequence for this contig
     */
    protected String getSequence(Connection connection, String contigId) throws SQLException {
        String sequence = null;
        
        Statement statement = connection.createStatement();
        ResultSet rs = null;
        String query = null;
        query = "SELECT d.sequence FROM dna d, seq_region s "
              + "WHERE d.seq_region_id = s.seq_region_id and s.name = '"
              + contigId + "'";

        rs = statement.executeQuery(query);
        while (rs.next()) {
            sequence =  rs.getString("sequence");
        }
        rs.close();

        return sequence;
    }

}

{'fraction_non_alphanumeric': 0.08834218618954431, 'fraction_numerical': 0.0022365110427732737, 'mean_word_length': 2.6319796954314723, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5529641', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1892, 'n_words': 589}