Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add controls example #44

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/controls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
import sys
import argparse
from IlluminaBeadArrayFiles import GenotypeCalls, BeadPoolManifest

parser = argparse.ArgumentParser('Generate a control dashboard report')
parser.add_argument('manifest', help='BPM manifest file')
parser.add_argument('gtc_directory', help='Directory containing GTC files')
parser.add_argument('output_file', help='Location to write report')

args = parser.parse_args()

try:
manifest = BeadPoolManifest(args.manifest)
except:
sys.stderr.write('Failed to read data from manifest\n')
sys.exit(-1)

with open(args.output_file, 'w') as output_handle:
# Write header and grab control_config from manifest
# Sample_ID and Sentrix_Label may differ from the genome studio report
# as we're just using the Sentrix_Label for both in this script
output_handle.write(','.join(['Category','Control','BeadType','Sample_ID','Sentrix_Label']))
controls = manifest.control_config.split('\n')
# Trim empty control if exists
if controls[-1] == '':
controls.pop(len(controls)-1)
num_sections = len(controls[0].split(',')[0].split(':'))
for i in range(1, num_sections+1):
output_handle.write(',')
output_handle.write(','.join([f'Section {i} X', f'Section {i} Y']))
output_handle.write('\n')

# Build controls dictionary from gtcs
samples = {}
for filename in os.listdir(args.gtc_directory):
if filename.lower().endswith('.gtc'):
base, ext = os.path.splitext(filename)
samples[base] = {'gtc': filename}
for sample in samples:
sys.stdout.write('Processing ' + sample + '\n')
gtc = GenotypeCalls(os.path.join(args.gtc_directory, samples[sample]['gtc']))
samples[sample]['controls_x'] = gtc.get_control_x_intensities()
samples[sample]['controls_y'] = gtc.get_control_y_intensities()

# Write out controls in genome studio "ControlDashboard.csv" format
sys.stdout.write(f'Writing to {args.output_file}\n')
control_offset = 0
for control_data in controls:
beadtypes, category, color, control = control_data.split(',')
# assumes all same beadtype, converts to int then back to str to trim off preceeding zeroes
beadtype = str(int(beadtypes.split(':')[0]))
for sample in sorted(samples):
output_handle.write(','.join([category, control, beadtype, sample, sample]))
mialam24 marked this conversation as resolved.
Show resolved Hide resolved
controls_x = samples[sample]['controls_x'][control_offset:control_offset + num_sections]
controls_y = samples[sample]['controls_y'][control_offset:control_offset + num_sections]
for x, y in zip(controls_x, controls_y):
output_handle.write(',')
output_handle.write(f'{x},{y}')
output_handle.write('\n')
control_offset = control_offset + num_sections