-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add self-correlation app and network visualization #1090
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fa85889
add self-correlations plugin
asizemore 8e0e953
succesful network response
asizemore 654d6b7
Merge branch 'main' into feature-1038-network-viz
asizemore 038fc88
Merge branch 'main' into feature-1038-network-viz
asizemore 87471eb
Merge branch 'main' into feature-1038-network-viz
asizemore 6e94ded
cleanup and address funny sizing issues
asizemore 15eb772
restrict selfcorrelation to taxa
asizemore 3056b21
fix empty network alignment
asizemore 8434e2e
cleanup
asizemore e2fad12
Merge branch 'main' into feature-1038-network-viz
asizemore 907f550
Update packages/libs/eda/src/lib/core/components/computations/plugins…
asizemore e82f114
comment, clean types, remove unnecessary memo
asizemore 1b09465
Merge branch 'feature-1038-network-viz' of https://github.com/VEuPath…
asizemore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
340 changes: 340 additions & 0 deletions
340
packages/libs/eda/src/lib/core/components/computations/plugins/selfCorrelation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,340 @@ | ||
import { useMemo } from 'react'; | ||
import { VariableTreeNode, useFindEntityAndVariableCollection } from '../../..'; | ||
import { ComputationConfigProps, ComputationPlugin } from '../Types'; | ||
import { partial } from 'lodash'; | ||
import { | ||
useConfigChangeHandler, | ||
assertComputationWithConfig, | ||
isNotAbsoluteAbundanceVariableCollection, | ||
} from '../Utils'; | ||
import { Computation } from '../../../types/visualization'; | ||
import { ComputationStepContainer } from '../ComputationStepContainer'; | ||
import './Plugins.scss'; | ||
import { makeClassNameHelper } from '@veupathdb/wdk-client/lib/Utils/ComponentUtils'; | ||
import { H6 } from '@veupathdb/coreui'; | ||
import { bipartiteNetworkVisualization } from '../../visualizations/implementations/BipartiteNetworkVisualization'; | ||
import { VariableCollectionSelectList } from '../../variableSelectors/VariableCollectionSingleSelect'; | ||
import SingleSelect, { | ||
ItemGroup, | ||
} from '@veupathdb/coreui/lib/components/inputs/SingleSelect'; | ||
import { | ||
entityTreeToArray, | ||
findEntityAndVariableCollection, | ||
isVariableCollectionDescriptor, | ||
} from '../../../utils/study-metadata'; | ||
import { IsEnabledInPickerParams } from '../../visualizations/VisualizationTypes'; | ||
import { ancestorEntitiesForEntityId } from '../../../utils/data-element-constraints'; | ||
import { NumberInput } from '@veupathdb/components/lib/components/widgets/NumberAndDateInputs'; | ||
import ExpandablePanel from '@veupathdb/coreui/lib/components/containers/ExpandablePanel'; | ||
import { variableCollectionsAreUnique } from '../../../utils/visualization'; | ||
import PluginError from '../../visualizations/PluginError'; | ||
import { | ||
CompleteSelfCorrelationConfig, | ||
SelfCorrelationConfig, | ||
} from '../../../types/apps'; | ||
|
||
const cx = makeClassNameHelper('AppStepConfigurationContainer'); | ||
|
||
/** | ||
* Self-Correlation | ||
* | ||
* The Correlation app takes all collections and visualizes the correlation between a collection and itself. | ||
* For example, if the collection is a set of genes, the app will show the correlation between every pair of genes in the collection. | ||
* | ||
* As of 05/14/24, this app will only be available for mbio assay data. | ||
*/ | ||
|
||
export const plugin: ComputationPlugin = { | ||
configurationComponent: SelfCorrelationConfiguration, | ||
configurationDescriptionComponent: SelfCorrelationConfigDescriptionComponent, | ||
createDefaultConfiguration: () => ({ | ||
prefilterThresholds: { | ||
proportionNonZero: DEFAULT_PROPORTION_NON_ZERO_THRESHOLD, | ||
variance: DEFAULT_VARIANCE_THRESHOLD, | ||
standardDeviation: DEFAULT_STANDARD_DEVIATION_THRESHOLD, | ||
}, | ||
}), | ||
isConfigurationComplete: (configuration) => { | ||
// First, the configuration must be complete | ||
// ANN CLEAN | ||
if (!CompleteSelfCorrelationConfig.is(configuration)) return false; | ||
return true; | ||
}, | ||
visualizationPlugins: { | ||
bipartitenetwork: bipartiteNetworkVisualization.withOptions({ | ||
getLegendTitle(config) { | ||
if (SelfCorrelationConfig.is(config)) { | ||
return ['absolute correlation coefficient', 'correlation direction']; | ||
} else { | ||
return []; | ||
} | ||
}, | ||
// makeGetNodeMenuActions(studyMetadata) { | ||
// const entities = entityTreeToArray(studyMetadata.rootEntity); | ||
// const variables = entities.flatMap((e) => e.variables); | ||
// const collections = entities.flatMap( | ||
// (entity) => entity.collections ?? [] | ||
// ); | ||
// const hostCollection = collections.find( | ||
// (c) => c.id === 'EUPATH_0005050' | ||
// ); | ||
// const parasiteCollection = collections.find( | ||
// (c) => c.id === 'EUPATH_0005051' | ||
// ); | ||
// return function getNodeActions(nodeId: string) { | ||
// const [, variableId] = nodeId.split('.'); | ||
// const variable = variables.find((v) => v.id === variableId); | ||
// if (variable == null) return []; | ||
|
||
// // E.g., "qa." | ||
// const urlPrefix = window.location.host.replace( | ||
// /(plasmodb|hostdb)\.org/, | ||
// '' | ||
// ); | ||
|
||
// const href = parasiteCollection?.memberVariableIds.includes( | ||
// variable.id | ||
// ) | ||
// ? `//${urlPrefix}plasmodb.org/plasmo/app/search/transcript/GenesByRNASeqpfal3D7_Lee_Gambian_ebi_rnaSeq_RSRCWGCNAModules?param.wgcnaParam=${variable.displayName.toLowerCase()}&autoRun=1` | ||
// : hostCollection?.memberVariableIds.includes(variable.id) | ||
// ? `//${urlPrefix}hostdb.org/hostdb/app/search/transcript/GenesByRNASeqhsapREF_Lee_Gambian_ebi_rnaSeq_RSRCWGCNAModules?param.wgcnaParam=${variable.displayName.toLowerCase()}&autoRun=1` | ||
// : undefined; | ||
// if (href == null) return []; | ||
// return [ | ||
// { | ||
// label: 'See list of genes', | ||
// href, | ||
// }, | ||
// ]; | ||
// }; | ||
// }, | ||
// getParitionNames(studyMetadata, config) { | ||
// if (CorrelationConfig.is(config)) { | ||
// const entities = entityTreeToArray(studyMetadata.rootEntity); | ||
// const partition1Name = findEntityAndVariableCollection( | ||
// entities, | ||
// config.data1?.collectionSpec | ||
// )?.variableCollection.displayName; | ||
// const partition2Name = | ||
// config.data2?.dataType === 'collection' | ||
// ? findEntityAndVariableCollection( | ||
// entities, | ||
// config.data2?.collectionSpec | ||
// )?.variableCollection.displayName | ||
// : 'Continuous metadata variables'; | ||
// return { partition1Name, partition2Name }; | ||
// } | ||
// }, | ||
}), // Must match name in data service and in visualization.tsx | ||
}, | ||
isEnabledInPicker: isEnabledInPicker, | ||
studyRequirements: | ||
'These visualizations are only available for studies with compatible metadata.', | ||
}; | ||
|
||
// Renders on the thumbnail page to give a summary of the app instance | ||
function SelfCorrelationConfigDescriptionComponent({ | ||
computation, | ||
}: { | ||
computation: Computation; | ||
}) { | ||
const findEntityAndVariableCollection = useFindEntityAndVariableCollection(); | ||
assertComputationWithConfig(computation, SelfCorrelationConfig); | ||
|
||
const { data1, correlationMethod } = computation.descriptor.configuration; | ||
|
||
const entityAndCollectionVariableTreeNode = | ||
findEntityAndVariableCollection(data1); | ||
|
||
const correlationMethodDisplayName = correlationMethod | ||
? CORRELATION_METHODS.find((method) => method.value === correlationMethod) | ||
?.displayName | ||
: undefined; | ||
|
||
return ( | ||
<div className="ConfigDescriptionContainer"> | ||
<h4> | ||
Data 1:{' '} | ||
<span> | ||
{entityAndCollectionVariableTreeNode ? ( | ||
`${entityAndCollectionVariableTreeNode.entity.displayName} > ${entityAndCollectionVariableTreeNode.variableCollection.displayName}` | ||
) : ( | ||
<i>Not selected</i> | ||
)} | ||
</span> | ||
</h4> | ||
{/* The method should be disabled unti lthe data is chosen */} | ||
<h4> | ||
Method:{' '} | ||
<span> | ||
{correlationMethod ? ( | ||
correlationMethodDisplayName | ||
) : ( | ||
<i>Not selected</i> | ||
)} | ||
</span> | ||
</h4> | ||
</div> | ||
); | ||
} | ||
|
||
const CORRELATION_METHODS = [ | ||
{ value: 'spearman', displayName: 'Spearman' }, | ||
{ value: 'pearson', displayName: 'Pearson' }, | ||
{ value: 'sparcc', displayName: 'SparCC' }, | ||
]; | ||
const DEFAULT_PROPORTION_NON_ZERO_THRESHOLD = 0.05; | ||
const DEFAULT_VARIANCE_THRESHOLD = 0; | ||
const DEFAULT_STANDARD_DEVIATION_THRESHOLD = 0; | ||
|
||
// Shows as Step 1 in the full screen visualization page | ||
export function SelfCorrelationConfiguration(props: ComputationConfigProps) { | ||
const { | ||
computationAppOverview, | ||
computation, | ||
analysisState, | ||
visualizationId, | ||
} = props; | ||
|
||
const configuration = computation.descriptor | ||
.configuration as SelfCorrelationConfig; | ||
|
||
assertComputationWithConfig(computation, SelfCorrelationConfig); | ||
asizemore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const changeConfigHandler = useConfigChangeHandler( | ||
analysisState, | ||
computation, | ||
visualizationId | ||
); | ||
|
||
// Content for the expandable help section | ||
// Note the text is dependent on the context, for example in genomics we'll use different | ||
// language than in mbio. | ||
const helpContent = ( | ||
<div className={cx('-HelpInfoContainer')}> | ||
<H6>What is correlation?</H6> | ||
<p> | ||
The correlation between two variables (genes, sample metadata, etc.) | ||
describes the degree to which their presence in samples co-fluctuate. | ||
For example, the Age and Shoe Size of children are correlated since as a | ||
child ages, their feet grow. | ||
</p> | ||
{/* ANN FILL IN */} | ||
</div> | ||
); | ||
|
||
const correlationMethodSelectorText = useMemo(() => { | ||
if (configuration.correlationMethod) { | ||
return ( | ||
CORRELATION_METHODS.find( | ||
(method) => method.value === configuration.correlationMethod | ||
)?.displayName ?? 'Select a method' | ||
); | ||
} else { | ||
return 'Select a method'; | ||
} | ||
}, [configuration.correlationMethod]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would lose the |
||
|
||
return ( | ||
<ComputationStepContainer | ||
computationStepInfo={{ | ||
stepNumber: 1, | ||
stepTitle: `Configure ${computationAppOverview.displayName}`, | ||
}} | ||
> | ||
<div style={{ display: 'flex', flexDirection: 'column' }}> | ||
<div className={cx()}> | ||
<div className={cx('-CorrelationOuterConfigContainer')}> | ||
<H6>Input Data</H6> | ||
<div className={cx('-InputContainer')}> | ||
<span>Data 1</span> | ||
<VariableCollectionSelectList | ||
value={configuration.data1} | ||
onSelect={partial(changeConfigHandler, 'data1')} | ||
collectionPredicate={isNotAbsoluteAbundanceVariableCollection} | ||
/> | ||
</div> | ||
</div> | ||
<div className={cx('-CorrelationOuterConfigContainer')}> | ||
<H6>Correlation Method</H6> | ||
<div className={cx('-InputContainer')}> | ||
<span>Method</span> | ||
<SingleSelect | ||
value={configuration.correlationMethod ?? 'Select a method'} | ||
buttonDisplayContent={correlationMethodSelectorText} | ||
items={CORRELATION_METHODS.map((method) => ({ | ||
value: method.value, | ||
display: method.displayName, | ||
}))} | ||
onSelect={partial(changeConfigHandler, 'correlationMethod')} | ||
/> | ||
</div> | ||
</div> | ||
<div className={cx('-CorrelationOuterConfigContainer')}> | ||
<H6>Prefilter Data</H6> | ||
<div className={cx('-InputContainer')}> | ||
<span>Prevalence: </span> | ||
<span className={cx('-DescriptionContainer')}> | ||
Keep if abundance is non-zero in at least{' '} | ||
</span> | ||
<NumberInput | ||
minValue={0} | ||
maxValue={100} | ||
step={1} | ||
value={ | ||
// display with % value | ||
configuration.prefilterThresholds?.proportionNonZero != null | ||
? configuration.prefilterThresholds?.proportionNonZero * 100 | ||
: DEFAULT_PROPORTION_NON_ZERO_THRESHOLD * 100 | ||
} | ||
onValueChange={(newValue) => { | ||
changeConfigHandler('prefilterThresholds', { | ||
proportionNonZero: | ||
// save as decimal point, not % | ||
newValue != null | ||
? Number((newValue as number) / 100) | ||
: DEFAULT_PROPORTION_NON_ZERO_THRESHOLD, | ||
variance: | ||
configuration.prefilterThresholds?.variance ?? | ||
DEFAULT_VARIANCE_THRESHOLD, | ||
standardDeviation: | ||
configuration.prefilterThresholds?.standardDeviation ?? | ||
DEFAULT_STANDARD_DEVIATION_THRESHOLD, | ||
}); | ||
}} | ||
containerStyles={{ width: '5.5em' }} | ||
/> | ||
<span className={cx('-DescriptionContainer')}>% of samples</span> | ||
</div> | ||
</div> | ||
</div> | ||
<div> | ||
{/* PluginError here if the method doesn't agree with the data */} | ||
</div> | ||
<ExpandablePanel | ||
title="Learn more about correlation" | ||
subTitle={{}} | ||
children={helpContent} | ||
stylePreset="floating" | ||
themeRole="primary" | ||
styleOverrides={{ container: { marginLeft: 40 } }} | ||
/> | ||
</div> | ||
</ComputationStepContainer> | ||
); | ||
} | ||
|
||
// The self-correlation app is only available for studies that have at least one collection. | ||
function isEnabledInPicker({ | ||
studyMetadata, | ||
}: IsEnabledInPickerParams): boolean { | ||
if (!studyMetadata) return false; | ||
|
||
const entities = entityTreeToArray(studyMetadata.rootEntity); | ||
// Ensure there are collections in this study. Otherwise, disable app | ||
const studyHasCollections = entities.some( | ||
(entity) => !!entity.collections?.length | ||
); | ||
|
||
return studyHasCollections; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason to keep this huge commented region in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in a recent commit!