<commit_before>package org.cbioportal.service.impl;

import org.cbioportal.model.GeneMolecularAlteration;
import org.cbioportal.model.GeneMolecularData;
import org.cbioportal.model.MolecularProfile;
import org.cbioportal.model.MolecularProfile.MolecularAlterationType;
import org.cbioportal.model.Sample;
import org.cbioportal.model.meta.BaseMeta;
import org.cbioportal.persistence.MolecularDataRepository;
import org.cbioportal.persistence.SampleListRepository;
import org.cbioportal.service.MolecularDataService;
import org.cbioportal.service.MolecularProfileService;
import org.cbioportal.service.SampleService;
import org.cbioportal.service.exception.MolecularProfileNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MolecularDataServiceImpl implements MolecularDataService {

    @Autowired
    private MolecularDataRepository molecularDataRepository;
    @Autowired
    private SampleService sampleService;
    @Autowired
    private MolecularProfileService molecularProfileService;
    @Autowired
    private SampleListRepository sampleListRepository;

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public List<GeneMolecularData> getMolecularData(String molecularProfileId, String sampleListId,
                                                    List<Integer> entrezGeneIds, String projection)
        throws MolecularProfileNotFoundException {
        
        validateMolecularProfile(molecularProfileId);
        List<String> sampleIds = sampleListRepository.getAllSampleIdsInSampleList(sampleListId);
        if (sampleIds.isEmpty()) {
            return Collections.emptyList();
        }
        return fetchMolecularData(molecularProfileId, sampleIds, entrezGeneIds, projection);
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public BaseMeta getMetaMolecularData(String molecularProfileId, String sampleListId, List<Integer> entrezGeneIds) 
        throws MolecularProfileNotFoundException {
        
        BaseMeta baseMeta = new BaseMeta();
        baseMeta.setTotalCount(getMolecularData(molecularProfileId, sampleListId, entrezGeneIds, "ID").size());
        return baseMeta;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public List<GeneMolecularData> fetchMolecularData(String molecularProfileId, List<String> sampleIds,
                                                      List<Integer> entrezGeneIds, String projection) 
        throws MolecularProfileNotFoundException {

        validateMolecularProfile(molecularProfileId);
        List<GeneMolecularData> molecularDataList = new ArrayList<>();

        String commaSeparatedSampleIdsOfMolecularProfile = molecularDataRepository
            .getCommaSeparatedSampleIdsOfMolecularProfile(molecularProfileId);
        if (commaSeparatedSampleIdsOfMolecularProfile == null) {
            return molecularDataList;
        }
        List<Integer> internalSampleIds = Arrays.stream(commaSeparatedSampleIdsOfMolecularProfile.split(","))
            .mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());

        List<Sample> samples;
        if (sampleIds == null) {
            samples = sampleService.getSamplesByInternalIds(internalSampleIds);
        } else {
            MolecularProfile molecularProfile = molecularProfileService.getMolecularProfile(molecularProfileId);
            List<String> studyIds = new ArrayList<>();
            sampleIds.forEach(s -> studyIds.add(molecularProfile.getCancerStudyIdentifier()));
            samples = sampleService.fetchSamples(studyIds, sampleIds, "ID");
        }

        List<GeneMolecularAlteration> molecularAlterations = molecularDataRepository.getGeneMolecularAlterations(
            molecularProfileId, entrezGeneIds, projection);
        
        for (Sample sample : samples) {
            int indexOfSampleId = internalSampleIds.indexOf(sample.getInternalId());
            if (indexOfSampleId != -1) {
                for (GeneMolecularAlteration molecularAlteration : molecularAlterations) {
                    GeneMolecularData molecularData = new GeneMolecularData();
                    molecularData.setMolecularProfileId(molecularProfileId);
                    molecularData.setSampleId(sample.getStableId());
                    molecularData.setPatientId(sample.getPatientStableId());
                    molecularData.setStudyId(sample.getCancerStudyIdentifier());
                    molecularData.setEntrezGeneId(molecularAlteration.getEntrezGeneId());
                    molecularData.setValue(molecularAlteration.getSplitValues()[indexOfSampleId]);
                    molecularData.setGene(molecularAlteration.getGene());
                    molecularDataList.add(molecularData);
                }
            }
        }
        
        return molecularDataList;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public BaseMeta fetchMetaMolecularData(String molecularProfileId, List<String> sampleIds, 
                                           List<Integer> entrezGeneIds) throws MolecularProfileNotFoundException {
        
        BaseMeta baseMeta = new BaseMeta();
        baseMeta.setTotalCount(fetchMolecularData(molecularProfileId, sampleIds, entrezGeneIds, "ID").size());
        return baseMeta;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public Integer getNumberOfSamplesInMolecularProfile(String molecularProfileId) {

        String commaSeparatedSampleIdsOfMolecularProfile = molecularDataRepository
            .getCommaSeparatedSampleIdsOfMolecularProfile(molecularProfileId);
        if (commaSeparatedSampleIdsOfMolecularProfile == null) {
            return null;
        }
        
        return commaSeparatedSampleIdsOfMolecularProfile.split(",").length;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileIds, 'List<MolecularProfileId>', 'read')")
	public List<GeneMolecularData> getMolecularDataInMultipleMolecularProfiles(List<String> molecularProfileIds,
			List<String> sampleIds, List<Integer> entrezGeneIds, String projection) {

        List<GeneMolecularData> molecularDataList = new ArrayList<>();
        List<String> distinctMolecularProfileIds = molecularProfileIds.stream().distinct().sorted().collect(Collectors.toList());

        List<String> commaSeparatedSampleIdsOfMolecularProfiles = molecularDataRepository
            .getCommaSeparatedSampleIdsOfMolecularProfiles(distinctMolecularProfileIds);

        Map<String, List<Integer>> internalSampleIdsMap = new HashMap<>();
        List<Integer> allInternalSampleIds = new ArrayList<>();
        
        for (int i = 0; i < distinctMolecularProfileIds.size(); i++) {
            List<Integer> internalSampleIds = Arrays.stream(commaSeparatedSampleIdsOfMolecularProfiles.get(i).split(","))
                .mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
            internalSampleIdsMap.put(distinctMolecularProfileIds.get(i), internalSampleIds);
            allInternalSampleIds.addAll(internalSampleIds);
        }

        List<MolecularProfile> molecularProfiles = new ArrayList<>();
        List<MolecularProfile> distinctMolecularProfiles = molecularProfileService.getMolecularProfiles(
            distinctMolecularProfileIds, "SUMMARY");
        Map<String, MolecularProfile> molecularProfileMapById = distinctMolecularProfiles.stream().collect(
            Collectors.toMap(MolecularProfile::getStableId, Function.identity()));
        Map<String, MolecularProfile> molecularProfileMapByStudyId = distinctMolecularProfiles.stream().collect(
            Collectors.toMap(MolecularProfile::getCancerStudyIdentifier, Function.identity()));
        List<Sample> samples;
        if (sampleIds == null) {
            samples = sampleService.getSamplesByInternalIds(allInternalSampleIds);
            for (String molecularProfileId : distinctMolecularProfileIds) {
                internalSampleIdsMap.get(molecularProfileId).forEach(s -> molecularProfiles.add(molecularProfileMapById
                    .get(molecularProfileId)));
            }
        } else {
            for (String molecularProfileId : molecularProfileIds) {
                molecularProfiles.add(molecularProfileMapById.get(molecularProfileId));
            }
            List<String> studyIds = molecularProfiles.stream().map(MolecularProfile::getCancerStudyIdentifier)
                .collect(Collectors.toList());
            samples = sampleService.fetchSamples(studyIds, sampleIds, "ID");
        }

        List<GeneMolecularAlteration> molecularAlterations = molecularDataRepository
            .getGeneMolecularAlterationsInMultipleMolecularProfiles(distinctMolecularProfileIds, entrezGeneIds, projection);
        Map<String, List<GeneMolecularAlteration>> molecularAlterationsMap = molecularAlterations.stream().collect(
            Collectors.groupingBy(GeneMolecularAlteration::getMolecularProfileId));
        
        for (Sample sample : samples) {
            String molecularProfileId = molecularProfileMapByStudyId.get(sample.getCancerStudyIdentifier()).getStableId();
            int indexOfSampleId = internalSampleIdsMap.get(molecularProfileId).indexOf(sample.getInternalId());
            if (indexOfSampleId != -1 && molecularAlterationsMap.containsKey(molecularProfileId)) {
                for (GeneMolecularAlteration molecularAlteration : molecularAlterationsMap.get(molecularProfileId)) {
                    GeneMolecularData molecularData = new GeneMolecularData();
                    molecularData.setMolecularProfileId(molecularProfileId);
                    molecularData.setSampleId(sample.getStableId());
                    molecularData.setPatientId(sample.getPatientStableId());
                    molecularData.setStudyId(sample.getCancerStudyIdentifier());
                    molecularData.setEntrezGeneId(molecularAlteration.getEntrezGeneId());
                    molecularData.setValue(molecularAlteration.getSplitValues()[indexOfSampleId]);
                    molecularData.setGene(molecularAlteration.getGene());
                    molecularDataList.add(molecularData);
                }
            }
        }
        
        return molecularDataList;
	}

    @Override
    @PreAuthorize("hasPermission(#molecularProfileIds, 'List<MolecularProfileId>', 'read')")
	public BaseMeta getMetaMolecularDataInMultipleMolecularProfiles(List<String> molecularProfileIds,
			List<String> sampleIds, List<Integer> entrezGeneIds) {
                
		BaseMeta baseMeta = new BaseMeta();
        baseMeta.setTotalCount(getMolecularDataInMultipleMolecularProfiles(molecularProfileIds, sampleIds, entrezGeneIds, "ID")
            .size());
        return baseMeta;
	}
<commit_msg>Fix Molecular Data API fetch endpoint<commit_after>package org.cbioportal.service.impl;

import org.cbioportal.model.GeneMolecularAlteration;
import org.cbioportal.model.GeneMolecularData;
import org.cbioportal.model.MolecularProfile;
import org.cbioportal.model.MolecularProfile.MolecularAlterationType;
import org.cbioportal.model.Sample;
import org.cbioportal.model.meta.BaseMeta;
import org.cbioportal.persistence.MolecularDataRepository;
import org.cbioportal.persistence.SampleListRepository;
import org.cbioportal.service.MolecularDataService;
import org.cbioportal.service.MolecularProfileService;
import org.cbioportal.service.SampleService;
import org.cbioportal.service.exception.MolecularProfileNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MolecularDataServiceImpl implements MolecularDataService {

    @Autowired
    private MolecularDataRepository molecularDataRepository;
    @Autowired
    private SampleService sampleService;
    @Autowired
    private MolecularProfileService molecularProfileService;
    @Autowired
    private SampleListRepository sampleListRepository;

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public List<GeneMolecularData> getMolecularData(String molecularProfileId, String sampleListId,
                                                    List<Integer> entrezGeneIds, String projection)
        throws MolecularProfileNotFoundException {
        
        validateMolecularProfile(molecularProfileId);
        List<String> sampleIds = sampleListRepository.getAllSampleIdsInSampleList(sampleListId);
        if (sampleIds.isEmpty()) {
            return Collections.emptyList();
        }
        return fetchMolecularData(molecularProfileId, sampleIds, entrezGeneIds, projection);
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public BaseMeta getMetaMolecularData(String molecularProfileId, String sampleListId, List<Integer> entrezGeneIds) 
        throws MolecularProfileNotFoundException {
        
        BaseMeta baseMeta = new BaseMeta();
        baseMeta.setTotalCount(getMolecularData(molecularProfileId, sampleListId, entrezGeneIds, "ID").size());
        return baseMeta;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public List<GeneMolecularData> fetchMolecularData(String molecularProfileId, List<String> sampleIds,
                                                      List<Integer> entrezGeneIds, String projection) 
        throws MolecularProfileNotFoundException {

        validateMolecularProfile(molecularProfileId);
        List<GeneMolecularData> molecularDataList = new ArrayList<>();

        String commaSeparatedSampleIdsOfMolecularProfile = molecularDataRepository
            .getCommaSeparatedSampleIdsOfMolecularProfile(molecularProfileId);
        if (commaSeparatedSampleIdsOfMolecularProfile == null) {
            return molecularDataList;
        }
        List<Integer> internalSampleIds = Arrays.stream(commaSeparatedSampleIdsOfMolecularProfile.split(","))
            .mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());

        List<Sample> samples;
        if (sampleIds == null) {
            samples = sampleService.getSamplesByInternalIds(internalSampleIds);
        } else {
            MolecularProfile molecularProfile = molecularProfileService.getMolecularProfile(molecularProfileId);
            List<String> studyIds = new ArrayList<>();
            sampleIds.forEach(s -> studyIds.add(molecularProfile.getCancerStudyIdentifier()));
            samples = sampleService.fetchSamples(studyIds, sampleIds, "ID");
        }

        List<GeneMolecularAlteration> molecularAlterations = molecularDataRepository.getGeneMolecularAlterations(
            molecularProfileId, entrezGeneIds, projection);
        
        for (Sample sample : samples) {
            int indexOfSampleId = internalSampleIds.indexOf(sample.getInternalId());
            if (indexOfSampleId != -1) {
                for (GeneMolecularAlteration molecularAlteration : molecularAlterations) {
                    GeneMolecularData molecularData = new GeneMolecularData();
                    molecularData.setMolecularProfileId(molecularProfileId);
                    molecularData.setSampleId(sample.getStableId());
                    molecularData.setPatientId(sample.getPatientStableId());
                    molecularData.setStudyId(sample.getCancerStudyIdentifier());
                    molecularData.setEntrezGeneId(molecularAlteration.getEntrezGeneId());
                    molecularData.setValue(molecularAlteration.getSplitValues()[indexOfSampleId]);
                    molecularData.setGene(molecularAlteration.getGene());
                    molecularDataList.add(molecularData);
                }
            }
        }
        
        return molecularDataList;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public BaseMeta fetchMetaMolecularData(String molecularProfileId, List<String> sampleIds, 
                                           List<Integer> entrezGeneIds) throws MolecularProfileNotFoundException {
        
        BaseMeta baseMeta = new BaseMeta();
        baseMeta.setTotalCount(fetchMolecularData(molecularProfileId, sampleIds, entrezGeneIds, "ID").size());
        return baseMeta;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileId, 'MolecularProfile', 'read')")
    public Integer getNumberOfSamplesInMolecularProfile(String molecularProfileId) {

        String commaSeparatedSampleIdsOfMolecularProfile = molecularDataRepository
            .getCommaSeparatedSampleIdsOfMolecularProfile(molecularProfileId);
        if (commaSeparatedSampleIdsOfMolecularProfile == null) {
            return null;
        }
        
        return commaSeparatedSampleIdsOfMolecularProfile.split(",").length;
    }

    @Override
    @PreAuthorize("hasPermission(#molecularProfileIds, 'List<MolecularProfileId>', 'read')")
	public List<GeneMolecularData> getMolecularDataInMultipleMolecularProfiles(List<String> molecularProfileIds,
			List<String> sampleIds, List<Integer> entrezGeneIds, String projection) {

        List<GeneMolecularData> molecularDataList = new ArrayList<>();
        List<String> distinctMolecularProfileIds = molecularProfileIds.stream().distinct().sorted().collect(Collectors.toList());

        List<String> commaSeparatedSampleIdsOfMolecularProfiles = molecularDataRepository
            .getCommaSeparatedSampleIdsOfMolecularProfiles(distinctMolecularProfileIds);

        Map<String, List<Integer>> internalSampleIdsMap = new HashMap<>();
        List<Integer> allInternalSampleIds = new ArrayList<>();
        
        for (int i = 0; i < distinctMolecularProfileIds.size(); i++) {
            List<Integer> internalSampleIds = Arrays.stream(commaSeparatedSampleIdsOfMolecularProfiles.get(i).split(","))
                .mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
            internalSampleIdsMap.put(distinctMolecularProfileIds.get(i), internalSampleIds);
            allInternalSampleIds.addAll(internalSampleIds);
        }

        List<MolecularProfile> molecularProfiles = new ArrayList<>();
        List<MolecularProfile> distinctMolecularProfiles = molecularProfileService.getMolecularProfiles(
            distinctMolecularProfileIds, "SUMMARY");
        Map<String, MolecularProfile> molecularProfileMapById = distinctMolecularProfiles.stream().collect(
            Collectors.toMap(MolecularProfile::getStableId, Function.identity()));
        Map<String, List<MolecularProfile>> molecularProfileMapByStudyId = distinctMolecularProfiles.stream().collect(
            Collectors.groupingBy(MolecularProfile::getCancerStudyIdentifier));
        List<Sample> samples;
        if (sampleIds == null) {
            samples = sampleService.getSamplesByInternalIds(allInternalSampleIds);
            for (String molecularProfileId : distinctMolecularProfileIds) {
                internalSampleIdsMap.get(molecularProfileId).forEach(s -> molecularProfiles.add(molecularProfileMapById
                    .get(molecularProfileId)));
            }
        } else {
            for (String molecularProfileId : molecularProfileIds) {
                molecularProfiles.add(molecularProfileMapById.get(molecularProfileId));
            }
            List<String> studyIds = molecularProfiles.stream().map(MolecularProfile::getCancerStudyIdentifier)
                .collect(Collectors.toList());
            samples = sampleService.fetchSamples(studyIds, sampleIds, "ID");
        }

        List<GeneMolecularAlteration> molecularAlterations = molecularDataRepository
            .getGeneMolecularAlterationsInMultipleMolecularProfiles(distinctMolecularProfileIds, entrezGeneIds, projection);
        Map<String, List<GeneMolecularAlteration>> molecularAlterationsMap = molecularAlterations.stream().collect(
            Collectors.groupingBy(GeneMolecularAlteration::getMolecularProfileId));
        
        for (Sample sample : samples) {
            for (MolecularProfile molecularProfile : molecularProfileMapByStudyId.get(sample.getCancerStudyIdentifier())) {
                String molecularProfileId = molecularProfile.getStableId();
                int indexOfSampleId = internalSampleIdsMap.get(molecularProfileId).indexOf(sample.getInternalId());
                if (indexOfSampleId != -1 && molecularAlterationsMap.containsKey(molecularProfileId)) {
                    for (GeneMolecularAlteration molecularAlteration : molecularAlterationsMap.get(molecularProfileId)) {
                        GeneMolecularData molecularData = new GeneMolecularData();
                        molecularData.setMolecularProfileId(molecularProfileId);
                        molecularData.setSampleId(sample.getStableId());
                        molecularData.setPatientId(sample.getPatientStableId());
                        molecularData.setStudyId(sample.getCancerStudyIdentifier());
                        molecularData.setEntrezGeneId(molecularAlteration.getEntrezGeneId());
                        molecularData.setValue(molecularAlteration.getSplitValues()[indexOfSampleId]);
                        molecularData.setGene(molecularAlteration.getGene());
                        molecularDataList.add(molecularData);
                    }
                }
            }
        }
        
        return molecularDataList;
	}

    @Override
    @PreAuthorize("hasPermission(#molecularProfileIds, 'List<MolecularProfileId>', 'read')")
	public BaseMeta getMetaMolecularDataInMultipleMolecularProfiles(List<String> molecularProfileIds,
			List<String> sampleIds, List<Integer> entrezGeneIds) {
                
		BaseMeta baseMeta = new BaseMeta();
        baseMeta.setTotalCount(getMolecularDataInMultipleMolecularProfiles(molecularProfileIds, sampleIds, entrezGeneIds, "ID")
            .size());
        return baseMeta;
	}

{'fraction_non_alphanumeric': 0.08440616898809263, 'fraction_numerical': 0.0002636319697702008, 'mean_word_length': 3.5006921099466086, 'pattern_counts': {'":': 0, '<': 96, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142784', 'n_tokens_mistral': 6081, 'n_tokens_neox': 5356, 'n_words': 844}