<commit_before>package org.yeastrc.xlink.www.objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yeastrc.xlink.dto.AnnotationTypeDTO;
import org.yeastrc.xlink.dto.LinkerDTO;
import org.yeastrc.xlink.www.dto.SearchDTO;
import org.yeastrc.xlink.www.annotation_utils.GetAnnotationTypeData;
import org.yeastrc.xlink.www.dto.CutoffsAppliedOnImportDTO;
import org.yeastrc.xlink.www.exceptions.ProxlWebappDataException;
import org.yeastrc.xlink.www.form_page_objects.CutoffPageDisplaySearchLevel;
import org.yeastrc.xlink.www.searcher.CutoffsAppliedOnImportSearcher;
import org.yeastrc.xlink.www.searcher.SearchProgramDisplaySearcher;

/**
 * Used for the searchDetailsBlock.jsp and viewProject.jsp
 *
 */
public class SearchDTODetailsDisplayWrapper {
	
	private static final Logger log = Logger.getLogger( SearchDTODetailsDisplayWrapper.class );
	
	

	private SearchDTO searchDTO;

	private List<SearchProgramDisplay> searchProgramDisplayList; 
	
	private List<CutoffsAppliedOnImportWebDisplay> cutoffsAppliedOnImportList;
	
	/**
	 * Not used on viewProject.jsp
	 */
	private CutoffPageDisplaySearchLevel cutoffPageDisplaySearchLevel;
	


	/**
	 * @return
	 * @throws Exception
	 */
	public String getLinkersDisplayString() throws Exception {
		
		if ( searchDTO == null ) {
			
			throw new IllegalStateException( "searchDTO == null");
		}
		
		List<LinkerDTO> linkers = searchDTO.getLinkers();
		
		if ( linkers == null || linkers.isEmpty() ) {
			
			return "";
		}
		
		List<String> linkerAbbreviations = new ArrayList<>( linkers.size() );
		
		for ( LinkerDTO linker : linkers ) {
			
			linkerAbbreviations.add( linker.getAbbr() );
		}
		
		Collections.sort( linkerAbbreviations );
		
		String linkersString = linkerAbbreviations.get(0);
		
		if ( linkerAbbreviations.size() > 1 ) {

			//  start loop at second index
			for ( int index = 1; index < linkerAbbreviations.size(); index++ ) {

				linkersString += ", ";
				linkersString += linkerAbbreviations.get( index );
			}
		}
		
		return linkersString;
	}
	

	
	/**
	 * @return
	 * @throws Exception
	 */
	public List<SearchProgramDisplay> getSearchPrograms() throws Exception {
		
		if ( searchProgramDisplayList != null ) {
			
			return searchProgramDisplayList;
		}
		
		int searchId = getSearchDTO().getId();
		
		searchProgramDisplayList = SearchProgramDisplaySearcher.getInstance().getSearchProgramDisplay( searchId );
		
		return searchProgramDisplayList;
	}
	
	
	

