def handle(self, **options): input_file = options['input_file'] output_dir = options['output_dir'] # Create output directory, exiting if already exists if os.path.exists(output_dir): raise CommandError( 'Output directory {} already exists'.format(output_dir)) else: os.makedirs(output_dir) # Parse input reader = csv.reader(input_file, delimiter=',') for experiment_id, well in reader: try: experiment_id = int(experiment_id.strip()) except ValueError: continue well = well.strip() if re.match('Tile0000\d\d\.bmp', well): tile = well.split('.bmp')[0] elif re.match('Tile0000\d\d', well): tile = well else: try: tile = well_to_tile(well) except ValueError: continue input_image_url = '{}/{}/{}.bmp'.format(settings.BASE_URL_IMG, experiment_id, tile) output_image_path = '{}/{}_{}.bmp'.format(output_dir, experiment_id, tile) r = requests.get(input_image_url, stream=True) if r.status_code != 200: raise CommandError( 'non-ok GET status for {}'.format(input_image_url)) with open(output_image_path, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) self.stdout.write('{} written'.format(output_image_path))
def handle(self, **options): input_file = options['input_file'] output_dir = options['output_dir'] # Create output directory, exiting if already exists if os.path.exists(output_dir): raise CommandError('Output directory {} already exists' .format(output_dir)) else: os.makedirs(output_dir) # Parse input reader = csv.reader(input_file, delimiter=',') for experiment_id, well in reader: try: experiment_id = int(experiment_id.strip()) except ValueError: continue well = well.strip() if re.match('Tile0000\d\d\.bmp', well): tile = well.split('.bmp')[0] elif re.match('Tile0000\d\d', well): tile = well else: try: tile = well_to_tile(well) except ValueError: continue input_image_url = '{}/{}/{}.bmp'.format( settings.BASE_URL_IMG, experiment_id, tile) output_image_path = '{}/{}_{}.bmp'.format( output_dir, experiment_id, tile) r = requests.get(input_image_url, stream=True) if r.status_code != 200: raise CommandError('non-ok GET status for {}' .format(input_image_url)) with open(output_image_path, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) self.stdout.write('{} written'.format(output_image_path))
def get_image_url(self, mode=None): """ Get the image url for this experiment. Optionally supply mode='thumbnail' or mode='devstar'. """ tile = well_to_tile(self.well) if mode == 'thumbnail': url = '/'.join( (settings.BASE_URL_THUMBNAIL, str(self.plate_id), tile)) url += '.jpg' elif mode == 'devstar': url = '/'.join( (settings.BASE_URL_DEVSTAR, str(self.plate_id), tile)) url += 'res.png' else: url = '/'.join((settings.BASE_URL_IMG, str(self.plate_id), tile)) url += '.bmp' return url
def get_image_url(self, mode=None): """ Get the image url for this experiment. Optionally supply mode='thumbnail' or mode='devstar'. """ tile = well_to_tile(self.well) if mode == 'thumbnail': url = '/'.join((settings.BASE_URL_THUMBNAIL, str(self.plate_id), tile)) url += '.jpg' elif mode == 'devstar': url = '/'.join((settings.BASE_URL_DEVSTAR, str(self.plate_id), tile)) url += 'res.png' else: url = '/'.join((settings.BASE_URL_IMG, str(self.plate_id), tile)) url += '.bmp' return url
def handle(self, **options): ###################################################### # FIRST STAGE # # Connect to legacy database and create a dictionary to # translate from new canonical clones names to legacy # clone information. # ###################################################### legacy_db = MySQLdb.connect(host=LEGACY_DATABASE['HOST'], user=LEGACY_DATABASE['USER'], passwd=LEGACY_DATABASE['PASSWORD'], db=LEGACY_DATABASE['NAME']) cursor = legacy_db.cursor() cursor.execute('SELECT clone, node_primary_name, 384PlateID, ' '384Well FROM RNAiPlate ' 'WHERE RNAiPlateID != "L4440"') new_to_old = {} for row in cursor.fetchall(): old_clone_name = row[0].strip() node_primary_name = row[1] if isinstance(node_primary_name, str): node_primary_name = node_primary_name.strip() plate_384 = row[2].strip() well_384 = row[3].strip() if plate_384[0:3] == 'GHR': new_clone_name = plate_384 + '@' + well_384 else: new_clone_name = old_clone_name new_to_old[new_clone_name] = { 'old_clone_name': old_clone_name, 'node_primary_name': node_primary_name, } ###################################################### # SECOND STAGE # # For each row in cherrypick_list, create a corresponding # row for the legacy database, with fields: # ('mutant', 'mutantAllele', 'RNAiPlateID', '96well', # 'ImgName', 'clone', 'node_primary_name', # 'seq_node_primary_name') # ###################################################### cherrypick_list = options['cherrypick_list'] # Skip header row next(cherrypick_list) ''' # Uncomment this to add header row with fieldnames legacy_fields = ('mutant', 'mutantAllele', 'RNAiPlateID', '96well', 'ImgName', 'clone', 'node_primary_name', 'seq_node_primary_name') self.stdout.write(','.join(legacy_fields)) ''' for line in cherrypick_list: row = line.split(',') source_plate_name = row[0].strip() # Since legacy database did not represent empty wells if not source_plate_name or source_plate_name == "None": continue source_well = row[1].strip() destination_plate_name = row[2].strip() destination_well = row[3].strip() destination_tile = well_to_tile(destination_well) + '.bmp' try: source_plate = LibraryPlate.objects.get( id=source_plate_name) source_stock = LibraryStock.objects.get( plate=source_plate, well=source_well) except ObjectDoesNotExist: raise CommandError('{} at {} not found' .format(source_plate_name, source_well)) allele = destination_plate_name.split('-')[0] if allele == 'universal': # For universal plates, allele == gene == 'universal' gene = 'universal' else: # Otherwise, gene can be retrieved from WormStrain gene = WormStrain.objects.get(allele=allele).gene # Account for legacy database using mispelled allele 'zc310' if allele == 'zu310': allele = 'zc310' destination_plate_chars = list(destination_plate_name) destination_plate_chars[1] = 'c' destination_plate_name = ''.join(destination_plate_chars) if source_stock.intended_clone: new_clone_name = source_stock.intended_clone.id old_clone_name = new_to_old[new_clone_name]['old_clone_name'] node_primary_name = ( new_to_old[new_clone_name]['node_primary_name']) if node_primary_name == 'NULL': node_primary_name = '' else: old_clone_name = '' node_primary_name = '' output = [ gene, allele, destination_plate_name, destination_well, destination_tile, old_clone_name, node_primary_name, 'X' ] self.stdout.write(','.join([str(x) for x in output]))
def handle(self, **options): ###################################################### # FIRST STAGE # # Connect to legacy database and create a dictionary to # translate from new canonical clones names to legacy # clone information. # ###################################################### # legacy_db = MySQLdb.connect(host=LEGACY_DATABASE['HOST'], legacy_db = mysql.connector.connect(host=LEGACY_DATABASE['HOST'], user=LEGACY_DATABASE['USER'], passwd=LEGACY_DATABASE['PASSWORD'], db=LEGACY_DATABASE['NAME']) cursor = legacy_db.cursor() cursor.execute('SELECT clone, node_primary_name, 384PlateID, ' '384Well FROM RNAiPlate ' 'WHERE RNAiPlateID != "L4440"') new_to_old = {} for row in cursor.fetchall(): old_clone_name = row[0].strip() node_primary_name = row[1] if isinstance(node_primary_name, str): node_primary_name = node_primary_name.strip() plate_384 = row[2].strip() well_384 = row[3].strip() if plate_384[0:3] == 'GHR': new_clone_name = plate_384 + '@' + well_384 else: new_clone_name = old_clone_name new_to_old[new_clone_name] = { 'old_clone_name': old_clone_name, 'node_primary_name': node_primary_name, } ###################################################### # SECOND STAGE # # For each row in cherrypick_list, create a corresponding # row for the legacy database, with fields: # ('mutant', 'mutantAllele', 'RNAiPlateID', '96well', # 'ImgName', 'clone', 'node_primary_name', # 'seq_node_primary_name') # ###################################################### cherrypick_list = options['cherrypick_list'] # Skip header row next(cherrypick_list) ''' # Uncomment this to add header row with fieldnames legacy_fields = ('mutant', 'mutantAllele', 'RNAiPlateID', '96well', 'ImgName', 'clone', 'node_primary_name', 'seq_node_primary_name') self.stdout.write(','.join(legacy_fields)) ''' for line in cherrypick_list: row = line.split(',') source_plate_name = row[0].strip() # Since legacy database did not represent empty wells if not source_plate_name or source_plate_name == "None": continue source_well = row[1].strip() destination_plate_name = row[2].strip() destination_well = row[3].strip() destination_tile = well_to_tile(destination_well) + '.bmp' try: source_plate = LibraryPlate.objects.get(id=source_plate_name) source_stock = LibraryStock.objects.get(plate=source_plate, well=source_well) except ObjectDoesNotExist: raise CommandError('{} at {} not found'.format( source_plate_name, source_well)) allele = destination_plate_name.split('-')[0] if allele == 'universal': # For universal plates, allele == gene == 'universal' gene = 'universal' else: # Otherwise, gene can be retrieved from WormStrain gene = WormStrain.objects.get(allele=allele).gene # Account for legacy database using mispelled allele 'zc310' if allele == 'zu310': allele = 'zc310' destination_plate_chars = list(destination_plate_name) destination_plate_chars[1] = 'c' destination_plate_name = ''.join(destination_plate_chars) if source_stock.intended_clone: new_clone_name = source_stock.intended_clone.id old_clone_name = new_to_old[new_clone_name]['old_clone_name'] node_primary_name = ( new_to_old[new_clone_name]['node_primary_name']) if node_primary_name == 'NULL': node_primary_name = '' else: old_clone_name = '' node_primary_name = '' output = [ gene, allele, destination_plate_name, destination_well, destination_tile, old_clone_name, node_primary_name, 'X' ] self.stdout.write(','.join([str(x) for x in output]))
def get_devstar_count_path(self): tile = well_to_tile(self.well) f = '/'.join((settings.BASE_DIR_DEVSTAR_OUTPUT, str(self.plate_id), tile)) f += 'cnt.txt' return f
def get_tile(self): return well_to_tile(self.well)
def get_tile(well): """Get the tile, e.g. Tile000094, corresponding to well.""" return well_to_tile(well)
def test_well_to_tile(self): for well, tile in self.well_tile_tuples: self.assertEqual(well_to_tile(well), tile)
def get_devstar_count_path(self): tile = well_to_tile(self.well) f = '/'.join( (settings.BASE_DIR_DEVSTAR_OUTPUT, str(self.plate_id), tile)) f += 'cnt.txt' return f