<commit_before>package org.intermine.bio.postprocess;

/*
 * Copyright (C) 2002-2014 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 org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.intermine.ObjectStoreWriterInterMineImpl;
import org.intermine.sql.Database;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * A task the replace the sequencefeatureoverlappingfeatures table with a view that uses the
 * bioseg type to calculate the overlaps.
 * @author <NAME>
 */
public class OverlapViewTask
{
    protected ObjectStoreWriterInterMineImpl osw;

    /**
     * Construct a new OverlapViewTask that will change the given object store.
     * @param osw an ObjectStore to write to
     */
    public OverlapViewTask(ObjectStoreWriter osw) {
        if (osw instanceof ObjectStoreWriterInterMineImpl) {
            this.osw = (ObjectStoreWriterInterMineImpl) osw;
        } else {
            throw new RuntimeException("the ObjectStoreWriter is not an "
                                       + "ObjectStoreWriterInterMineImpl");
        }
    }

    /**
     * Drop the sequencefeatureoverlappingfeatures table and replace it with a view that
     * uses the bioseg type to calculate the overlaps.
     * @throws SQLException if there is a problem dropping the table or creating the view
     */
    public void createView() throws SQLException {
        Database db = this.osw.getDatabase();
        Connection con = db.getConnection();

        con.setAutoCommit(false);

        String dropSql = "DROP VIEW overlappingfeaturessequencefeature";
        String viewSql =
            "CREATE VIEW overlappingfeaturessequencefeature "
            + " AS SELECT l1.featureid AS overlappingfeatures, "
            + "           l2.featureid AS sequencefeature "
            + "      FROM location l1, location l2 "
            + "     WHERE l1.locatedonid = l2.locatedonid "
            + "       AND l1.featureid != l2.featureid"
            + "       AND bioseg_create(l1.intermine_start, l1.intermine_end) "
            + "              && bioseg_create(l2.intermine_start, l2.intermine_end)";
       
        String viewSqlRange =
                "CREATE VIEW overlappingfeaturessequencefeature "
                + " AS SELECT l1.featureid AS overlappingfeatures, "
                + "           l2.featureid AS sequencefeature "
                + "      FROM location l1, location l2 "
                + "     WHERE l1.locatedonid = l2.locatedonid "
                + "       AND l1.featureid != l2.featureid"
                + "       AND l1.intermine_locrange && l2.intermine_locrange";
        Statement statement = con.createStatement();
        statement.executeUpdate(dropSql);
        statement.close();

        statement = con.createStatement();
        statement.executeUpdate(viewSql);
        statement.close();

        con.commit();
        con.close();
    }
}<commit_msg>Use postgres range type for overlapping features index if available, otherwise bioseg or exception.<commit_after>package org.intermine.bio.postprocess;

/*
 * Copyright (C) 2002-2014 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 org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.intermine.ObjectStoreWriterInterMineImpl;
import org.intermine.sql.Database;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * A task the replace the sequencefeatureoverlappingfeatures table with a view that uses the
 * bioseg type to calculate the overlaps.
 * @author <NAME>
 */
public class OverlapViewTask
{
    protected ObjectStoreWriterInterMineImpl osw;

    /**
     * Construct a new OverlapViewTask that will change the given object store.
     * @param osw an ObjectStore to write to
     */
    public OverlapViewTask(ObjectStoreWriter osw) {
        if (osw instanceof ObjectStoreWriterInterMineImpl) {
            this.osw = (ObjectStoreWriterInterMineImpl) osw;
        } else {
            throw new RuntimeException("the ObjectStoreWriter is not an "
                                       + "ObjectStoreWriterInterMineImpl");
        }
    }

    /**
     * Drop the sequencefeatureoverlappingfeatures table and replace it with a view that
     * uses the bioseg type to calculate the overlaps.
     * @throws SQLException if there is a problem dropping the table or creating the view
     */
    public void createView() throws SQLException {
        Database db = this.osw.getDatabase();
        Connection con = db.getConnection();

        con.setAutoCommit(false);

        String dropSql = "DROP VIEW overlappingfeaturessequencefeature";
        String viewSql;

        if (osw.getSchema().useRangeTypes()) {
            viewSql =
                    "CREATE VIEW overlappingfeaturessequencefeature "
                            + " AS SELECT l1.featureid AS overlappingfeatures, "
                            + "           l2.featureid AS sequencefeature "
                            + "      FROM location l1, location l2 "
                            + "     WHERE l1.locatedonid = l2.locatedonid "
                            + "       AND l1.featureid != l2.featureid"
                            + "       AND int4range(l1.intermine_start, l1.intermine_end) "
                            + "              && int4range(l2.intermine_start, l2.intermine_end)";
        } else if (osw.getSchema().hasBioSeg()) {
            viewSql =
                    "CREATE VIEW overlappingfeaturessequencefeature "
                            + " AS SELECT l1.featureid AS overlappingfeatures, "
                            + "           l2.featureid AS sequencefeature "
                            + "      FROM location l1, location l2 "
                            + "     WHERE l1.locatedonid = l2.locatedonid "
                            + "       AND l1.featureid != l2.featureid"
                            + "       AND bioseg_create(l1.intermine_start, l1.intermine_end) "
                            + "            && bioseg_create(l2.intermine_start, l2.intermine_end)";
        } else {
            throw new IllegalArgumentException("Attempt to create overlappingfeatures view but"
                    + " database doesn't support Postgres built in ranges (has to be > 9.2"
                    + " and doesn't have bioseg installed. Aborting.");
        }

        Statement statement = con.createStatement();
        statement.executeUpdate(dropSql);
        statement.close();

        statement = con.createStatement();
        statement.executeUpdate(viewSql);
        statement.close();

        con.commit();
        con.close();
    }
}
{'fraction_non_alphanumeric': 0.07838745800671892, 'fraction_numerical': 0.009238521836506159, 'mean_word_length': 2.061268209083119, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3142819', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1828, 'n_words': 512}