Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Standardized query result files #392

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions cypress/e2e/ResultsTSV.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Results TSV', () => {
});
});
describe('Unprotected response', () => {
it.only('Checks whether the rows in the participant.tsv file generated according to session_type', () => {
it('Checks whether the rows in the participant.tsv file generated according to session_type', () => {
cy.intercept('query?*', unprotectedResponse).as('call');
cy.intercept(
{
Expand Down Expand Up @@ -107,12 +107,12 @@ describe('Unprotected response', () => {
expect(phenotypicSession.split('\t')[4]).to.equal('ses-nb01');
expect(phenotypicSession.split('\t')[5]).to.equal('');
expect(phenotypicSession.split('\t')[6]).to.equal('Phenotypic');
expect(phenotypicSession.split('\t')[7]).to.equal('1');
expect(phenotypicSession.split('\t')[8]).to.equal('0');
expect(phenotypicSession.split('\t')[9]).to.equal('10.4');
expect(phenotypicSession.split('\t')[10]).to.equal('female');
expect(phenotypicSession.split('\t')[11]).to.equal('Major depressive disorder');
expect(phenotypicSession.split('\t')[12]).to.equal('multisource interference task');
expect(phenotypicSession.split('\t')[7]).to.equal('10.4');
expect(phenotypicSession.split('\t')[8]).to.equal('female');
expect(phenotypicSession.split('\t')[9]).to.equal('Major depressive disorder');
expect(phenotypicSession.split('\t')[10]).to.equal('multisource interference task');
expect(phenotypicSession.split('\t')[11]).to.equal('1');
expect(phenotypicSession.split('\t')[12]).to.equal('0');
expect(phenotypicSession.split('\t')[13]).to.equal('');
expect(phenotypicSession.split('\t')[14]).to.equal('');
expect(phenotypicSession.split('\t')[15]).to.equal('Flow Weighted, T2 Weighted');
Expand All @@ -127,12 +127,12 @@ describe('Unprotected response', () => {
expect(imagingSession.split('\t')[4]).to.equal('ses-nb01');
expect(imagingSession.split('\t')[5]).to.equal('/ds004116/sub-300101');
expect(imagingSession.split('\t')[6]).to.equal('Imaging');
expect(imagingSession.split('\t')[7]).to.equal('0');
expect(imagingSession.split('\t')[8]).to.equal('1');
expect(imagingSession.split('\t')[7]).to.equal('');
expect(imagingSession.split('\t')[8]).to.equal('');
expect(imagingSession.split('\t')[9]).to.equal('');
expect(imagingSession.split('\t')[10]).to.equal('');
expect(imagingSession.split('\t')[11]).to.equal('');
expect(imagingSession.split('\t')[12]).to.equal('');
expect(imagingSession.split('\t')[11]).to.equal('0');
expect(imagingSession.split('\t')[12]).to.equal('1');
expect(imagingSession.split('\t')[13]).to.equal('Flow Weighted, T2 Weighted');
expect(imagingSession.split('\t')[14]).to.equal('fmriprep 23.1.3, freesurfer 7.3.2');
expect(imagingSession.split('\t')[15]).to.equal('Flow Weighted, T2 Weighted');
Expand Down
220 changes: 131 additions & 89 deletions src/components/ResultContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,104 +145,146 @@ function ResultContainer({
const tsvRows = [];
const datasets = response.responses.filter((res) => download.includes(res.dataset_uuid));

const isHumanFile = buttonIdentifier === 'cohort-participant';
if (buttonIdentifier === 'cohort-participant') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the export logic into a reusable configuration-driven function.

The export logic can be simplified by extracting the common pattern into a reusable function. Here's how:

interface FieldConfig {
  header: string;
  getValue: (res: any, subject?: any) => string;
  protected?: string;
}

const exportConfigs = {
  'cohort-participant': {
    fields: [
      { header: 'DatasetName', getValue: (res) => res.dataset_name.replace('\n', ' ') },
      { header: 'SubjectID', getValue: (_, subject) => subject?.sub_id, protected: 'protected' },
      // ... other fields
    ]
  },
  'other-export': {
    fields: [
      // ... field configurations
    ]
  }
};

function generateTSVString(buttonIdentifier: string) {
  if (!response) return '';

  const config = exportConfigs[buttonIdentifier];
  const datasets = response.responses.filter((res) => download.includes(res.dataset_uuid));
  const tsvRows = [config.fields.map(f => f.header).join('\t')];

  datasets.forEach(res => {
    if (res.records_protected) {
      tsvRows.push(config.fields.map(f => f.protected || 'protected').join('\t'));
    } else {
      res.subject_data.forEach(subject => {
        tsvRows.push(config.fields.map(f => f.getValue(res, subject)).join('\t'));
      });
    }
  });

  return tsvRows.join('\n');
}

This approach:

  • Eliminates code duplication
  • Makes field definitions and differences between export types explicit
  • Easier to maintain and extend with new export types
  • Reduces chance of bugs when making changes

const headers = [
'DatasetName',
'PortalURI',
'NumMatchingSubjects',
'SubjectID',
'SessionID',
'SessionFilePath',
'SessionType',
'Age',
'Sex',
'Diagnosis',
'Assessment',
'NumMatchingPhenotypicSessions',
'NumMatchingImagingSessions',
'SessionImagingModalities',
'SessionCompletedPipelines',
'DatasetImagingModalities',
'DatasetPipelines',
].join('\t');
tsvRows.push(headers);

const headers = [
'DatasetName',
'PortalURI',
'NumMatchingSubjects',
'SubjectID',
'SessionID',
'ImagingSessionPath',
'SessionType',
'NumMatchingPhenotypicSessions',
'NumMatchingImagingSessions',
'Age',
'Sex',
'Diagnosis',
'Assessment',
'SessionImagingModalities',
'SessionCompletedPipelines',
'DatasetImagingModalities',
'DatasetPipelines',
].join('\t');
tsvRows.push(headers);

datasets.forEach((res) => {
if (res.records_protected) {
tsvRows.push(
[
res.dataset_name.replace('\n', ' '),
res.dataset_portal_uri,
res.num_matching_subjects,
'protected', // subject_id
'protected', // session_id
'protected', // session_file_path
'protected', // session_type
'protected', // num_matching_phenotypic_sessions
'protected', // num_matching_imaging_sessions
'protected', // age
'protected', // sex
'protected', // diagnosis
'protected', // assessment
'protected', // session_imaging_modality
'protected', // session_completed_pipelines
isHumanFile
? convertURIToLabel('modality', res.image_modals)
: res.image_modals?.join(', '),
isHumanFile
? convertURIToLabel(
datasets.forEach((res) => {
if (res.records_protected) {
tsvRows.push(
[
res.dataset_name.replace('\n', ' '),
res.dataset_portal_uri,
res.num_matching_subjects,
'protected', // subject_id
'protected', // session_id
'protected', // session_file_path
'protected', // session_type
'protected', // age
'protected', // sex
'protected', // diagnosis
'protected', // assessment
'protected', // num_matching_phenotypic_sessions
'protected', // num_matching_imaging_sessions
'protected', // session_imaging_modality
'protected', // session_completed_pipelines
convertURIToLabel('modality', res.image_modals),
convertURIToLabel(
'pipeline',
parsePipelinesInfoToString(res.available_pipelines).split(', ')
),
].join('\t')
);
} else {
// @ts-expect-error: typescript doesn't know that subject_data is an array when records_protected is false.
res.subject_data.forEach((subject) => {
tsvRows.push(
[
res.dataset_name.replace('\n', ' '),
res.dataset_portal_uri,
res.num_matching_subjects,
subject.sub_id,
subject.session_id,
subject.session_file_path,
convertURIToLabel('sessionType', subject.session_type),
subject.age,
convertURIToLabel('sex', subject.sex),
convertURIToLabel('diagnosis', subject.diagnosis),
convertURIToLabel('assessment', subject.assessment),
subject.num_matching_phenotypic_sessions,
subject.num_matching_imaging_sessions,
convertURIToLabel('modality', subject.image_modal),
convertURIToLabel(
'pipeline',
parsePipelinesInfoToString(subject.completed_pipelines).split(', ')
),
convertURIToLabel('modality', res.image_modals),
convertURIToLabel(
'pipeline',
parsePipelinesInfoToString(res.available_pipelines).split(', ')
)
: parsePipelinesInfoToString(res.available_pipelines),
].join('\t')
);
} else {
// @ts-expect-error: typescript doesn't know that subject_data is an array when records_protected is false.
res.subject_data.forEach((subject) => {
),
].join('\t')
);
});
}
});
} else {
const headers = [
'DatasetName',
'PortalURI',
'SubjectID',
'SessionID',
'SessionFilePath',
'SessionType',
'NumMatchingPhenotypicSessions',
'NumMatchingImagingSessions',
'SessionImagingModalities',
'SessionCompletedPipelines',
'DatasetImagingModalities',
'DatasetPipelines',
].join('\t');
tsvRows.push(headers);

datasets.forEach((res) => {
if (res.records_protected) {
tsvRows.push(
[
res.dataset_name.replace('\n', ' '),
res.dataset_portal_uri,
res.num_matching_subjects,
subject.sub_id,
subject.session_id,
subject.session_file_path,
isHumanFile
? convertURIToLabel('sessionType', subject.session_type)
: subject.session_type,
subject.num_matching_phenotypic_sessions,
subject.num_matching_imaging_sessions,
subject.age,
isHumanFile ? convertURIToLabel('sex', subject.sex) : subject.sex,
isHumanFile ? convertURIToLabel('diagnosis', subject.diagnosis) : subject.diagnosis,
isHumanFile
? convertURIToLabel('assessment', subject.assessment)
: subject.assessment,
isHumanFile
? convertURIToLabel('modality', subject.image_modal)
: subject.image_modal?.join(', '),
isHumanFile
? convertURIToLabel(
'pipeline',
parsePipelinesInfoToString(subject.completed_pipelines).split(', ')
)
: parsePipelinesInfoToString(subject.completed_pipelines),
isHumanFile
? convertURIToLabel('modality', res.image_modals)
: res.image_modals?.join(', '),
isHumanFile
? convertURIToLabel(
'pipeline',
parsePipelinesInfoToString(res.available_pipelines).split(', ')
)
: parsePipelinesInfoToString(res.available_pipelines),
'protected', // subject_id
'protected', // session_id
'protected', // session_file_path
'protected', // session_type
'protected', // num_matching_phenotypic_sessions
'protected', // num_matching_imaging_sessions
'protected', // session_imaging_modality
'protected', // session_completed_pipelines
res.image_modals?.join(', '),
parsePipelinesInfoToString(res.available_pipelines),
].join('\t')
);
});
}
});
} else {
// @ts-expect-error: typescript doesn't know that subject_data is an array when records_protected is false.
res.subject_data.forEach((subject) => {
tsvRows.push(
[
res.dataset_name.replace('\n', ' '),
res.dataset_portal_uri,
subject.sub_id,
subject.session_id,
subject.session_file_path,
subject.session_type,
subject.num_matching_phenotypic_sessions,
subject.num_matching_imaging_sessions,
subject.image_modal?.join(', '),
parsePipelinesInfoToString(subject.completed_pipelines),
res.image_modals?.join(', '),
parsePipelinesInfoToString(res.available_pipelines),
].join('\t')
);
});
}
});
}

return tsvRows.join('\n');
}

Expand Down
Loading