-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract-collaborations.js
More file actions
305 lines (257 loc) · 10.1 KB
/
Copy pathextract-collaborations.js
File metadata and controls
305 lines (257 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env node
/**
* Extract author and collaboration data from publication QMD files
* This script scans publication files and generates data for the collaborators page
*/
const fs = require('fs');
const path = require('path');
// Known lab members and alumni for filtering
const LAB_MEMBERS = [
"Steven Roberts", "Steven B. Roberts", "Steven B Roberts",
"Mackenzie Gavery", "Mackenzie R. Gavery", "Mackenzie R Gavery",
"Shelly Wanamaker", "Shelly A. Wanamaker", "Shelly A Wanamaker",
"Emma Timmins-Schiffman", "Emma B. Timmins-Schiffman", "Emma B Timmins-Schiffman",
"Yaamini Venkataraman", "Yaamini R. Venkataraman", "Yaamini R Venkataraman",
"Laura Spencer", "Laura H. Spencer", "Laura H Spencer",
"Grace Crandall", "Grace A. Crandall", "Grace A Crandall",
"Matt George", "Matthew George", "Matthew N. George", "Matthew N George",
"Zach Bengtsson", "Zachary Bengtsson",
"Chris Mantegna", "Christopher Mantegna",
"Celeste Valdivia",
"Hollie Putnam", "Hollie M. Putnam", "Hollie M Putnam",
"Brent Vadopalas",
"Sam White", "Samuel White", "Samuel J. White", "Samuel J White",
"Jay Dimond", "James Dimond", "James L. Dimond", "James L Dimond",
"Aspen Coyle",
"Olivia Cattau",
"Jake Heare",
"Andy Jasonowicz", "Andrew Jasonowicz",
"Claire Olson", "Claire E. Olson", "Claire E Olson",
"Doug Immerman",
"Caroline Storer",
"Dave Metzger", "David Metzger", "David C. Metzger", "David C Metzger",
"Giles Goetz", "Frederick Goetz", "Frederick W. Goetz", "Frederick W Goetz",
"Delaney Lawson",
"Kaitlyn Mitchell", "Kaitlyn R. Mitchell", "Kaitlyn R Mitchell",
"Crystal Simchick",
"Megan Hintz",
"Rhonda Elliott", "Rhonda Elliott Thompson",
"Benoit Eudeline",
"Larken Root"
];
function normalizeAuthorName(name) {
return name.replace(/[.,]/g, '').trim().toLowerCase();
}
function isLabMember(authorName) {
const normalized = normalizeAuthorName(authorName);
// Direct match
if (LAB_MEMBERS.some(labMember => normalizeAuthorName(labMember) === normalized)) {
return true;
}
// Check for partial matches (handle initials, etc.)
const authorParts = authorName.toLowerCase().split(' ');
return LAB_MEMBERS.some(labMember => {
const labParts = labMember.toLowerCase().split(' ');
// Check if last names match and first initial matches
if (authorParts.length >= 2 && labParts.length >= 2) {
const authorLast = authorParts[authorParts.length - 1];
const labLast = labParts[labParts.length - 1];
const authorFirst = authorParts[0];
const labFirst = labParts[0];
return (authorLast === labLast &&
(authorFirst === labFirst ||
authorFirst.charAt(0) === labFirst.charAt(0) ||
labFirst.charAt(0) === authorFirst.charAt(0)));
}
return false;
});
}
function parseAuthors(authorString) {
// Handle various author format patterns
if (authorString.includes(' et al.')) {
// Handle "Author et al." format - extract the first authors
const firstAuthor = authorString.replace(' et al.', '').trim();
return [firstAuthor];
}
// Clean up the string
let cleanString = authorString.replace(/"/g, '').trim();
// Handle specific patterns found in the data
let authors = [];
// Pattern 1: "Name1, Name2, Name3" (most common)
if (cleanString.includes(', ')) {
authors = cleanString.split(', ');
}
// Pattern 2: "Name1 & Name2"
else if (cleanString.includes(' & ')) {
authors = cleanString.split(' & ');
}
// Pattern 3: "Name1 and Name2"
else if (cleanString.includes(' and ')) {
authors = cleanString.split(' and ');
}
// Pattern 4: Single author
else {
authors = [cleanString];
}
// Clean and filter authors
return authors
.map(author => author.trim())
.filter(author => author.length > 0)
.filter(author => !author.match(/^[A-Z]\.?$/)) // Remove single initials
.map(author => {
// Clean up the name
author = author.replace(/\s+/g, ' '); // Normalize whitespace
author = author.replace(/,$/, ''); // Remove trailing comma
// Handle "Last, First" to "First Last" conversion if needed
if (author.includes(',') && !author.includes(' ')) {
const parts = author.split(',');
if (parts.length === 2) {
author = `${parts[1].trim()} ${parts[0].trim()}`;
}
}
return author;
});
}
function extractPublicationData(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
let inYamlHeader = false;
let yamlLines = [];
for (let line of lines) {
if (line.trim() === '---') {
if (!inYamlHeader) {
inYamlHeader = true;
continue;
} else {
break;
}
}
if (inYamlHeader) {
yamlLines.push(line);
}
}
// Parse YAML-like content with multi-line support
const publication = {};
let currentKey = null;
let currentValue = '';
yamlLines.forEach(line => {
// Check if this is a new key-value pair
const match = line.match(/^(\w+):\s*(.*)$/);
if (match) {
// Save previous key-value if exists
if (currentKey) {
publication[currentKey] = currentValue.replace(/^"(.*)"$/, '$1').trim();
}
currentKey = match[1];
currentValue = match[2] || '';
} else if (currentKey && line.trim()) {
// This is a continuation of the previous value
currentValue += ' ' + line.trim();
}
});
// Save the last key-value pair
if (currentKey) {
publication[currentKey] = currentValue.replace(/^"(.*)"$/, '$1').trim();
}
if (publication.author) {
publication.authors = parseAuthors(publication.author);
}
return publication;
} catch (error) {
console.error(`Error parsing ${filePath}:`, error.message);
return null;
}
}
function scanPublications(publicationsDir) {
const publications = [];
const files = fs.readdirSync(publicationsDir);
files.forEach(file => {
if (file.endsWith('.qmd')) {
const filePath = path.join(publicationsDir, file);
const publication = extractPublicationData(filePath);
if (publication && publication.authors) {
publications.push(publication);
}
}
});
return publications;
}
function analyzeCollaborations(publications) {
const externalCollaborators = new Map();
const collaborationNetwork = [];
const coAuthorships = new Map();
publications.forEach(pub => {
if (!pub.authors) return;
// Track external collaborators
pub.authors.forEach(author => {
if (!isLabMember(author)) {
const count = externalCollaborators.get(author) || 0;
externalCollaborators.set(author, count + 1);
}
});
// Build collaboration network
for (let i = 0; i < pub.authors.length; i++) {
for (let j = i + 1; j < pub.authors.length; j++) {
const author1 = pub.authors[i];
const author2 = pub.authors[j];
const key = [author1, author2].sort().join('|');
if (!coAuthorships.has(key)) {
coAuthorships.set(key, []);
}
coAuthorships.get(key).push(pub.title);
}
}
});
// Convert to network format
coAuthorships.forEach((papers, key) => {
const [author1, author2] = key.split('|');
collaborationNetwork.push({
source: author1,
target: author2,
weight: papers.length,
publications: papers
});
});
return {
publications,
externalCollaborators: Array.from(externalCollaborators.entries())
.sort((a, b) => b[1] - a[1])
.map(([name, count]) => ({ name, count })),
collaborationNetwork
};
}
function generateDataFile(analysisResult, outputPath) {
const data = {
generated: new Date().toISOString(),
...analysisResult
};
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2));
console.log(`Generated collaboration data: ${outputPath}`);
}
// Main execution
if (require.main === module) {
const publicationsDir = path.join(__dirname, 'publications', 'articles');
const outputPath = path.join(__dirname, 'docs', 'collaborations-data.json');
try {
const publications = scanPublications(publicationsDir);
console.log(`Scanned ${publications.length} publications`);
const analysis = analyzeCollaborations(publications);
console.log(`Found ${analysis.externalCollaborators.length} external collaborators`);
// Ensure output directory exists
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
generateDataFile(analysis, outputPath);
// Also output summary to console
console.log('\nTop 10 External Collaborators:');
analysis.externalCollaborators.slice(0, 10).forEach(({ name, count }) => {
console.log(` ${name}: ${count} publications`);
});
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
module.exports = { analyzeCollaborations, scanPublications };