<commit_before>/*
 * Copyright 2015-2020 OpenCB
 *
 * 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.opencb.cellbase.lib.managers;

import org.opencb.biodata.models.core.Gene;
import org.opencb.cellbase.core.api.core.GeneDBAdaptor;
import org.opencb.cellbase.core.api.queries.GeneQuery;
import org.opencb.cellbase.core.api.queries.QueryException;
import org.opencb.cellbase.core.config.CellBaseConfiguration;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.commons.datastore.core.Query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GeneManager extends AbstractManager {

    private GeneDBAdaptor geneDBAdaptor;

    public GeneManager(String species, String assembly, CellBaseConfiguration configuration) {
        super(species, assembly, configuration);
        this.init();
    }

    private void init() {
        geneDBAdaptor = dbAdaptorFactory.getGeneDBAdaptor(species, assembly);
    }

    public CellBaseDataResult<Gene> search(GeneQuery geneQuery) throws QueryException {
        geneQuery.setDefaults();
        geneQuery.validate();
        // TODO throw execption if facets populated
//        return geneDBAdaptor.nativeGet(geneQuery);
        return null;
    }

    public CellBaseDataResult<Gene> groupBy(Query geneQuery, String fields) {
        return geneDBAdaptor.groupBy(geneQuery, Arrays.asList(fields.split(",")), null);
    }

    public CellBaseDataResult<Gene> aggregationStats(Query geneQuery, String fields) {
//        geneQuery.setCount(Boolean.TRUE);
        return geneDBAdaptor.groupBy(geneQuery, Arrays.asList(fields.split(",")), null);
    }

    public List<CellBaseDataResult> info(Query geneQuery, String genes) {
        List<Query> queries = createXrefQueries(geneQuery, genes);
        List<CellBaseDataResult> geneQueryResults = geneDBAdaptor.nativeGet(queries, null);
//        for (int i = 0; i < queries.size(); i++) {
//            geneQueryResults.get(i).setId((queries.get(i).getTranscriptsXrefs().get(0)));
//        }
        return geneQueryResults;
    }

    private List<Query> createXrefQueries(Query geneQuery, String xrefs) {
        String[] ids = xrefs.split(",");
        List<Query> queries = new ArrayList<>(ids.length);
        for (String id : ids) {
            // TODO need to clone query properly
            Query geneXrefQuery = new Query();
//            geneXrefQuery.setTranscriptsXrefs(id);
            queries.add(geneXrefQuery);
        }
        return queries;
    }

    public CellBaseDataResult<Gene> distinct(Query geneQuery, String field) {
        return geneDBAdaptor.distinct(geneQuery, field);
    }

    public List<CellBaseDataResult> getRegulatoryElements(Query geneQuery, String genes) {
        String[] geneArray = genes.split(",");<commit_msg>add iterator to gene manager<commit_after>/*
 * Copyright 2015-2020 OpenCB
 *
 * 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.opencb.cellbase.lib.managers;

import org.opencb.biodata.models.core.Gene;
import org.opencb.cellbase.core.api.core.GeneDBAdaptor;
import org.opencb.cellbase.core.api.queries.GeneQuery;
import org.opencb.cellbase.core.api.queries.QueryException;
import org.opencb.cellbase.core.config.CellBaseConfiguration;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.commons.datastore.core.Query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class GeneManager extends AbstractManager {

    private GeneDBAdaptor geneDBAdaptor;

    public GeneManager(String species, String assembly, CellBaseConfiguration configuration) {
        super(species, assembly, configuration);
        this.init();
    }

    private void init() {
        geneDBAdaptor = dbAdaptorFactory.getGeneDBAdaptor(species, assembly);
    }

    public CellBaseDataResult<Gene> search(GeneQuery geneQuery) throws QueryException {
        geneQuery.setDefaults();
        geneQuery.validate();
        // TODO throw execption if facets populated
//        return geneDBAdaptor.nativeGet(geneQuery);
        return null;
    }

    public CellBaseDataResult<Gene> groupBy(Query geneQuery, String fields) {
        return geneDBAdaptor.groupBy(geneQuery, Arrays.asList(fields.split(",")), null);
    }

    public CellBaseDataResult<Gene> aggregationStats(Query geneQuery, String fields) {
//        geneQuery.setCount(Boolean.TRUE);
        return geneDBAdaptor.groupBy(geneQuery, Arrays.asList(fields.split(",")), null);
    }

    public List<CellBaseDataResult> info(Query geneQuery, String genes) {
        List<Query> queries = createXrefQueries(geneQuery, genes);
        List<CellBaseDataResult> geneQueryResults = geneDBAdaptor.nativeGet(queries, null);
//        for (int i = 0; i < queries.size(); i++) {
//            geneQueryResults.get(i).setId((queries.get(i).getTranscriptsXrefs().get(0)));
//        }
        return geneQueryResults;
    }

    private List<Query> createXrefQueries(Query geneQuery, String xrefs) {
        String[] ids = xrefs.split(",");
        List<Query> queries = new ArrayList<>(ids.length);
        for (String id : ids) {
            // TODO need to clone query properly
            Query geneXrefQuery = new Query();
//            geneXrefQuery.setTranscriptsXrefs(id);
            queries.add(geneXrefQuery);
        }
        return queries;
    }

    public CellBaseDataResult<Gene> distinct(Query geneQuery, String field) {
        return geneDBAdaptor.distinct(geneQuery, field);
    }

    public Iterator<CellBaseDataResult<Gene> > iterator(Query geneQuery, String field) {
        return geneDBAdaptor.iterator(geneQuery, null);
    }

    public List<CellBaseDataResult> getRegulatoryElements(Query geneQuery, String genes) {
        String[] geneArray = genes.split(",");
{'fraction_non_alphanumeric': 0.10180600058258084, 'fraction_numerical': 0.00407806583163414, 'mean_word_length': 3.514792899408284, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, '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': '6746210', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1970, 'n_words': 496}