1+ const PARTICIPANT_ID = 'participantId'
2+ const AGE = 'age'
13/**
2- * Go from tsv format string with participant_id as a required header to object of form
3- * {
4- * participant_id_1: {
4+ * Go from tsv format string with participant_id as a required header to array of form
5+ * [
6+ * {
7+ * participantId: 'participant_id_1'
58 * foo: 'x',
69 * ...
710 * },
8- * participant_id_2: {
11+ * {
12+ * participantId: 'participant_id_2'
913 * foo: 'y',
1014 * ...
1115 * }
1216 * ...
13- * }
17+ * ]
1418 *
1519 * returns null if participant_id is not a header or file contents do not exist
1620 * @param {string } participantsTsvContent
@@ -21,29 +25,44 @@ const collectSubjectMetadata = participantsTsvContent => {
2125 . split ( '\n' )
2226 . filter ( row => row !== '' )
2327 . map ( row => row . split ( '\t' ) )
24- const [ headers , ...subjectData ] = contentTable
25- const participant_idIndex = headers . findIndex (
26- header => header === 'participant_id' ,
28+ const [ snakeCaseHeaders , ...subjectData ] = contentTable
29+ const headers = snakeCaseHeaders . map ( header =>
30+ header === 'participant_id' ? PARTICIPANT_ID : header ,
2731 )
28- if ( participant_idIndex === - 1 ) return null
32+ const targetKeys = [ PARTICIPANT_ID , 'age' , 'sex' , 'group' ]
33+ . map ( key => ( {
34+ key,
35+ index : headers . findIndex ( targetKey => targetKey === key ) ,
36+ } ) )
37+ . filter ( ( { index } ) => index !== - 1 )
38+ const participantIdKey = targetKeys . find (
39+ ( { key } ) => key === PARTICIPANT_ID ,
40+ )
41+ const ageKey = targetKeys . find ( ( { key } ) => key === AGE )
42+ if ( participantIdKey === undefined ) return null
2943 else
30- return subjectData . reduce (
31- ( subjectMetadata , data ) => ( {
32- ...subjectMetadata ,
33- [ data [ participant_idIndex ] . replace ( / ^ s u b - / , '' ) ] : data . reduce (
34- ( subjectMetadata , datum , i ) =>
35- i === participant_idIndex
36- ? subjectMetadata
37- : {
38- ...subjectMetadata ,
39- [ headers [ i ] ] :
40- headers [ i ] === 'age' ? parseInt ( datum ) : datum ,
41- } ,
44+ return subjectData
45+ . map ( data => {
46+ // this first map is for transforming any data coming out of participants.tsv:
47+ // strip subject ids to match metadata.subjects: 'sub-01' -> '01'
48+ data [ participantIdKey . index ] = data [ participantIdKey . index ] . replace (
49+ / ^ s u b - / ,
50+ '' ,
51+ )
52+ // make age an integer
53+ if ( ageKey ) data [ ageKey . index ] = parseInt ( data [ ageKey . index ] )
54+ return data
55+ } )
56+ . map ( data =>
57+ //extract all target metadata for each subject
58+ targetKeys . reduce (
59+ ( subject , { key, index } ) => ( {
60+ ...subject ,
61+ [ key ] : data [ index ] ,
62+ } ) ,
4263 { } ,
4364 ) ,
44- } ) ,
45- { } ,
46- )
65+ )
4766 }
4867}
4968
0 commit comments