def find_maps(self): if not self.bound_conf: return ValueError( 'Bound conf not set! use RefinementObjectFiles().find_bound_file()' ) self.mtz_path = db_functions.check_file_status('refine.mtz', self.bound_conf) self.two_fofc_path = db_functions.check_file_status( '2fofc.map', self.bound_conf) self.fofc_path = db_functions.check_file_status( 'fofc.map', self.bound_conf)
# if can't find bound state, just use the latest pdb file bound_conf = obj.pdb_latest else: # no pdb = no proasis upload (same for mtz, two_fofc and fofc) # TODO: Turn this into a function instead of repeating file check fail_count += 1 print('FAILURE FOR: ' + str(obj.crystal_name.crystal_name)) print('no pdb = no proasis upload (same for mtz, two_fofc and fofc)') continue print(bound_conf) if 'split.bound-state' in bound_conf: mtz = db_functions.check_file_status('refine.mtz', obj.pdb_latest) two_fofc = db_functions.check_file_status('2fofc.map', obj.pdb_latest) fofc = db_functions.check_file_status('fofc.map', obj.pdb_latest) else: mtz = db_functions.check_file_status('refine.mtz', bound_conf) two_fofc = db_functions.check_file_status('2fofc.map', bound_conf) fofc = db_functions.check_file_status('fofc.map', bound_conf) if not mtz[0] or not two_fofc[0] or not fofc[0]: fail_count += 1 print('FAILURE FOR: ' + str(obj.crystal_name.crystal_name)) print('missing file') print(bound_conf) print(mtz) print(two_fofc)
def get_crystal_info(request): queryset = Target.objects.filter() filter_fields = ('target_name', ) submission = request.GET.get('target_name') crystals = Crystal.objects.filter(target__target_name=submission) data = [] for crys in crystals: refinements = Refinement.objects.filter(crystal_name=crys, outcome__gte=4) for ref in refinements: out_dict = { 'crys': '', 'refinement_status': '✘', 'pandda_model': '✘', 'pdb_present': '✘', 'twofofc_present': '✘', 'fofc_present': '✘', 'mtz_present': '✘', 'in_proasis': '✘', 'proasis_strucids': '✘', 'files_out': '✘', 'out_directory': '✘', 'uploaded_to_verne': '✘', } out_dict['crys'] = str(crys.crystal_name) out_dict['refinement_status'] = str(ref.outcome) events = list( set([ pr.pandda_run for pr in PanddaEvent.objects.filter(crystal=crys) ])) if events: out_dict['pandda_model'] = '✓' proasis = ProasisHits.objects.filter(crystal_name=crys).exclude( strucid=None).exclude(strucid='') pout = [] bound_conf = '' # if there is a pdb file named in the bound_conf field, use it as the upload structure for proasis if ref.bound_conf: if os.path.isfile(ref.bound_conf): bound_conf = ref.bound_conf # otherwise, use the most recent pdb file (according to soakdb) elif ref.pdb_latest: if os.path.isfile(ref.pdb_latest): # if this is from a refinement folder, find the bound-state pdb file, rather than the ensemble if 'Refine' in ref.pdb_latest: search_path = '/'.join(ref.pdb_latest.split('/')[:-1]) print(search_path) files = glob.glob( str(search_path + '/refine*split.bound*.pdb')) print(files) if len(files) == 1: bound_conf = files[0] else: # if can't find bound state, just use the latest pdb file bound_conf = ref.pdb_latest if bound_conf: out_dict['pdb_present'] = bound_conf.split('/')[-1] if 'split.bound-state' in bound_conf: mtz = check_file_status('refine.mtz', ref.pdb_latest) two_fofc = check_file_status('2fofc.map', ref.pdb_latest) fofc = check_file_status('fofc.map', ref.pdb_latest) else: mtz = check_file_status('refine.mtz', bound_conf) two_fofc = check_file_status('2fofc.map', bound_conf) fofc = check_file_status('fofc.map', bound_conf) if mtz[0]: out_dict['mtz_present'] = '✓' if two_fofc[0]: out_dict['twofofc_present'] = '✓' if fofc[0]: out_dict['fofc_present'] = '✓' if proasis: out_dict['in_proasis'] = '✓' proasis_strucids = [p.strucid for p in proasis] out_dict['proasis_strucids'] = ', '.join(proasis_strucids) for p in proasis: pout = ProasisOut.objects.filter(crystal=crys, proasis=p) if pout: out_dirs = list( set([os.path.join(o.root, o.start) for o in pout])) out_dict['files_out'] = '✓' out_dict['out_directory'] = ', '.join(out_dirs) out_root = list( set([ os.path.join(o.root, o.start.split('/')[0]) for o in pout ])) if len(out_root) == 1: if os.path.isfile( os.path.join(out_root[0], 'verne.transferred')): out_dict['uploaded_to_verne'] = '✓' data.append(out_dict) return JsonResponse(data, safe=False)
def run(self): fail_count = 0 # select anything 'in refinement' (3) or above refinement = Refinement.objects.filter(outcome__gte=3) # set up info for each entry that matches the filter for obj in refinement: # set up blank fields for entries in proasis hits table bound_conf = '' files = [] mtz = '' two_fofc = '' fofc = '' mod_date = '' proasis_hit_entry = '' entry = '' confs = [] ligand_list = [] # if there is a pdb file named in the bound_conf field, use it as the upload structure for proasis if obj.bound_conf: if os.path.isfile(obj.bound_conf): bound_conf = obj.bound_conf # otherwise, use the most recent pdb file (according to soakdb) elif obj.pdb_latest: if os.path.isfile(obj.pdb_latest): # if this is from a refinement folder, find the bound-state pdb file, rather than the ensemble if 'Refine' in obj.pdb_latest: search_path = '/'.join(obj.pdb_latest.split('/')[:-1]) files = glob.glob( str(search_path + '/refine*bound*.pdb')) if len(files) == 1: bound_conf = files[0] else: # if can't find bound state, just use the latest pdb file bound_conf = obj.pdb_latest else: # no pdb = no proasis upload (same for mtz, two_fofc and fofc) # TODO: Turn this into a function instead of repeating file check fail_count += 1 continue mtz = db_functions.check_file_status('refine.mtz', bound_conf) two_fofc = db_functions.check_file_status('2fofc.map', bound_conf) fofc = db_functions.check_file_status('fofc.map', bound_conf) if not mtz[0] or not two_fofc[0] or not fofc[0]: fail_count += 1 continue # if a suitable pdb file is found, then search for ligands if bound_conf: try: pdb_file = open(bound_conf, 'r') ligand_list = [] for line in pdb_file: if "LIG" in line: try: # ligands identified by 'LIG', with preceeding '.' for alt conf letter lig_string = re.search(r".LIG.......", line).group() # just use lig string instead of separating into list items (to handle altconfs) # TODO: This has changed from a list of ['LIG','RES','ID'] to string. Check usage ligand_list.append(lig_string) except: continue # if no ligands are found in the pdb file, no upload to proasis (checked that no strucs. had alternative # labels in them) except: ligand_list = None if not ligand_list: continue # get a unique list of ligands unique_ligands = list(set(ligand_list)) # remove the first letter (alt conf) from unique ligands lig_no_conf = [l[1:] for l in unique_ligands] for l in lig_no_conf: # check whether there are more than 1 entries for any of the lig strings without alt conf if lig_no_conf.count(l) > 1: # this is an alt conf situation - add the alt confs to the conf list confs.extend([lig for lig in unique_ligands if l in lig]) # get the date the pdb file was modified mod_date = misc_functions.get_mod_date(bound_conf) if mod_date: # if there's already an entry for that structure if ProasisHits.objects.filter( refinement=obj, crystal_name=obj.crystal_name).exists(): # if there are no alternate conformations if not confs: # get the relevant entry entry = ProasisHits.objects.get( refinement=obj, crystal_name=obj.crystal_name) # if the pdb file is older than the current one, or it has not been uploaded to proasis if entry.modification_date < mod_date or not entry.strucid: # delete structure and remove files to remove from proasis is strucid exists if entry.strucid: proasis_api_funcs.delete_structure( entry.strucid) entry.strucid = None entry.save() if self.hit_directory in entry.pdb_file: os.remove(entry.pdb_file) if self.hit_directory in entry.mtz: os.remove(entry.mtz) if self.hit_directory in entry.two_fofc: os.remove(entry.two_fofc) if self.hit_directory in entry.fofc: os.remove(entry.fofc) # otherwise, just update the relevant fields entry.pdb_file = bound_conf entry.modification_date = mod_date entry.mtz = mtz[1] entry.two_fofc = two_fofc[1] entry.fofc = fofc[1] entry.ligand_list = unique_ligands entry.save() # if there ARE alternate conformations else: # for each conformation for conf in confs: # do the same as above, but setting the altconf field too # TODO: functionalise to add altconfs and not repeat method entry = ProasisHits.objects.get( refinement=obj, crystal_name=obj.crystal_name, altconf=conf) if entry.modification_date < mod_date or not entry.strucid: if entry.strucid: proasis_api_funcs.delete_structure( entry.strucid) entry.strucid = None entry.save() if self.hit_directory in entry.pdb_file: os.remove(entry.pdb_file) if self.hit_directory in entry.mtz: os.remove(entry.mtz) if self.hit_directory in entry.two_fofc: os.remove(entry.two_fofc) if self.hit_directory in entry.fofc: os.remove(entry.fofc) entry.pdb_file = bound_conf entry.modification_date = mod_date entry.mtz = mtz[1] entry.two_fofc = two_fofc[1] entry.fofc = fofc[1] entry.ligand_list = unique_ligands entry.altconf = conf entry.save() # if there's not already an entry for that structure else: # if no altconfs if not confs: # create entry without an altconf proasis_hit_entry = ProasisHits.objects.get_or_create( refinement=obj, crystal_name=obj.crystal_name, pdb_file=bound_conf, modification_date=mod_date, mtz=mtz[1], two_fofc=two_fofc[1], fofc=fofc[1], ligand_list=unique_ligands) # if altconfs if confs: for conf in confs: # create an entry for each altconf # TODO: The pdb file will need to be edited later to pull out other altconfs of the same lig proasis_hit_entry = ProasisHits.objects.get_or_create( refinement=obj, crystal_name=obj.crystal_name, pdb_file=bound_conf, modification_date=mod_date, mtz=mtz[1], two_fofc=two_fofc[1], fofc=fofc[1], ligand_list=unique_ligands, altconf=conf) dimple = Dimple.objects.filter(crystal_name=obj.crystal_name) if dimple.count() == 1: if dimple[0].reference and dimple[ 0].reference.reference_pdb: if os.path.isfile(dimple[0].reference.reference_pdb): proasis_lead_entry = ProasisLeads.objects.get_or_create( reference_pdb=dimple[0].reference) else: if ProasisLeads.objects.filter( reference_pdb=dimple[0].reference).exists( ): print('removing...') proasis_lead_entry = ProasisLeads.objects.get( reference_pdb=dimple[0].reference) proasis_lead_entry.delete() print(fail_count) with self.output().open('w') as f: f.write('')