ProximileAdmin commited on
Commit
5ce3f7b
·
verified ·
1 Parent(s): 56919c1

Update create_diffusion_dataset.py

Browse files
Files changed (1) hide show
  1. create_diffusion_dataset.py +34 -10
create_diffusion_dataset.py CHANGED
@@ -36,6 +36,8 @@ class ProteinNetworkConversationDataset:
36
 
37
  interactions = []
38
  protein_set = set()
 
 
39
 
40
  for idx, row in self.df.iterrows():
41
  try:
@@ -45,7 +47,19 @@ class ProteinNetworkConversationDataset:
45
 
46
  if protein_a in ['-', 'nan', ''] or protein_b in ['-', 'nan', '']:
47
  continue
 
 
 
 
 
 
 
 
 
 
 
48
 
 
49
  protein_set.add(protein_a)
50
  protein_set.add(protein_b)
51
 
@@ -63,7 +77,7 @@ class ProteinNetworkConversationDataset:
63
  except Exception:
64
  continue
65
 
66
- print(f"Extracted {len(interactions)} valid interactions")
67
  print(f"Found {len(protein_set)} unique proteins")
68
 
69
  return interactions, sorted(list(protein_set))
@@ -331,24 +345,29 @@ class ProteinNetworkConversationDataset:
331
  # Sort for consistency
332
  proteins = sorted(proteins)
333
 
334
- # Group interactions by type
335
- interactions_by_type = defaultdict(list)
336
  for interaction in interactions:
 
 
 
 
337
  int_type = interaction.get('interaction_type', 'physical')
338
  # Ensure consistent ordering
339
  p1, p2 = sorted([interaction['protein_a'], interaction['protein_b']])
340
- interactions_by_type[int_type].append(f"{p1}--{p2}")
341
 
342
  result = f"PROTEINS: {', '.join(proteins)}\n\n"
343
 
344
  for int_type, edges in interactions_by_type.items():
345
  if edges:
346
  result += f"{int_type.upper()} INTERACTIONS:\n"
347
- for edge in sorted(edges):
348
  result += f" {edge}\n"
349
  result += "\n"
350
 
351
- result += f"NETWORK SUMMARY: {len(proteins)} proteins, {len(interactions)} interactions"
 
352
  return result.strip()
353
 
354
  def format_interactions_as_text(self, interactions):
@@ -358,17 +377,22 @@ class ProteinNetworkConversationDataset:
358
  if not interactions:
359
  return "No interactions predicted."
360
 
361
- interactions_by_type = defaultdict(list)
 
362
  for interaction in interactions:
 
 
 
 
363
  int_type = interaction.get('interaction_type', 'physical')
364
  p1, p2 = sorted([interaction['protein_a'], interaction['protein_b']])
365
- interactions_by_type[int_type].append(f"{p1}--{p2}")
366
 
367
  result = ""
368
  for int_type, edges in interactions_by_type.items():
369
  if edges:
370
  result += f"{int_type.upper()} INTERACTIONS:\n"
371
- for edge in sorted(edges):
372
  result += f" {edge}\n"
373
  result += "\n"
374
 
@@ -433,4 +457,4 @@ if __name__ == "__main__":
433
 
434
  print("\nTask distribution:")
435
  for task, count in task_types.items():
436
- print(f" {task}: {count}")
 
36
 
37
  interactions = []
38
  protein_set = set()
39
+ # Use set to track unique interactions and avoid duplicates
40
+ seen_interactions = set()
41
 
42
  for idx, row in self.df.iterrows():
43
  try:
 
47
 
48
  if protein_a in ['-', 'nan', ''] or protein_b in ['-', 'nan', '']:
49
  continue
50
+
51
+ # Skip self-interactions
52
+ if protein_a == protein_b:
53
+ continue
54
+
55
+ # Create canonical interaction key (sorted to avoid A-B vs B-A duplicates)
56
+ interaction_key = tuple(sorted([protein_a, protein_b]) + [interaction_type])
57
+
58
+ # Skip if we've already seen this exact interaction
59
+ if interaction_key in seen_interactions:
60
+ continue
61
 
62
+ seen_interactions.add(interaction_key)
63
  protein_set.add(protein_a)
64
  protein_set.add(protein_b)
65
 
 
77
  except Exception:
78
  continue
79
 
80
+ print(f"Extracted {len(interactions)} valid unique interactions")
81
  print(f"Found {len(protein_set)} unique proteins")
82
 
83
  return interactions, sorted(list(protein_set))
 
345
  # Sort for consistency
346
  proteins = sorted(proteins)
347
 
348
+ # Group interactions by type and use sets to avoid duplicates
349
+ interactions_by_type = defaultdict(set)
350
  for interaction in interactions:
351
+ # Skip self-interactions
352
+ if interaction['protein_a'] == interaction['protein_b']:
353
+ continue
354
+
355
  int_type = interaction.get('interaction_type', 'physical')
356
  # Ensure consistent ordering
357
  p1, p2 = sorted([interaction['protein_a'], interaction['protein_b']])
358
+ interactions_by_type[int_type].add(f"{p1}--{p2}")
359
 
360
  result = f"PROTEINS: {', '.join(proteins)}\n\n"
361
 
362
  for int_type, edges in interactions_by_type.items():
363
  if edges:
364
  result += f"{int_type.upper()} INTERACTIONS:\n"
365
+ for edge in sorted(edges): # Sort the set for consistent output
366
  result += f" {edge}\n"
367
  result += "\n"
368
 
369
+ total_interactions = sum(len(edges) for edges in interactions_by_type.values())
370
+ result += f"NETWORK SUMMARY: {len(proteins)} proteins, {total_interactions} unique interactions"
371
  return result.strip()
372
 
373
  def format_interactions_as_text(self, interactions):
 
377
  if not interactions:
378
  return "No interactions predicted."
379
 
380
+ # Group interactions by type and use sets to avoid duplicates
381
+ interactions_by_type = defaultdict(set)
382
  for interaction in interactions:
383
+ # Skip self-interactions
384
+ if interaction['protein_a'] == interaction['protein_b']:
385
+ continue
386
+
387
  int_type = interaction.get('interaction_type', 'physical')
388
  p1, p2 = sorted([interaction['protein_a'], interaction['protein_b']])
389
+ interactions_by_type[int_type].add(f"{p1}--{p2}")
390
 
391
  result = ""
392
  for int_type, edges in interactions_by_type.items():
393
  if edges:
394
  result += f"{int_type.upper()} INTERACTIONS:\n"
395
+ for edge in sorted(edges): # Sort the set for consistent output
396
  result += f" {edge}\n"
397
  result += "\n"
398
 
 
457
 
458
  print("\nTask distribution:")
459
  for task, count in task_types.items():
460
+ print(f" {task}: {count}")