@@ -8,6 +8,13 @@ module StudySearchResultsObjects
88 # list of metadata names to include in cohort responses
99 COHORT_METADATA = %w[ disease__ontology_label organ__ontology_label species__ontology_label sex
1010 library_preparation_protocol__ontology_label ] . freeze
11+ # headers for TSV text export of search results
12+ TEXT_HEADERS = [
13+ 'Study source' , 'Accession' , 'Name' , 'Description' , 'Public' , 'Detached' , 'Cell count' , 'Gene count' ,
14+ 'Study URL' , 'Disease' , 'Organ' , 'Species' , 'Sex' , 'Library preparation protocol' , 'Facet matches' ,
15+ 'Term matches'
16+ ] . freeze
17+ COMMA_SPACE = ', ' . freeze
1118
1219 def search_results_obj
1320 response_obj = {
@@ -112,6 +119,79 @@ def cohort_metadata(study)
112119 cohort_entries
113120 end
114121
122+ def results_text_export
123+ lines = [ TEXT_HEADERS . join ( "\t " ) ]
124+ @studies . each { |study | lines << study_text_export ( study ) . join ( "\t " ) }
125+ lines . join ( "\n " )
126+ end
127+
128+ def study_text_export ( study )
129+ if study . is_a? ( Study )
130+ metadata = cohort_metadata ( study ) . with_indifferent_access
131+ term_matches = study . search_weight ( @term_list || [ ] )
132+ facet_data = @studies_by_facet &.[]( study . accession ) || { }
133+ text_to_facet = @metadata_matches &.[]( study . accession ) || { }
134+ facet_matches = Api ::V1 ::StudySearchResultsObjects . merge_facet_matches ( facet_data , text_to_facet )
135+ inferred = @inferred_accessions &.include? ( study . accession )
136+ [
137+ 'SCP' ,
138+ study . accession ,
139+ study . name ,
140+ study . description ,
141+ study . public ,
142+ study . detached ,
143+ study . cell_count ,
144+ study . gene_count ,
145+ result_url_for ( study ) ,
146+ metadata [ :disease ] . join ( COMMA_SPACE ) ,
147+ metadata [ :organ ] . join ( COMMA_SPACE ) ,
148+ metadata [ :species ] . join ( COMMA_SPACE ) ,
149+ metadata [ :sex ] . join ( COMMA_SPACE ) ,
150+ metadata [ :library_preparation_protocol ] . join ( COMMA_SPACE ) ,
151+ Api ::V1 ::StudySearchResultsObjects . facet_results_as_text ( facet_matches ) ,
152+ inferred ? "inferred text match on #{ @inferred_terms . join ( COMMA_SPACE ) } " : term_matches [ :terms ] . keys . join ( COMMA_SPACE )
153+ ]
154+ else
155+ [
156+ study [ :hca_result ] ? 'HCA' : 'TDR' ,
157+ study [ :accession ] ,
158+ study [ :name ] ,
159+ study [ :description ] . gsub ( /\n / , '' ) ,
160+ true ,
161+ false ,
162+ 0 ,
163+ 0 ,
164+ result_url_for ( study ) ,
165+ study . dig ( :metadata , :disease ) . join ( COMMA_SPACE ) ,
166+ study . dig ( :metadata , :organ ) . join ( COMMA_SPACE ) ,
167+ study . dig ( :metadata , :species ) . join ( COMMA_SPACE ) ,
168+ study . dig ( :metadata , :sex ) . join ( COMMA_SPACE ) ,
169+ study . dig ( :metadata , :library_preparation_protocol ) . join ( COMMA_SPACE ) ,
170+ Api ::V1 ::StudySearchResultsObjects . facet_results_as_text ( @studies_by_facet [ study [ :accession ] ] ) ,
171+ nil
172+ ]
173+ end
174+ end
175+
176+ # returns a URL for the study, either SCP or HCA
177+ def result_url_for ( study )
178+ if study . is_a? ( Study )
179+ view_study_url ( accession : study . accession , study_name : study . url_safe_name )
180+ else
181+ "https://data.humancellatlas.org/explore/projects/#{ study [ :hca_project_id ] } "
182+ end
183+ end
184+
185+ # flatten facet matches into a text string for export
186+ def self . facet_results_as_text ( facets )
187+ entries = [ ]
188+ facets . delete ( :facet_search_weight )
189+ facets . each do |facet_name , filters |
190+ entries << "#{ facet_name } :#{ filters . map { |f | f [ :name ] } . join ( '|' ) } "
191+ end
192+ entries . join ( COMMA_SPACE )
193+ end
194+
115195 # merge in multiple facet match data objects into a single merged entity for a given study
116196 def self . merge_facet_matches ( existing_data , new_data )
117197 study_data = existing_data || { }
0 commit comments