function selection_clauses=query_generator(query_style,filename_fragment_or_cellarray_of_filenames,target_file_suffix)
%description: creates queries to pass to the sql database.  query creation takes several styles, as set in analyze_across_expts.
%called by: analyze_across_expts, analyze_across_sd_stdp_expts(deprecated)
%calls: [no custom functions]
%notes: checked may 19, 2007, changed aug 6, 2007 to include stable_minutes_from_relevant_induction as one of the fields retrieved from patchlog_epochs_table when query_style==3
%code confirmed date:

if query_style== 1
    desired_characterization_epoch_info='SELECT patchlogfiles_this_epoch,trials_to_ignore,has_baseline_and_postinduction.suffix,event_detection_threshold,minutes_per_trial,APs_per_trial,color_for_plotting FROM (';
	desired_induction_epoch_info='SELECT patchlogfiles_this_epoch,conversion_to_mV_or_pA_postsyn_channel,induction_interval_prepost_insamples,color_for_plotting,has_baseline_and_postinduction.suffix FROM (';
	
    first_epochal_check='SELECT first_patchlogfile_this_expt,suffix FROM patchlog_epochs_table WHERE epoch_class_and_sequence = ''C1'' AND next_epoch_class_and_sequence = ''I1''';
	second_epochal_check='SELECT first_patchlogfile_this_expt,suffix FROM patchlog_epochs_table WHERE epoch_class_and_sequence = ''I1'' AND induction_stimulus = ''RO'' AND next_epoch_class_and_sequence = ''C2''';
	third_epochal_check='SELECT first_patchlogfile_this_expt,suffix FROM patchlog_epochs_table WHERE epoch_class_and_sequence = ''C2''';
	charac_epochs_check=strcat('SELECT first_epochal_check.first_patchlogfile_this_expt,first_epochal_check.suffix FROM (', first_epochal_check, ') AS first_epochal_check INNER JOIN (', third_epochal_check,') AS third_epochal_check ON first_epochal_check.first_patchlogfile_this_expt=third_epochal_check.first_patchlogfile_this_expt AND first_epochal_check.suffix=third_epochal_check.suffix');
	charac_and_induc_epochs_check=strcat('SELECT charac_epochs_check.first_patchlogfile_this_expt,charac_epochs_check.suffix FROM (', charac_epochs_check, ') AS charac_epochs_check INNER JOIN (', second_epochal_check, ') AS second_epochal_check ON charac_epochs_check.first_patchlogfile_this_expt = second_epochal_check.first_patchlogfile_this_expt AND charac_epochs_check.suffix=second_epochal_check.suffix');
	joint_exptal_epochal_check=strcat('SELECT charac_and_induc_epochs_check.first_patchlogfile_this_expt,charac_and_induc_epochs_check.suffix,patchlog_expts_table.color_for_plotting FROM (',charac_and_induc_epochs_check,') AS charac_and_induc_epochs_check INNER JOIN patchlog_expts_table ON patchlog_expts_table.first_patchlogfile_this_expt = charac_and_induc_epochs_check.first_patchlogfile_this_expt WHERE patchlog_expts_table.expt_main_stimulus_class = ''stdp'' and patchlog_expts_table.expt_main_configuration_class = ''microperfusion''');
	
	baseline_select_clause=') AS has_baseline_and_postinduction INNER JOIN patchlog_epochs_table ON has_baseline_and_postinduction.first_patchlogfile_this_expt = patchlog_epochs_table.first_patchlogfile_this_expt AND has_baseline_and_postinduction.suffix = patchlog_epochs_table.suffix WHERE patchlog_epochs_table.epoch_class_and_sequence = ''C1'' ORDER BY patchlogfiles_this_epoch ASC';
	induction_select_clause=') AS has_baseline_and_postinduction INNER JOIN patchlog_epochs_table ON has_baseline_and_postinduction.first_patchlogfile_this_expt = patchlog_epochs_table.first_patchlogfile_this_expt AND has_baseline_and_postinduction.suffix = patchlog_epochs_table.suffix WHERE patchlog_epochs_table.epoch_class_and_sequence = ''I1'' ORDER BY patchlogfiles_this_epoch ASC';
	postinduction_select_clause=') AS has_baseline_and_postinduction INNER JOIN patchlog_epochs_table ON has_baseline_and_postinduction.first_patchlogfile_this_expt = patchlog_epochs_table.first_patchlogfile_this_expt AND has_baseline_and_postinduction.suffix = patchlog_epochs_table.suffix WHERE patchlog_epochs_table.epoch_class_and_sequence = ''C2'' ORDER BY patchlogfiles_this_epoch ASC';
	
	selection_clauses{1}={%selecting epoch C1, 'characterization1', aka baseline
        strcat(desired_characterization_epoch_info,joint_exptal_epochal_check,baseline_select_clause)
	};
	selection_clauses{2}={%selecting epoch I1, 'induction1', aka first induction
        strcat(desired_induction_epoch_info,joint_exptal_epochal_check,induction_select_clause)
	};
	selection_clauses{3}={%selecting epoch C2, 'characterization2', aka the first postinduction period
        strcat(desired_characterization_epoch_info,joint_exptal_epochal_check,postinduction_select_clause)
	};
