<commit_before>package org.flymine.postprocess;

/*
 * Copyright (C) 2002-2004 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.util.Collections;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

import org.biojava.bio.symbol.SymbolList;
import org.biojava.bio.symbol.IllegalSymbolException;
import org.biojava.bio.symbol.IllegalAlphabetException;
import org.biojava.bio.seq.DNATools;

import org.intermine.objectstore.query.*;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.util.DynamicUtil;

import org.intermine.objectstore.query.ResultsRow;
import org.flymine.model.genomic.*;

import org.apache.log4j.Logger;

/**
 * Transfer sequences from the Contig objects to the other objects that are located on the Contigs
 * and to the objects that the Contigs are located on (eg. Chromosomes).
 *
 * @author <NAME>
 */

public class TransferSequences
{
    protected ObjectStoreWriter osw;

    /**
     * Create a new TransferSequences object from the given ObjectStoreWriter
     * @param osw writer on genomic ObjectStore
     */
    public TransferSequences (ObjectStoreWriter osw) {
        this.osw = osw;
    }

    /**
     * Copy the contig sequences to the appropriate place in the chromosome sequences and store a
     * Sequence object for each Chromosome.  Uses the ObjectStoreWriter that was passed to the
     * constructor
     * @throws Exception if there are problems with the transfer
     */
    public void transferToChromosome()
        throws Exception {
        osw.beginTransaction();
        ObjectStore os = osw.getObjectStore();
        Results results = PostProcessUtil.findLocations(os, Chromosome.class, Contig.class, false);
        results.setBatchSize(500);
        
        Iterator resIter = results.iterator();        

        Chromosome currentChr = null;
        char[] currentChrBases = null;

        while (resIter.hasNext()) {
            ResultsRow rr = (ResultsRow) resIter.next();
            Integer chrId = (Integer) rr.get(0);
            Chromosome chr = (Chromosome) os.getObjectById(chrId);
            Contig contig = (Contig) rr.get(1);
            Location contigOnChrLocation = (Location) rr.get(2);

            if (currentChr == null || !chr.equals(currentChr)) {
                if (currentChr != null) {
                    storeNewSequence(currentChr, currentChrBases);
                }
                currentChrBases = new char[chr.getLength().intValue()];
                // fill with '.' so we can see the parts of the Chromosome sequence that haven't
                // been set
                for (int i = 0; i < currentChrBases.length; i++) {
                    currentChrBases[i] = '.';
                }
                currentChr = chr;
            }

            copySeqArray(currentChrBases, contig.getSequence().getSequence(),
                         contigOnChrLocation.getStart().intValue(),
                         contigOnChrLocation.getEnd().intValue(),
                         contigOnChrLocation.getStrand().intValue());
        }
        storeNewSequence(currentChr, currentChrBases);
        osw.commitTransaction();
    }

    private void storeNewSequence(LocatedSequenceFeature feature, char [] featureBases)
        throws ObjectStoreException {
        Sequence sequence =
            (Sequence) DynamicUtil.createObject(Collections.singleton(Sequence.class));
        sequence.setSequence(new String(featureBases));
        feature.setSequence(sequence);
        osw.store(feature);
        osw.store(sequence);
    }

    /**
     * Use the Location relations to copy the sequence from the Chromosomes to every
     * LocatedSequenceFeature that is located on a Chromosome and which doesn't already have a
     * sequence (ie. don't copy to Contig).  Uses the ObjectStoreWriter that was passed to the
     * constructor
     * @throws Exception if there are problems with the transfer
     */
    public void transferToLocatedSequenceFeatures()
        throws Exception {
        ObjectStore os = osw.getObjectStore();
        Results results = PostProcessUtil.findLocations(os, Chromosome.class,
                                                        LocatedSequenceFeature.class, true);

        Iterator resIter = results.iterator();        

        while (resIter.hasNext()) {
            ResultsRow rr = (ResultsRow) resIter.next();

            Integer chrId = (Integer) rr.get(0);
            LocatedSequenceFeature feature = (LocatedSequenceFeature) rr.get(1);
            Location locationOnChr = (Location) rr.get(2);

            if (feature instanceof Contig) {
                continue;
            }

            Chromosome chr = (Chromosome) os.getObjectById(chrId);
            Sequence chromosomeSequence = chr.getSequence();

            String featureSeq = getSubSequence(chromosomeSequence, locationOnChr);

            Sequence sequence =
                (Sequence) DynamicUtil.createObject(Collections.singleton(Sequence.class));
            sequence.setSequence(featureSeq);
            feature.setSequence(sequence);
            osw.store(feature);
            osw.store(sequence);
        }
    }

