<commit_before>/*
 * Copyright 2008-2010 Microarray Informatics Team, 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.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.gxa.index;

import uk.ac.ebi.gxa.utils.EscapeUtil;
import uk.ac.ebi.gxa.utils.FilterIterator;
import uk.ac.ebi.microarray.atlas.model.ExpressionAnalysis;

import java.io.*;
import java.util.*;

/**
 * @author pashky
 */
public class GeneExpressionAnalyticsTable implements Serializable {
    private ArrayList<ExpressionAnalysis> expas = new ArrayList<ExpressionAnalysis>();
    private HashMap<String, BitSet> byEfEfvId = new HashMap<String, BitSet>();
    private HashMap<String, BitSet> byEfoId = new HashMap<String, BitSet>();
    private static final long serialVersionUID = 2L;

    public void add(ExpressionAnalysis analysis) {
        expas.add(analysis);

        int pos = expas.size() - 1;

        String efefvId = EscapeUtil.encode(analysis.getEfName(), analysis.getEfvName());
        if(!byEfEfvId.containsKey(efefvId))
            byEfEfvId.put(efefvId, new BitSet());

        byEfEfvId.get(efefvId).set(pos);

        if(analysis.getEfoAccessions() != null)
            for(String oneefo : analysis.getEfoAccessions()) {
                if(!byEfoId.containsKey(oneefo))
                    byEfoId.put(oneefo, new BitSet());
                byEfoId.get(oneefo).set(pos);
            }

    }

    public void addAll(Collection<ExpressionAnalysis> analyses) {
        expas.addAll(analyses);

        int pos = expas.size() - 1;

        for (ExpressionAnalysis analysis : analyses) {
            String efefvId = EscapeUtil.encode(analysis.getEfName(), analysis.getEfvName());
            if(!byEfEfvId.containsKey(efefvId))
                byEfEfvId.put(efefvId, new BitSet());

            byEfEfvId.get(efefvId).set(pos);

            if(analysis.getEfoAccessions() != null)
                for(String oneefo : analysis.getEfoAccessions()) {
                    if(!byEfoId.containsKey(oneefo))
                        byEfoId.put(oneefo, new BitSet());
                    byEfoId.get(oneefo).set(pos);
                }
        }
    }

    private Iterable<ExpressionAnalysis> makeIterable(final BitSet bs) {
        return new Iterable<ExpressionAnalysis>() {
            public Iterator<ExpressionAnalysis> iterator() {
                return new Iterator<ExpressionAnalysis>() {
                    int pos = bs.nextSetBit(0);

                    public boolean hasNext() {
                        return pos >= 0; 
                    }

                    public ExpressionAnalysis next() {
                        int curr = pos;
                        pos = bs.nextSetBit(pos + 1);<commit_msg>Fixed a bug in addAll(Collection<ExpressionAnalysis> analyses) method that caused all efefvs and efos belonging to each of the added analyses to the last analysis to be added, instead of the one they belonged to. Luckily this method was not used anywhere.<commit_after>/*
 * Copyright 2008-2010 Microarray Informatics Team, 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.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.gxa.index;

import uk.ac.ebi.gxa.utils.EscapeUtil;
import uk.ac.ebi.gxa.utils.FilterIterator;
import uk.ac.ebi.microarray.atlas.model.ExpressionAnalysis;

import java.io.*;
import java.util.*;

/**
 * @author pashky
 */
public class GeneExpressionAnalyticsTable implements Serializable {
    private ArrayList<ExpressionAnalysis> expas = new ArrayList<ExpressionAnalysis>();
    private HashMap<String, BitSet> byEfEfvId = new HashMap<String, BitSet>();
    private HashMap<String, BitSet> byEfoId = new HashMap<String, BitSet>();
    private static final long serialVersionUID = 2L;

    public void add(ExpressionAnalysis analysis) {
        expas.add(analysis);

        int pos = expas.size() - 1;

        String efefvId = EscapeUtil.encode(analysis.getEfName(), analysis.getEfvName());
        if(!byEfEfvId.containsKey(efefvId))
            byEfEfvId.put(efefvId, new BitSet());

        byEfEfvId.get(efefvId).set(pos);

        if(analysis.getEfoAccessions() != null)
            for(String oneefo : analysis.getEfoAccessions()) {
                if(!byEfoId.containsKey(oneefo))
                    byEfoId.put(oneefo, new BitSet());
                byEfoId.get(oneefo).set(pos);
            }

    }

    public void addAll(Collection<ExpressionAnalysis> analyses) {
        int pos = expas.size() - 1; // last valid position before adding new analyses

        expas.addAll(analyses);

        for (ExpressionAnalysis analysis : analyses) {
            pos++;
            String efefvId = EscapeUtil.encode(analysis.getEfName(), analysis.getEfvName());
            if(!byEfEfvId.containsKey(efefvId))
                byEfEfvId.put(efefvId, new BitSet());

            byEfEfvId.get(efefvId).set(pos);

            if(analysis.getEfoAccessions() != null)
                for(String oneefo : analysis.getEfoAccessions()) {
                    if(!byEfoId.containsKey(oneefo))
                        byEfoId.put(oneefo, new BitSet());
                    byEfoId.get(oneefo).set(pos);
                }
        }
    }

    private Iterable<ExpressionAnalysis> makeIterable(final BitSet bs) {
        return new Iterable<ExpressionAnalysis>() {
            public Iterator<ExpressionAnalysis> iterator() {
                return new Iterator<ExpressionAnalysis>() {
                    int pos = bs.nextSetBit(0);

                    public boolean hasNext() {
                        return pos >= 0; 
                    }

                    public ExpressionAnalysis next() {
                        int curr = pos;
                        pos = bs.nextSetBit(pos + 1);
{'fraction_non_alphanumeric': 0.0992787441663131, 'fraction_numerical': 0.005091217649554518, 'mean_word_length': 2.507936507936508, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '833796', 'n_tokens_mistral': 2094, 'n_tokens_neox': 1948, 'n_words': 529}