	public List<CutoffsAppliedOnImportWebDisplay> getCutoffsAppliedOnImportList() throws Exception {
		
		if ( cutoffsAppliedOnImportList != null ) {
			
			return cutoffsAppliedOnImportList;
		}
		
		if ( searchDTO == null ) {
			
			throw new IllegalStateException( "searchDTO == null");
		}
		

		cutoffsAppliedOnImportList = new ArrayList<>();
		

		Integer searchId = searchDTO.getId();
		
		////////  List of cutoffs applied on import
		
		List<CutoffsAppliedOnImportDTO> cutoffsAppliedOnImportDTOList = 
				CutoffsAppliedOnImportSearcher.getInstance().getCutoffsAppliedOnImportDTOForSearchId( searchId );
		
		if ( ! cutoffsAppliedOnImportDTOList.isEmpty() ) {
			
			List<Integer> searchIds = new ArrayList<>( 1 );
			
			searchIds.add( searchId );

			Map<Integer, Map<Integer, AnnotationTypeDTO>> all_Peptide_Filterable_ForSearchIds =
					GetAnnotationTypeData.getInstance().getAll_Peptide_Filterable_ForSearchIds( searchIds );
			
			Map<Integer, Map<Integer, AnnotationTypeDTO>> all_Psm_Filterable_ForSearchIds =
					GetAnnotationTypeData.getInstance().getAll_Psm_Filterable_ForSearchIds( searchIds );
			
			if ( all_Peptide_Filterable_ForSearchIds == null ) {
				
				String msg = "ERROR: all_Peptide_Filterable_ForSearchIds == null ";
				log.error( msg );
				throw new ProxlWebappDataException( msg );
			}

			if ( all_Psm_Filterable_ForSearchIds == null ) {
				
				String msg = "ERROR: all_Psm_Filterable_ForSearchIds == null ";
				log.error( msg );
				throw new ProxlWebappDataException( msg );
			}
			
			Map<Integer, AnnotationTypeDTO> all_Peptide_Filterable_PerSearchId = all_Peptide_Filterable_ForSearchIds.get( searchId );
			Map<Integer, AnnotationTypeDTO> all_Psm_Filterable_PerSearchId = all_Psm_Filterable_ForSearchIds.get( searchId );
			
			
			for ( CutoffsAppliedOnImportDTO cutoffsAppliedOnImportDTO : cutoffsAppliedOnImportDTOList ) {
				
				AnnotationTypeDTO peptideAnnotationTypeDTO = all_Peptide_Filterable_PerSearchId.get( cutoffsAppliedOnImportDTO.getAnnotationTypeId() );
				AnnotationTypeDTO psmAnnotationTypeDTO = all_Psm_Filterable_PerSearchId.get( cutoffsAppliedOnImportDTO.getAnnotationTypeId() );
				
				
				CutoffsAppliedOnImportWebDisplay cutoffsAppliedOnImportWebDisplay = new CutoffsAppliedOnImportWebDisplay();

				cutoffsAppliedOnImportWebDisplay.setCutoffValue( cutoffsAppliedOnImportDTO.getCutoffValueString() );

				if ( peptideAnnotationTypeDTO != null ) {
					
					cutoffsAppliedOnImportWebDisplay.setAnnotationName( peptideAnnotationTypeDTO.getName() );
					cutoffsAppliedOnImportWebDisplay.setPeptideCutoff( true );
					
				} else if ( psmAnnotationTypeDTO != null ) {

					cutoffsAppliedOnImportWebDisplay.setAnnotationName( psmAnnotationTypeDTO.getName() );
					cutoffsAppliedOnImportWebDisplay.setPeptideCutoff( false );
				
				} else {

					String msg = "ERROR: cutoffsAppliedOnImportDTO AnnotationTypeId not found in Peptide or PSM. "
							+ "  AnnotationTypeId: " + cutoffsAppliedOnImportDTO.getAnnotationTypeId()
							+ ", Search id: " + searchId;<commit_msg>bugfix for when PSM import cutoff but no Peptide import cutoffs<commit_after>package org.yeastrc.xlink.www.objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yeastrc.xlink.dto.AnnotationTypeDTO;
import org.yeastrc.xlink.dto.LinkerDTO;
import org.yeastrc.xlink.www.dto.SearchDTO;
import org.yeastrc.xlink.www.annotation_utils.GetAnnotationTypeData;
import org.yeastrc.xlink.www.dto.CutoffsAppliedOnImportDTO;
import org.yeastrc.xlink.www.exceptions.ProxlWebappDataException;
import org.yeastrc.xlink.www.form_page_objects.CutoffPageDisplaySearchLevel;
import org.yeastrc.xlink.www.searcher.CutoffsAppliedOnImportSearcher;
import org.yeastrc.xlink.www.searcher.SearchProgramDisplaySearcher;

/**
 * Used for the searchDetailsBlock.jsp and viewProject.jsp
 *
 */
public class SearchDTODetailsDisplayWrapper {
	
	private static final Logger log = Logger.getLogger( SearchDTODetailsDisplayWrapper.class );
	
	

	private SearchDTO searchDTO;

	private List<SearchProgramDisplay> searchProgramDisplayList; 
	
	private List<CutoffsAppliedOnImportWebDisplay> cutoffsAppliedOnImportList;
	
	/**
	 * Not used on viewProject.jsp
	 */
	private CutoffPageDisplaySearchLevel cutoffPageDisplaySearchLevel;
	


	/**
	 * @return
	 * @throws Exception
	 */
	public String getLinkersDisplayString() throws Exception {
		
		if ( searchDTO == null ) {
			
			throw new IllegalStateException( "searchDTO == null");
		}
		
		List<LinkerDTO> linkers = searchDTO.getLinkers();
		
		if ( linkers == null || linkers.isEmpty() ) {
			
			return "";
		}
		
		List<String> linkerAbbreviations = new ArrayList<>( linkers.size() );
		
		for ( LinkerDTO linker : linkers ) {
			
			linkerAbbreviations.add( linker.getAbbr() );
		}
		
		Collections.sort( linkerAbbreviations );
		
		String linkersString = linkerAbbreviations.get(0);
		
		if ( linkerAbbreviations.size() > 1 ) {

			//  start loop at second index
			for ( int index = 1; index < linkerAbbreviations.size(); index++ ) {

				linkersString += ", ";
				linkersString += linkerAbbreviations.get( index );
			}
		}
		
		return linkersString;
	}
	

	
	/**
	 * @return
	 * @throws Exception
	 */
	public List<SearchProgramDisplay> getSearchPrograms() throws Exception {
		
		if ( searchProgramDisplayList != null ) {
			
			return searchProgramDisplayList;
		}
		
		int searchId = getSearchDTO().getId();
		
		searchProgramDisplayList = SearchProgramDisplaySearcher.getInstance().getSearchProgramDisplay( searchId );
		
		return searchProgramDisplayList;
	}
	
	
	

	public List<CutoffsAppliedOnImportWebDisplay> getCutoffsAppliedOnImportList() throws Exception {
		
		if ( cutoffsAppliedOnImportList != null ) {
			
			return cutoffsAppliedOnImportList;
		}
		
		if ( searchDTO == null ) {
			
			throw new IllegalStateException( "searchDTO == null");
		}
		

		cutoffsAppliedOnImportList = new ArrayList<>();
		

		Integer searchId = searchDTO.getId();
		
		////////  List of cutoffs applied on import
		
		List<CutoffsAppliedOnImportDTO> cutoffsAppliedOnImportDTOList = 
				CutoffsAppliedOnImportSearcher.getInstance().getCutoffsAppliedOnImportDTOForSearchId( searchId );
		
		if ( ! cutoffsAppliedOnImportDTOList.isEmpty() ) {
			
			List<Integer> searchIds = new ArrayList<>( 1 );
			
			searchIds.add( searchId );

			Map<Integer, Map<Integer, AnnotationTypeDTO>> all_Peptide_Filterable_ForSearchIds =
					GetAnnotationTypeData.getInstance().getAll_Peptide_Filterable_ForSearchIds( searchIds );
			
			Map<Integer, Map<Integer, AnnotationTypeDTO>> all_Psm_Filterable_ForSearchIds =
					GetAnnotationTypeData.getInstance().getAll_Psm_Filterable_ForSearchIds( searchIds );
			
			if ( all_Peptide_Filterable_ForSearchIds == null ) {
				
				String msg = "ERROR: all_Peptide_Filterable_ForSearchIds == null ";
				log.error( msg );
				throw new ProxlWebappDataException( msg );
			}

			if ( all_Psm_Filterable_ForSearchIds == null ) {
				
				String msg = "ERROR: all_Psm_Filterable_ForSearchIds == null ";
				log.error( msg );
				throw new ProxlWebappDataException( msg );
			}
			
			Map<Integer, AnnotationTypeDTO> all_Peptide_Filterable_PerSearchId = all_Peptide_Filterable_ForSearchIds.get( searchId );
			Map<Integer, AnnotationTypeDTO> all_Psm_Filterable_PerSearchId = all_Psm_Filterable_ForSearchIds.get( searchId );
			
			
			for ( CutoffsAppliedOnImportDTO cutoffsAppliedOnImportDTO : cutoffsAppliedOnImportDTOList ) {
				
				AnnotationTypeDTO peptideAnnotationTypeDTO = null;
				AnnotationTypeDTO psmAnnotationTypeDTO = null;
				
				if ( all_Peptide_Filterable_PerSearchId != null ) {
				
					peptideAnnotationTypeDTO = all_Peptide_Filterable_PerSearchId.get( cutoffsAppliedOnImportDTO.getAnnotationTypeId() );
				}

				if ( all_Psm_Filterable_PerSearchId != null ) {
				
					psmAnnotationTypeDTO = all_Psm_Filterable_PerSearchId.get( cutoffsAppliedOnImportDTO.getAnnotationTypeId() );
				}
				
				
				CutoffsAppliedOnImportWebDisplay cutoffsAppliedOnImportWebDisplay = new CutoffsAppliedOnImportWebDisplay();

				cutoffsAppliedOnImportWebDisplay.setCutoffValue( cutoffsAppliedOnImportDTO.getCutoffValueString() );

				if ( peptideAnnotationTypeDTO != null ) {
					
					cutoffsAppliedOnImportWebDisplay.setAnnotationName( peptideAnnotationTypeDTO.getName() );
					cutoffsAppliedOnImportWebDisplay.setPeptideCutoff( true );
					
				} else if ( psmAnnotationTypeDTO != null ) {

					cutoffsAppliedOnImportWebDisplay.setAnnotationName( psmAnnotationTypeDTO.getName() );
					cutoffsAppliedOnImportWebDisplay.setPeptideCutoff( false );
				
				} else {

					String msg = "ERROR: cutoffsAppliedOnImportDTO AnnotationTypeId not found in Peptide or PSM. "
							+ "  AnnotationTypeId: " + cutoffsAppliedOnImportDTO.getAnnotationTypeId()
							+ ", Search id: " + searchId;
{'fraction_non_alphanumeric': 0.08943796763528798, 'fraction_numerical': 0.0008700191404210892, 'mean_word_length': 4.394181135617081, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 16, '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': '4741429', 'n_tokens_mistral': 4183, 'n_tokens_neox': 3423, 'n_words': 597}