| import argparse |
| import os |
| import re |
| from collections import defaultdict |
|
|
| def is_dir(p): |
| try: |
| return os.path.isdir(p) |
| except Exception: |
| return False |
|
|
| def is_file(p): |
| try: |
| return os.path.isfile(p) |
| except Exception: |
| return False |
|
|
| TCGA_ID_RE = re.compile(r"(TCGA-[A-Z0-9]{2}-[A-Z0-9]+)") |
|
|
| def extract_case_id(name): |
| m = TCGA_ID_RE.search(name) |
| return m.group(1) if m else None |
|
|
| def collect_from_dir(path): |
| items = [] |
| for n in os.listdir(path): |
| if n.startswith('.'): |
| continue |
| items.append(n) |
| cases = set() |
| case_to_items = defaultdict(list) |
| for n in items: |
| cid = extract_case_id(n) |
| if cid: |
| cases.add(cid) |
| case_to_items[cid].append(n) |
| return items, cases, case_to_items |
|
|
| def collect_from_file(path): |
| items = [] |
| with open(path, 'r', encoding='utf-8', errors='ignore') as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
| items.append(line) |
| cases = set() |
| case_to_items = defaultdict(list) |
| for n in items: |
| cid = extract_case_id(n) |
| if cid: |
| cases.add(cid) |
| case_to_items[cid].append(n) |
| return items, cases, case_to_items |
|
|
| def collect(path): |
| if is_dir(path): |
| return collect_from_dir(path) |
| if is_file(path): |
| return collect_from_file(path) |
| raise FileNotFoundError(path) |
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument('--feature_source', type=str, default='/mnt/datadisk0/TCGA_pt/MESO_UNI') |
| ap.add_argument('--label_source', type=str, default='/mnt/datadisk0/datasets/TCGA-MESO') |
| ap.add_argument('--limit', type=int, default=50) |
| args = ap.parse_args() |
|
|
| feat_items, feat_cases, feat_map = collect(args.feature_source) |
| lab_items, lab_cases, lab_map = collect(args.label_source) |
|
|
| common = sorted(feat_cases & lab_cases) |
| miss_labels = sorted(feat_cases - lab_cases) |
| miss_features = sorted(lab_cases - feat_cases) |
|
|
| print('Feature source:', args.feature_source) |
| print('Total feature items:', len(feat_items)) |
| print('Feature cases:', len(feat_cases)) |
| print('Label source:', args.label_source) |
| print('Total label items:', len(lab_items)) |
| print('Label cases:', len(lab_cases)) |
| print('Matched cases:', len(common)) |
|
|
| if miss_labels: |
| print('Cases present in features but missing labels:', len(miss_labels)) |
| for cid in miss_labels[:args.limit]: |
| print(cid, 'feature_count=', len(feat_map.get(cid, []))) |
| if len(miss_labels) > args.limit: |
| print('... and', len(miss_labels) - args.limit, 'more') |
| else: |
| print('No cases missing labels') |
|
|
| if miss_features: |
| print('Cases present in labels but missing features:', len(miss_features)) |
| for cid in miss_features[:args.limit]: |
| print(cid) |
| if len(miss_features) > args.limit: |
| print('... and', len(miss_features) - args.limit, 'more') |
| else: |
| print('No cases missing features') |
|
|
| top_counts = sorted(((cid, len(feat_map[cid])) for cid in common), key=lambda x: (-x[1], x[0])) |
| if top_counts: |
| print('Top matched cases by feature count:') |
| for cid, c in top_counts[:min(args.limit, 20)]: |
| print(cid, 'feature_count=', c) |
|
|
| if __name__ == '__main__': |
| main() |
|
|
|
|