elseif query_style==2   %this style expects to be told some distinct portion of the name of the preinduction filename (the filename_fragment_or_cellarray_of_filenames);  it retrieves info for that preinduction file and for the related induction and postinduction files
    desired_characterization_epoch_info='SELECT patchlogfiles_this_epoch,trials_to_ignore,suffix,event_detection_threshold,minutes_per_trial,APs_per_trial,color_for_plotting FROM ';
    desired_induction_epoch_info='SELECT patchlogfiles_this_epoch,conversion_to_mV_or_pA_postsyn_channel,induction_interval_prepost_insamples,color_for_plotting,suffix FROM ';
    
    prelim_query{1}={strcat('SELECT first_patchlogfile_this_expt,patchlogfiles_this_epoch,epoch_class_and_sequence,next_epoch_class_and_sequence, next_C_epoch_if_C_epoch_and_epochs_can_be_compared FROM patchlog_epochs_table WHERE patchlogfiles_this_epoch LIKE ''%', filename_fragment_or_cellarray_of_filenames, '%'' AND suffix = ''',target_file_suffix,''' AND epoch_free_of_significant_technical_problems =1 AND next_C_epoch_if_C_epoch_and_epochs_can_be_compared IS NOT NULL')};
    prelim_files=find_flagged_filenames_from_access(prelim_query{1})
    if size(prelim_files,1)>1,error('The filename_fragment_or_cellarray_of_filenames/suffix combination is found in more than one preinduction file'), end
    
   selection_clauses{1}={%selecting epoch C1, 'characterization1', aka baseline
        strcat(desired_characterization_epoch_info,' patchlog_epochs_table INNER JOIN patchlog_expts_table ON patchlog_epochs_table.first_patchlogfile_this_expt = patchlog_expts_table.first_patchlogfile_this_expt WHERE patchlog_epochs_table.first_patchlogfile_this_expt =''',prelim_files{1},'''AND patchlog_epochs_table.epoch_class_and_sequence = ''',prelim_files{3},''' AND patchlog_epochs_table.suffix = ''',target_file_suffix ,'''')
	};
	selection_clauses{2}={%selecting epoch I1, 'induction1', aka first induction
        strcat(desired_induction_epoch_info,' patchlog_epochs_table INNER JOIN patchlog_expts_table ON patchlog_epochs_table.first_patchlogfile_this_expt = patchlog_expts_table.first_patchlogfile_this_expt WHERE patchlog_epochs_table.first_patchlogfile_this_expt =''',prelim_files{1},'''AND patchlog_epochs_table.epoch_class_and_sequence = ''',prelim_files{4},''' AND patchlog_epochs_table.suffix = ''',target_file_suffix ,'''')
	};
	selection_clauses{3}={%selecting epoch C2, 'characterization2', aka the first postinduction period
        strcat(desired_characterization_epoch_info,' patchlog_epochs_table INNER JOIN patchlog_expts_table ON patchlog_epochs_table.first_patchlogfile_this_expt = patchlog_expts_table.first_patchlogfile_this_expt WHERE patchlog_epochs_table.first_patchlogfile_this_expt =''',prelim_files{1},'''AND patchlog_epochs_table.epoch_class_and_sequence = ''',prelim_files{5},''' AND patchlog_epochs_table.suffix = ''',target_file_suffix ,'''')
	};
elseif query_style==3
    from_expts_table='SELECT first_patchlogfile_this_expt, color_for_plotting, num_samples_per_event, values_to_average_for_local_correction FROM patchlog_expts_table WHERE expt_main_stimulus_class =''stdp'' AND expt_main_configuration_class = ''microperfusion'' AND expt_free_of_significant_technical_problems = 1 AND induction_can_be_measured = 1';
    from_epochs_table='SELECT first_patchlogfile_this_expt,patchlogfiles_this_epoch, epoch_class_and_sequence, trials_to_ignore,suffix, next_epoch_class_and_sequence, next_C_epoch_if_C_epoch_and_epochs_can_be_compared, event_detection_threshold,minutes_per_trial,APs_per_trial,conversion_to_mV_or_pA_postsyn_channel,induction_interval_prepost_insamples,stable_minutes_from_relevant_induction FROM patchlog_epochs_table WHERE epoch_free_of_significant_technical_problems =1 AND next_C_epoch_if_C_epoch_and_epochs_can_be_compared IS NOT NULL';
    lotsa_info_table=strcat('SELECT from_epochs_table.first_patchlogfile_this_expt,color_for_plotting, num_samples_per_event, values_to_average_for_local_correction, patchlogfiles_this_epoch, epoch_class_and_sequence, trials_to_ignore,suffix, next_epoch_class_and_sequence, next_C_epoch_if_C_epoch_and_epochs_can_be_compared, event_detection_threshold,minutes_per_trial,APs_per_trial,conversion_to_mV_or_pA_postsyn_channel,induction_interval_prepost_insamples,stable_minutes_from_relevant_induction FROM (',from_epochs_table,') AS from_epochs_table INNER JOIN (',from_expts_table,')AS from_expts_table ON  from_epochs_table.first_patchlogfile_this_expt = from_expts_table.first_patchlogfile_this_expt');
    selected_preinduction_table=strcat('SELECT patchlogfiles_this_epoch,trials_to_ignore,suffix,event_detection_threshold,minutes_per_trial,APs_per_trial,color_for_plotting,stable_minutes_from_relevant_induction,num_samples_per_event, values_to_average_for_local_correction FROM (',lotsa_info_table,') AS lotsa_info_table ORDER BY patchlogfiles_this_epoch');  %note, oct 26 2008:  the 'AS lotsa_info_table' on this line would seem useless, but it appears to be necessary on the macintosh
    selected_induction_table=strcat('SELECT patchlog_epochs_table.patchlogfiles_this_epoch,patchlog_epochs_table.conversion_to_mV_or_pA_postsyn_channel,patchlog_epochs_table.induction_interval_prepost_insamples,lotsa_info_table.color_for_plotting,patchlog_epochs_table.suffix FROM patchlog_epochs_table INNER JOIN (',lotsa_info_table, ') AS lotsa_info_table ON patchlog_epochs_table.first_patchlogfile_this_expt =lotsa_info_table.first_patchlogfile_this_expt AND patchlog_epochs_table.suffix =lotsa_info_table.suffix AND patchlog_epochs_table.epoch_class_and_sequence =lotsa_info_table.next_epoch_class_and_sequence ORDER BY patchlog_epochs_table.patchlogfiles_this_epoch');
    %selected_induction_table=strcat('SELECT patchlog_epochs_table.patchlogfiles_this_epoch,patchlog_epochs_table.induction_stimulus FROM patchlog_epochs_table INNER JOIN (',lotsa_info_table, ') AS lotsa_info_table ON patchlog_epochs_table.first_patchlogfile_this_expt =lotsa_info_table.first_patchlogfile_this_expt AND patchlog_epochs_table.suffix =lotsa_info_table.suffix AND patchlog_epochs_table.epoch_class_and_sequence =lotsa_info_table.next_epoch_class_and_sequence ORDER BY patchlog_epochs_table.patchlogfiles_this_epoch');
    selected_postinduction_table=strcat('SELECT patchlog_epochs_table.patchlogfiles_this_epoch,patchlog_epochs_table.trials_to_ignore,patchlog_epochs_table.suffix,patchlog_epochs_table.event_detection_threshold,patchlog_epochs_table.minutes_per_trial,patchlog_epochs_table.APs_per_trial,lotsa_info_table.color_for_plotting,patchlog_epochs_table.stable_minutes_from_relevant_induction, num_samples_per_event, values_to_average_for_local_correction FROM patchlog_epochs_table INNER JOIN (',lotsa_info_table, ') AS lotsa_info_table ON patchlog_epochs_table.first_patchlogfile_this_expt =lotsa_info_table.first_patchlogfile_this_expt AND patchlog_epochs_table.suffix =lotsa_info_table.suffix AND patchlog_epochs_table.epoch_class_and_sequence =lotsa_info_table.next_C_epoch_if_C_epoch_and_epochs_can_be_compared ORDER BY patchlog_epochs_table.patchlogfiles_this_epoch');
    selection_clauses{1}={%selecting epoch C1, 'characterization1', aka baseline
        selected_preinduction_table
	};
	selection_clauses{2}={%selecting epoch I1, 'induction1', aka first induction
        selected_induction_table
	};
	selection_clauses{3}={%selecting epoch C2, 'characterization2', aka the first postinduction period
        selected_postinduction_table
	}; 
elseif query_style==4   %get one or more filenames as input (from the command line through analyze_across_expts) and look up these files and their related inducs and postinducs -- the filenames should be of preinduc epochs
    nomen=strcat(' patchlogfiles_this_epoch LIKE ''%',filename_fragment_or_cellarray_of_filenames{1},'%''');
	for patchlogfile=2:length(filename_fragment_or_cellarray_of_filenames)
        nomen=strcat(nomen,' OR patchlogfiles_this_epoch LIKE ''%',filename_fragment_or_cellarray_of_filenames{patchlogfile},'%''');
    end
    from_epochs_table=strcat('SELECT first_patchlogfile_this_expt,patchlogfiles_this_epoch,trials_to_ignore,suffix,event_detection_threshold,minutes_per_trial,APs_per_trial,stable_minutes_from_relevant_induction FROM patchlog_epochs_table WHERE (',nomen,') AND suffix=''[]''');     %trials_to_ignore=''[1:43]'' OR trials_to_ignore=''[1:28]'' OR trials_to_ignore=''[66:82]'' OR
    from_expts_table=strcat('SELECT first_patchlogfile_this_expt,color_for_plotting,num_samples_per_event,values_to_average_for_local_correction FROM patchlog_expts_table');
    lotsa_info_table=strcat('SELECT color_for_plotting,num_samples_per_event,values_to_average_for_local_correction,patchlogfiles_this_epoch,trials_to_ignore,suffix,event_detection_threshold,minutes_per_trial,APs_per_trial,stable_minutes_from_relevant_induction FROM (',from_epochs_table,') AS from_epochs_table INNER JOIN (',from_expts_table,')AS from_expts_table ON from_epochs_table.first_patchlogfile_this_expt = from_expts_table.first_patchlogfile_this_expt');
    selected_preinduction_table=strcat('SELECT patchlogfiles_this_epoch,trials_to_ignore,suffix,event_detection_threshold,minutes_per_trial,APs_per_trial,color_for_plotting,stable_minutes_from_relevant_induction,num_samples_per_event,values_to_average_for_local_correction FROM (',lotsa_info_table,') AS lotsa_info_table ORDER BY patchlogfiles_this_epoch');
    selected_induction_table=strcat('SELECT patchlog_epochs_table.patchlogfiles_this_epoch,patchlog_epochs_table.conversion_to_mV_or_pA_postsyn_channel,patchlog_epochs_table.induction_interval_prepost_insamples,lotsa_info_table.color_for_plotting,patchlog_epochs_table.suffix FROM patchlog_epochs_table INNER JOIN (',lotsa_info_table,') AS lotsa_info_table ON patchlog_epochs_table.first_patchlogfile_this_expt=lotsa_info_table.first_patchlogfile_this_expt AND patchlog_epochs_table.suffix =lotsa_info_table.suffix AND patchlog_epochs_table.epoch_class_and_sequence =lotsa_info_table.next_epoch_class_and_sequence ORDER BY patchlog_epochs_table.patchlogfiles_this_epoch');
    selected_postinduction_table=strcat('SELECT patchlog_epochs_table.patchlogfiles_this_epoch,patchlog_epochs_table.trials_to_ignore,patchlog_epochs_table.suffix,patchlog_epochs_table.event_detection_threshold,patchlog_epochs_table.minutes_per_trial,patchlog_epochs_table.APs_per_trial,lotsa_info_table.color_for_plotting,patchlog_epochs_table.stable_minutes_from_relevant_induction, num_samples_per_event, values_to_average_for_local_correction FROM patchlog_epochs_table INNER JOIN (',lotsa_info_table, ') AS lotsa_info_table ON patchlog_epochs_table.first_patchlogfile_this_expt =lotsa_info_table.first_patchlogfile_this_expt AND patchlog_epochs_table.suffix =lotsa_info_table.suffix AND patchlog_epochs_table.epoch_class_and_sequence =lotsa_info_table.next_C_epoch_if_C_epoch_and_epochs_can_be_compared ORDER BY patchlog_epochs_table.patchlogfiles_this_epoch');
    selection_clauses{1}={%selecting epoch C1, 'characterization1', aka baseline
        selected_preinduction_table
	};
    selection_clauses{2}={%selecting epoch I1, 'induction1', aka first induction
        selected_induction_table
	};
	selection_clauses{3}={%selecting epoch C2, 'characterization2', aka the first postinduction period
        selected_postinduction_table
	}; 
end

%---------
%nested_in_clause='_table WHERE is_this_a_microperfusion_expt=yes AND patchlogfiles_next_epoch in (SELECT patchlogfiles_this_epoch FROM patchlogs_table WHERE patchlogfiles_next_epoch in (SELECT patchlogfiles_this_epoch FROM patchlogs_table WHERE is_this_a_microperfusion_expt=yes))'% AND is_there_a_contiguous_preceding_epoch=yes' %this is from the time when the database only had one table, epochs, and epochs within an experiment were only linked through the 'next_epoch' and previous_epoch' fields
%baseline_and_postinduc_check_clause='SELECT patchlog_expts_table.first_patchlogfile_this_expt FROM (SELECT patchlog_expts_table.first_patchlogfile_this_expt FROM patchlog_epochs_table INNER JOIN patchlog_expts_table ON patchlog_epochs_table.first_patchlogfile_this_expt = patchlog_expts_table.first_patchlogfile_this_expt WHERE patchlog_epochs_table.epoch_class_and_sequence = ''C1'') AS has_baseline INNER JOIN patchlog_epochs_table ON has_baseline.first_patchlogfile_this_expt = patchlog_epochs_table.first_patchlogfile_this_expt WHERE patchlog_epochs_table.epoch_class_and_sequence = ''C2''';
%a memento of nested clauses: strcat('SELECT patchlogfiles_this_epoch, trials_to_ignore, suffix_for_extracted_values_filename FROM patchlogs_table WHERE patchlogfiles_this_epoch in (SELECT patchlogfiles_next_epoch FROM patchlogs_table WHERE patchlogfiles_this_epoch in (SELECT patchlogfiles_next_epoch FROM patchlogs',whereclause,'))')