"""RegalFire original procedural fixture generators. Independent validation lives in check.py.""" import argparse from collections import deque import heapq import json from pathlib import Path import random import sys from common import * def astar(grid,costs,start,goal): h,w=len(grid),len(grid[0]); start=tuple(start); goal=tuple(goal) distance={start:0}; parent={}; queue=[(0,0,start)] while queue: _,cost,cell=heapq.heappop(queue) if distance[cell]!=cost: continue if cell==goal: path=[list(cell)] while cell!=start: cell=parent[cell]; path.append(list(cell)) return {'cost':cost,'path':path[::-1]} y,x=cell for dy,dx in [(1,0),(-1,0),(0,1),(0,-1)]: b=(y+dy,x+dx) if 0<=b[0]w//2] if not left or not right: raise ValueError('empty region') starts=[r.choice(left),r.choice(right)];goals=starts[::-1] elif family=='crossing': rows=[[p for p in free if p[0]==y] for y in range(h)] candidates=[row for row in rows if len(row)>=2 and row[-1][1]-row[0][1]>=2] if not candidates:raise ValueError('no crossing row') row=r.choice(candidates);starts=[row[0],row[-1]];goals=starts[::-1] else: goals=r.sample([p for p in free if p not in starts],2) grid=[''.join(row) for row in grid] lower=[single(grid,s,g) for s,g in zip(starts,goals)] plan=joint_bfs(grid,starts,goals) if all(x is not None for x in lower) else None lb=max(lower) if all(x is not None for x in lower) else None cost=len(plan)-1 if plan else None return {'grid':grid,'starts':starts,'goals':goals}, {'plan':plan,'optimal_makespan':cost,'independent_lower_bound':lb,'coordination_overhead':cost-lb if cost is not None else None} def read_label(fastq,policy): header,seq,sep,qual=fastq.split('\n'); errors=[] if not header.startswith('@'): errors.append('header') if sep!='+': errors.append('separator') if any(x not in 'ACGTN' for x in seq): errors.append('alphabet') if len(seq)!=len(qual): errors.append('quality_length') if any(not 33<=ord(q)<=74 for q in qual): errors.append('quality_ascii') if errors: return {'valid_fastq':False,'errors':sorted(errors),'passed_filter':False,'trimmed_sequence':None,'trimmed_quality':None,'quality_sum':None,'length':None,'n_count':None,'gc_count':None} end=len(seq) while end>0 and ord(qual[end-1])-33=policy['min_length'] and seq.count('N')<=policy['max_n'] and total>=policy['mean_q_min']*len(seq), 'trimmed_sequence':seq,'trimmed_quality':qual,'quality_sum':total,'length':len(seq),'n_count':seq.count('N'),'gc_count':seq.count('G')+seq.count('C')} def reads(r,family): policy={'min_length':r.randint(20,50),'max_n':r.randint(0,3),'mean_q_min':r.randint(18,28),'trim_q_min':r.randint(4,15)} length=r.randint(24,90) if family=='length_boundary': length=policy['min_length']+r.choice([-1,0,1]) seq=''.join(r.choices('ACGT',k=length));q=[r.randint(30,41) for _ in seq] if family=='quality_boundary': level=policy['mean_q_min']+r.choice([-1,0,1]);q=[level for _ in seq] if family=='ambiguous_bases': count=policy['max_n']+r.choice([0,1]);idx=r.sample(range(length),count);s=list(seq) for i in idx:s[i]='N' seq=''.join(s) if family=='clean': tail=r.randint(1,8);q[-tail:]=[2]*tail qual=''.join(chr(x+33) for x in q);header='@synthetic_read';sep='+' if family=='malformed_record': error=r.choice(['header','separator','alphabet','quality_length','quality_ascii']) if error=='header':header='synthetic_read' elif error=='separator':sep='-' elif error=='alphabet':seq=seq[:-1]+'X' elif error=='quality_length':qual=qual[:-1] else:qual=qual[:-1]+chr(32) fastq='\n'.join([header,seq,sep,qual]) return {'fastq':fastq,'policy':policy},read_label(fastq,policy) def alignment(a,b): n,m=len(a),len(b);d=[[0]*(m+1) for _ in range(n+1)] for i in range(n+1):d[i][0]=i for j in range(m+1):d[0][j]=j for i in range(1,n+1): for j in range(1,m+1):d[i][j]=min(d[i-1][j]+1,d[i][j-1]+1,d[i-1][j-1]+(a[i-1]!=b[j-1])) aa=[];bb=[];i=n;j=m while i or j: if i and j and d[i][j]==d[i-1][j-1]+(a[i-1]!=b[j-1]):aa.append(a[i-1]);bb.append(b[j-1]);i-=1;j-=1 elif i and d[i][j]==d[i-1][j]+1:aa.append(a[i-1]);bb.append('-');i-=1 else:aa.append('-');bb.append(b[j-1]);j-=1 return d[n][m],''.join(aa[::-1]),''.join(bb[::-1]) def align(r,family): rand=lambda n:''.join(r.choices('ACGT',k=n)) if family=='homopolymer':a=rand(10)+r.choice('ACGT')*r.randint(8,16)+rand(10) elif family=='tandem_repeat':a=rand(9)+rand(3)*r.randint(3,7)+rand(9) else:a=rand(r.randint(24,50)) if family=='literal_N':a=a[:10]+'NN'+a[12:] source=a if family=='reverse_complement':b=rc(a) else: b=list(a) for _ in range(r.randint(1,5)): pos=r.randrange(len(b));operation=r.choice(['sub','ins','del']) if operation=='sub':b[pos]=r.choice('ACGTN' if family=='literal_N' else 'ACGT') elif operation=='ins':b.insert(pos,r.choice('ACGT')) else:b.pop(pos) b=''.join(b) distance,aa,bb=alignment(a,b);threshold=max(0,distance+r.choice([-1,0,1])) return {'a':a,'b':b,'source_sequence':source,'threshold':threshold},{'edit_distance':distance,'within_threshold':distance<=threshold,'aligned_a':aa,'aligned_b':bb} def interval_label(inp): def bed(v):return v['start']-(v['basis']=='one_closed'),v['end'] q=inp['query'];a,b=bed(q);matches=[];segments=[] for i,f in enumerate(inp['features']): if f['contig']!=q['contig'] or (inp['same_strand'] and f['strand']!=q['strand']):continue c,d=bed(f);lo,hi=max(a,c),min(b,d) if locount:raise ValueError('count below checkpoint size') attempts=0 while total