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: Validate phenotype filenames #1957

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions bids-validator/src/deps/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export {
parse,
SEPARATOR,
} from 'https://deno.land/[email protected]/path/mod.ts'
export {
globToRegExp,
} from 'https://deno.land/[email protected]/path/glob_to_regexp.ts'
31 changes: 11 additions & 20 deletions bids-validator/src/validators/filenameIdentify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* object in the schema for reference.
*/
// @ts-nocheck
import { SEPARATOR } from '../deps/path.ts'
import { SEPARATOR, globToRegExp } from '../deps/path.ts'
import { GenericSchema, Schema } from '../types/schema.ts'
import { BIDSContext } from '../schema/context.ts'
import { lookupModality } from '../schema/modalities.ts'
Expand Down Expand Up @@ -56,7 +56,7 @@ function findRuleMatches(schema, context) {
export function _findRuleMatches(node, path, context) {
if (
('path' in node && context.file.name.endsWith(node.path)) ||
('stem' in node && context.file.name.startsWith(node.stem)) ||
('stem' in node && context.file.name.match(globToRegExp(node.stem + '*'))) ||
('suffixes' in node && node.suffixes.includes(context.suffix))
) {
context.filenameRules.push(path)
Expand All @@ -74,27 +74,18 @@ export function _findRuleMatches(node, path, context) {

export async function datatypeFromDirectory(schema, context) {
const subEntity = schema.objects.entities.subject.name
const subFormat = schema.objects.formats[subEntity.format]
const sesEntity = schema.objects.entities.session.name
const sesFormat = schema.objects.formats[sesEntity.format]
const parts = context.file.path.split(SEPARATOR)
let datatypeIndex = 2
if (parts[0] !== '') {
// we assume paths have leading '/'
}
// Ignoring associated data for now
const subParts = parts[1].split('-')
if (!(subParts.length === 2 && subParts[0] === subEntity)) {
// first directory must be subject
}
if (parts.length < 3) {
let datatypeIndex = parts.length - 2
if (datatypeIndex < 1) {
return Promise.resolve()
}
const sesParts = parts[2].split('-')
if (sesParts.length === 2 && sesParts[0] === sesEntity) {
datatypeIndex = 3
}
const dirDatatype = parts[datatypeIndex]
if (dirDatatype === 'phenotype') {
// Phenotype is a pseudo-datatype for now.
context.datatype = dirDatatype
return Promise.resolve()
}
for (let key in schema.rules.modalities) {
if (schema.rules.modalities[key].datatypes.includes(dirDatatype)) {
context.modality = key
Expand Down Expand Up @@ -154,9 +145,9 @@ function entitiesExtensionsInRule(
): boolean {
const rule = schema[path]
const fileEntities = Object.keys(context.entities)
const ruleEntities = Object.keys(rule.entities).map((key) =>
const ruleEntities = rule.entities ? Object.keys(rule.entities).map((key) =>
lookupEntityLiteral(key, schema),
)
) : []
const extInRule =
!rule.extensions ||
(rule.extensions && rule.extensions.includes(context.extension))
Expand Down
Loading