    private String getSubSequence(Sequence chromosomeSequence, Location locationOnChr)
        throws IllegalSymbolException, IllegalAlphabetException {
        int charsToCopy =
            locationOnChr.getEnd().intValue() - locationOnChr.getStart().intValue() + 1;
        String chromosomeSequenceString = chromosomeSequence.getSequence();
        int startPos = locationOnChr.getStart().intValue() - 1;
        int endPos = startPos + charsToCopy;
        String subSeqString = new String(chromosomeSequenceString.substring(startPos, endPos));
<commit_msg>transferToLocatedSequenceFeatures() now wraps its calls to store() in a transaction.<commit_after>package org.flymine.postprocess;

/*
 * Copyright (C) 2002-2004 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.util.Collections;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

import org.biojava.bio.symbol.SymbolList;
import org.biojava.bio.symbol.IllegalSymbolException;
import org.biojava.bio.symbol.IllegalAlphabetException;
import org.biojava.bio.seq.DNATools;

import org.intermine.objectstore.query.*;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreWriter;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.util.DynamicUtil;

import org.intermine.objectstore.query.ResultsRow;
import org.flymine.model.genomic.*;

import org.apache.log4j.Logger;

/**
 * Transfer sequences from the Contig objects to the other objects that are located on the Contigs
 * and to the objects that the Contigs are located on (eg. Chromosomes).
 *
 * @author <NAME>
 */

public class TransferSequences
{
    protected ObjectStoreWriter osw;

    /**
     * Create a new TransferSequences object from the given ObjectStoreWriter
     * @param osw writer on genomic ObjectStore
     */
    public TransferSequences (ObjectStoreWriter osw) {
        this.osw = osw;
    }

    /**
     * Copy the contig sequences to the appropriate place in the chromosome sequences and store a
     * Sequence object for each Chromosome.  Uses the ObjectStoreWriter that was passed to the
     * constructor
     * @throws Exception if there are problems with the transfer
     */
    public void transferToChromosome()
        throws Exception {
        osw.beginTransaction();
        ObjectStore os = osw.getObjectStore();
        Results results = PostProcessUtil.findLocations(os, Chromosome.class, Contig.class, false);
        results.setBatchSize(500);
        
        Iterator resIter = results.iterator();        

        Chromosome currentChr = null;
        char[] currentChrBases = null;

        while (resIter.hasNext()) {
            ResultsRow rr = (ResultsRow) resIter.next();
            Integer chrId = (Integer) rr.get(0);
            Chromosome chr = (Chromosome) os.getObjectById(chrId);
            Contig contig = (Contig) rr.get(1);
            Location contigOnChrLocation = (Location) rr.get(2);

            if (currentChr == null || !chr.equals(currentChr)) {
                if (currentChr != null) {
                    storeNewSequence(currentChr, currentChrBases);
                }
                currentChrBases = new char[chr.getLength().intValue()];
                // fill with '.' so we can see the parts of the Chromosome sequence that haven't
                // been set
                for (int i = 0; i < currentChrBases.length; i++) {
                    currentChrBases[i] = '.';
                }
                currentChr = chr;
            }

            copySeqArray(currentChrBases, contig.getSequence().getSequence(),
                         contigOnChrLocation.getStart().intValue(),
                         contigOnChrLocation.getEnd().intValue(),
                         contigOnChrLocation.getStrand().intValue());
        }
        storeNewSequence(currentChr, currentChrBases);
        osw.commitTransaction();
    }

    private void storeNewSequence(LocatedSequenceFeature feature, char [] featureBases)
        throws ObjectStoreException {
        Sequence sequence =
            (Sequence) DynamicUtil.createObject(Collections.singleton(Sequence.class));
        sequence.setSequence(new String(featureBases));
        feature.setSequence(sequence);
        osw.store(feature);
        osw.store(sequence);
    }

    /**
     * Use the Location relations to copy the sequence from the Chromosomes to every
     * LocatedSequenceFeature that is located on a Chromosome and which doesn't already have a
     * sequence (ie. don't copy to Contig).  Uses the ObjectStoreWriter that was passed to the
     * constructor
     * @throws Exception if there are problems with the transfer
     */
    public void transferToLocatedSequenceFeatures()
        throws Exception {
        ObjectStore os = osw.getObjectStore();
        osw.beginTransaction();

        Results results = PostProcessUtil.findLocations(os, Chromosome.class,
                                                        LocatedSequenceFeature.class, true);

        results.setBatchSize(50000);
        Iterator resIter = results.iterator();        

        while (resIter.hasNext()) {
            ResultsRow rr = (ResultsRow) resIter.next();

            Integer chrId = (Integer) rr.get(0);
            LocatedSequenceFeature feature = (LocatedSequenceFeature) rr.get(1);
            Location locationOnChr = (Location) rr.get(2);

            if (feature instanceof Contig) {
                continue;
            }

            Chromosome chr = (Chromosome) os.getObjectById(chrId);
            Sequence chromosomeSequence = chr.getSequence();

            String featureSeq = getSubSequence(chromosomeSequence, locationOnChr);

            Sequence sequence =
                (Sequence) DynamicUtil.createObject(Collections.singleton(Sequence.class));
            sequence.setSequence(featureSeq);
            feature.setSequence(sequence);
            osw.store(feature);
            osw.store(sequence);
        }

        osw.commitTransaction();
    }

    private String getSubSequence(Sequence chromosomeSequence, Location locationOnChr)
        throws IllegalSymbolException, IllegalAlphabetException {
        int charsToCopy =
            locationOnChr.getEnd().intValue() - locationOnChr.getStart().intValue() + 1;
        String chromosomeSequenceString = chromosomeSequence.getSequence();
        int startPos = locationOnChr.getStart().intValue() - 1;
        int endPos = startPos + charsToCopy;
        String subSeqString = new String(chromosomeSequenceString.substring(startPos, endPos));

{'fraction_non_alphanumeric': 0.0875396244818337, 'fraction_numerical': 0.003820206453710477, 'mean_word_length': 2.536648462201782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '421531', 'n_tokens_mistral': 3350, 'n_tokens_neox': 3149, 'n_words': 844}