def rundmp(spath, msapath, outdir): """Run DeepMetaPSICOV to produce contact prediction lists. :param seqpath: Input sequence filepath. :type seqpath: str :param msapath: Input MSA filepath. :type msapath: str """ dmp_exec = '"' + ppaths.check_path(DMP_PATH, 'file') + '"' try: os.system(dmp_exec + ' -i ' + spath + ' -a ' + msapath) except Exception: logging.critical( ' An error occurred while executing DeepMetaPSICOV.') raise OSError
def _check_uniprot(inuniprot): """Return Uniprot segment threshold value and Uniprot database path. :param inuniprot: Initial argument for Uniprot threshold :type inuniprot: str :raises ValueError: Argument is not valid. :return: Threshold value and database path. :rtype: float, str """ try: float(inuniprot) except Exception: logging.critical('Uniprot threshold given not valid.') raise ValueError try: dbpath = check_path(pconf.UNICLUST_FASTA_PATH, 'file') except Exception: logging.warning( 'Uniprot database file does not exist. Switching to server-only.') dbpath = 'server-only' return float(inuniprot), dbpath
def main(): parser = create_argument_parser() args = parser.parse_args() global logger logger = pcl.pisacov_logger(level="info") welcomemsg, starttime = pcl.welcome(command=__script__) logger.info(welcomemsg) kwlist = pco._default_keys() configin = {} if args.update_sifts_database is not None: outpath = ppaths.check_path(args.update_sifts_database[0], 'either') if os.path.isdir(outpath) is True: outpath = os.path.join(outpath, ('pdb_chain_uniprot' + os.extsep + 'csv')) pol.getsifts(outpath) configin['SIFTS_PATH'] = outpath else: pass if args.view_configuration is True: print('** ' + __prog__ + " v." + __version__ + ' configuration file **' + os.linesep) with open(pconf.__file__) as f: for line in f: line = _lineformat(line) print(line, end='') print('** End of configuration file **' + os.linesep) return else: pass if args.get_confpath is True: print(pconf.__file__) return else: pass if args.conf_file is False: newconf = pco._parse_conf() else: newconf = {} if args.reset_hhblits_arguments is True: newconf['HHBLITS_PARAMETERS'] = pco._default_values( 'HHBLITS_PARAMETERS') else: pass if args.sifts_path is not None: configin['SIFTS_PATH'] = args.sifts_path[0] if args.pisa_path is not None: configin['PISA_PATH'] = args.pisa_path[0] if args.hhblits_path is not None: configin['HHBLITS_PATH'] = args.hhblits_path[0] if args.dmp_path is not None: configin['DMP_PATH'] = args.dmp_path[0] if args.uniclust_path is not None: configin['UNICLUST_FASTA_PATH'] = args.uniclust_path[0] if args.hhblits_arguments is not None: configin['HHBLITS_PARAMETERS'] = args.hhblits_arguments[0] if args.neighbours is not None: configin['NEIGHBOURS_MINDISTANCE'] = args.neighbours[0] configin['REMOVE_INTRA_CONTACTS'] = False if isinstance(args.hhblits_location, list) is True: configin['HHBLITS_DATABASE_NAME'] = args.hhblits_location[0] configin['HHBLITS_DATABASE_DIR'] = args.hhblits_location[1] compmsg = { 'SIFTS_PATH': "Please, enter the path to the SIFTS csv file:\n", 'PISA_PATH': "Please, enter the path to the PISA executable:\n", 'HHBLITS_PATH': "Please, enter the path to the HHBLITS executable:\n", 'HHBLITS_DATABASE_NAME': ("Please, enter the name of the HHBLITS database name\n" + "as requested by DeepMetaPSICOV (e.g. uniclust30_2018_08):\n"), 'HHBLITS_DATABASE_DIR': "Please, enter the directory of the HHBLITS database:\n", 'DMP_PATH': "Please, enter the path to the DeepMetaPSICOV executable:\n", 'HHBLITS_PARAMETERS': ("Please, enter the 5 HHBLITS parameters.\n" + "#iterations, E-value cutoff, Non-redundant seqs to keep, " + " MinimumCoverageWithMasterSeq(%), and MaxPairwiseSequenceIdentity.\n" + "Leave empty for DMP default (3, 0.001, 'inf', 50, 99).\n"), 'UNICLUST_FASTA_PATH': ("Please, enter the path to the UniClust fasta file.\n" "An empty input will deactivate this option.\n"), 'NEIGHBOURS_MINDISTANCE': ("Press ENTER for intramolecular contacts " + "to be removed from intermolecular contact lists.\n" + "Otherwise, enter the minimum distance to be cosidered " + "between neighbours. This will override the removal of" + "intramolecular contacts that will be now ignored.\n") } for keystr in kwlist[:-1]: if keystr in configin or args.conf_file is True: if args.conf_file is True: while True: newval = input(compmsg[keystr]) try: newconf[keystr] = pco._check_input(newval, keystr) if keystr == 'NEIGHBOURS_MINDISTANCE': if newval is None or newval == "": newconf['REMOVE_INTRA_CONTACTS'] = True else: newconf['REMOVE_INTRA_CONTACTS'] = False except Exception: print("Not a valid input. Please, try again:") else: break else: newconf[keystr] = pco._check_input(configin[keystr], keystr) if keystr == 'NEIGHBOURS_MINDISTANCE': newconf['REMOVE_INTRA_CONTACTS'] = pco._check_input( configin['REMOVE_INTRA_CONTACTS'], 'REMOVE_INTRA_CONTACTS') else: pass if 'REMOVE_INTRA_CONTACTS' in configin: newconf['REMOVE_INTRA_CONTACTS'] = pco._check_input( configin['REMOVE_INTRA_CONTACTS'], 'REMOVE_INTRA_CONTACTS') if newconf['REMOVE_INTRA_CONTACTS'] is True: newconf['NEIGHBOURS_MINDISTANCE'] == 2 else: pass else: pass _outconffile(newconf) endmsg = pcl.ok(starttime, command=__script__) logger.info(endmsg) return
def main(): parser = create_argument_parser() args = parser.parse_args() global logger logger = pcl.pisacov_logger(level="info") welcomemsg, starttime = pcl.welcome(command=__script__) logger.info(welcomemsg) # PARSE CONFIGURATION FILE: invals = pco._initialise_inputs() invals['INSEQ'] = None invals['INSTR'] = None invals['ALTDB'] = None invals['OUTROOT'] = None invals['OUTCSVPATH'] = None invals['UPTHRESHOLD'] = None # READ INPUT ARGUMENTS invals['INSEQ'] = ppaths.check_path(args.seqpath[0], 'file') invals['INSTR'] = ppaths.check_path(args.crystalpath[0], 'file') if args.hhblits_arguments is not None: invals['HHBLITS_PARAMETERS'] = pco._check_hhparams( args.hhblits_arguments) else: pass if args.uniprot_threshold is not None: try: invals['UPTHRESHOLD'] = float(args.uniprot_threshold[0]) except ValueError: logger.critical('Uniprot threshold given not valid.') if invals['UNICLUST_FASTA_PATH'] is None: invals['UNICLUST_FASTA_PATH'] = pco._uniurl else: pass if args.skip_conpred is True: skipexec = True if (args.hhblits_arguments is not None or args.uniprot_threshold is not None): logger.info( 'HHblits, UniProt threshold parameters given bypassed by --skip_conpred' ) else: skipexec = False cropping = args.remove_insertions scoring = [cropping, not cropping] if args.outdir is None: invals['OUTROOT'] = ppaths.check_path(os.path.dirname(invals['INSEQ'])) else: invals['OUTROOT'] = ppaths.check_path(os.path.join(args.outdir[0], '')) ppaths.mdir(invals['OUTROOT']) invals['OUTCSVPATH'] = [] if args.collection_file is None: invals['OUTCSVPATH'].append( ppaths.check_path( os.path.join(invals['OUTROOT'], ("evcovsignal" + os.extsep + "cropped" + os.extsep + "pisacov" + os.extsep + "csv")))) invals['OUTCSVPATH'].append( ppaths.check_path( os.path.join(invals['OUTROOT'], ("evcovsignal" + os.extsep + "full" + os.extsep + "pisacov" + os.extsep + "csv")))) else: if cropping is True: invals['OUTCSVPATH'].append( ppaths.check_path(args.collection_file[0])) invals['OUTCSVPATH'].append( ppaths.check_path( os.path.splitext(args.collection_file[0])[0] + os.extsep + 'full' + os.extsep + os.path.splitext(args.collection_file[0])[1])) else: invals['OUTCSVPATH'].append(None) invals['OUTCSVPATH'].append( ppaths.check_path(args.collection_file[0])) # Define formats used sources = pco._sources() # Parse sequence and structure files logger.info('Parsing sequence file...') seqs = cps.parseseqfile(invals['INSEQ']) logger.info('Parsing structure file...') strs, filestrs = cps.parsestrfile(invals['INSTR']) if len(seqs) == 1 or len(strs) == 1: if len(seqs) == 1: for key in seqs: pdbid = key elif len(seqs) > 1 and len(strs) == 1: for key in strs: for key2 in seqs: if key.upper() == key2.upper(): pdbid = key.upper() else: if key2.upper() in key.upper(): pdbid = key2.upper() else: raise Exception( 'More than one pdbid in sequence and/or structure set.') seq = seqs[pdbid] #structure = strs[pdbid] # CROPPING AND RENUMBERING outpdbdir = os.path.join(invals['OUTROOT'], pdbid, "") instrc = os.path.join(invals['OUTROOT'], pdbid, os.path.basename(invals['INSTR'])) fseq = {} fmsa = {} if skipexec is False: if cropping is True: logger.info('Cropping and renumbering sequences, ' + 'structures according to SIFTS database.') logger.info(pcl.running('CROPS-cropstr')) itime = datetime.datetime.now() psc.runcrops(invals['INSEQ'], invals['INSTR'], invals['SIFTS_PATH'], invals['UPTHRESHOLD'], invals['UNICLUST_FASTA_PATH'], invals['OUTROOT']) logger.info(pcl.running('CROPS-cropstr', done=itime)) else: logger.info('Renumbering structure ' + 'according to position in sequence.') logger.info(pcl.running('CROPS-renumber')) itime = datetime.datetime.now() psc.renumcrops(invals['INSEQ'], invals['INSTR'], invals['OUTROOT']) logger.info(pcl.running('CROPS-renumber', done=itime)) ppaths.mdir(outpdbdir) if cropping is False: psc.splitseqs(invals['INSEQ'], outpdbdir) copyfile(invals['INSTR'], instrc) for i, iseq in seq.imer.items(): fiseq = pdbid + '_' + i + '.fasta' fseq[i] = os.path.join(invals['OUTROOT'], pdbid, fiseq) fiseq = pdbid + '_' + i + '.msa.aln' fmsa[i] = os.path.join(invals['OUTROOT'], pdbid, 'hhblits', fiseq) if skipexec is False: iseq.dump(fseq[i]) # Parse cropped sequences and maps if cropping is True: amap = {} fcropseq = {} fcropmsa = {} for i, iseq in seq.imer.items(): fprefix = pdbid + '_' + i + '.crops.to_uniprot' fmap = os.path.join(invals['OUTROOT'], pdbid, fprefix + os.extsep + 'cropmap') amap.update(cps.parsemapfile(fmap)[pdbid]) fcropseq[i] = os.path.join(invals['OUTROOT'], pdbid, fprefix + os.extsep + 'fasta') fcropmsa[i] = os.path.join( invals['OUTROOT'], pdbid, 'hhblits', (fprefix + os.extsep + 'msa' + os.extsep + 'aln')) seq.set_cropmaps(amap, cropmain=True) # EXECUTION OF EXTERNAL PROGRAMS hhdir = os.path.join(invals['OUTROOT'], pdbid, 'hhblits', '') dmpdir = os.path.join(invals['OUTROOT'], pdbid, 'dmp', '') pisadir = os.path.join(invals['OUTROOT'], pdbid, 'pisa', '') fstr = os.path.join( invals['OUTROOT'], (pdbid + os.extsep + 'crops' + os.extsep + 'seq' + os.extsep + 'pdb')) if cropping: fcropstr = os.path.join( invals['OUTROOT'], pdbid, (pdbid + os.extsep + 'crops' + os.extsep + 'oldids' + os.extsep + 'to_uniprot' + os.path.splitext(invals['INSTR'])[1])) if skipexec is False: # MSA GENERATOR ppaths.mdir(hhdir) if invals['HHBLITS_PARAMETERS'] == ['3', '0.001', 'inf', '50', '99']: logger.info( 'Generating Multiple Sequence Alignment using DeepMetaPSICOV default parameters... [AS RECOMMENDED]' ) elif invals['HHBLITS_PARAMETERS'] == ['2', '0.001', '1000', '0', '90']: logger.info( 'Generating Multiple Sequence Alignment using HHBlits default parameters...' ) else: logger.info( 'Generating Multiple Sequence Alignment using user-custom parameters...' ) for i, iseq in seq.imer.items(): sfile = fcropseq[i] if cropping is True else fseq[i] afile = fcropmsa[i] if cropping is True else fmsa[i] logger.info(pcl.running('HHBlits')) itime = datetime.datetime.now() themsa = psm.runhhblits(sfile, invals['HHBLITS_PARAMETERS'], hhdir) logger.info(pcl.running('HHBlits', done=itime)) if cropping is True: iseq.cropmsa = themsa if iseq.ncrops() == 0: iseq.msa = iseq.cropmsa logger.info(' Cropped sequence ' + iseq.oligomer_id + '_' + iseq.name + ' is identical to original sequence.') continue else: pass else: iseq.msa = themsa # DEEP META PSICOV RUN ppaths.mdir(dmpdir) if skipexec is False: logger.info( 'Generating contact prediction lists via DeepMetaPSICOV...') for i, iseq in seq.imer.items(): sfile = fcropseq[i] if cropping is True else fseq[i] afile = fcropmsa[i] if cropping is True else fmsa[i] nsfile = os.path.join(dmpdir, os.path.basename(sfile)) if sfile != nsfile: copyfile(sfile, nsfile) logger.info(pcl.running('DeepMetaPSICOV')) itime = datetime.datetime.now() psd.rundmp(nsfile, afile, dmpdir) logger.info(pcl.running('DeepMetaPSICOV', done=itime)) # INTERFACE GENERATION, PISA ppaths.mdir(pisadir) if skipexec is False: logger.info('Generating interface files via PISA...') sfile = fcropstr if cropping is True else fstr logger.info(pcl.running('PISA')) itime = datetime.datetime.now() iflist = psp.runpisa(sfile, pisadir, sessionid=pdbid) logger.info(pcl.running('PISA', done=itime)) endmsg = pcl.ok(starttime, command=__script__) logger.info(endmsg) return
def main(): starttime = time.time() parser = create_argument_parser() args = parser.parse_args() global logger logger = pcl.pisacov_logger(level="info") welcomemsg, starttime = pcl.welcome(command=__script__) logger.info(welcomemsg) # PARSE CONFIGURATION FILE: invals = pco._initialise_inputs() invals['INSEQ'] = None invals['INIFS'] = None invals['OUTROOT'] = None invals['OUTCSVPATH'] = None # READ INPUT ARGUMENTS invals['INSEQ'] == ppaths.check_path(args.seqpath[0], 'file') invals['INIFS'] = [] args.remove_insertions = False for fp in args.dimers: if '*' in fp: invals['INIFS'] += ppaths.check_wildcard(fp) else: invals['INIFS'].append(ppaths.check_path(fp, 'file')) invals['INIFS'] = list(dict.fromkeys(invals['INIFS'])) if args.hhblits_arguments is not None: invals['HHBLITS_PARAMETERS'] = pco._check_hhparams( args.hhblits_arguments) else: pass if args.skip_conpred is True: skipexec = True if args.hhblits_arguments is not None: logger.info('HHblits parameters given bypassed by --skip_conpred') else: skipexec = False if args.outdir is None: invals['OUTROOT'] = ppaths.check_path(os.path.dirname(invals['INSEQ'])) else: invals['OUTROOT'] = ppaths.check_path(os.path.join(args.outdir[0], '')) ppaths.mdir(invals['OUTROOT']) if args.collection_file is None: invals['OUTCSVPATH'] = ppaths.check_path( os.path.join(invals['OUTROOT'], ("evcovsignal" + os.extsep + "full" + os.extsep + "pisacov" + os.extsep + "csv"))) else: invals['OUTCSVPATH'] = ppaths.check_path(args.collection_file[0]) if os.path.isfile(invals['OUTCSVPATH']) is False: pic.csvheader(invals['OUTCSVPATH'], cropped=False) # Define formats used sources = pco._sources() # Parse sequence and structure files logger.info('Parsing sequence file...') seqs = cps.parseseqfile(invals['INSEQ']) if len(seqs) == 1: if len(seqs) == 1: for key in seqs: pdbid = key.lower() else: raise Exception('More than one pdbid in sequence set.') seq = seqs[pdbid] outpdbdir = os.path.join(invals['OUTROOT'], pdbid, "") # RENUMBERING fseq = {} fmsa = {} if skipexec is False: if invals['INIFS'] is not None: logger.info('Renumbering interfaces provided ' + 'according to position in sequence.') for path in invals['INIFS']: instrc = os.path.join(invals['OUTROOT'], pdbid, os.path.basename(path)) logger.info(pcl.running('CROPS-renumber')) itime = datetime.datetime.now() psc.renumcrops(invals['INSEQ'], path, invals['OUTROOT']) copyfile(path, instrc) logger.info(pcl.running('CROPS-renumber', done=itime)) ppaths.mdir(outpdbdir) for i, iseq in seq.imer.items(): fiseq = pdbid + '_' + i + os.extsep + 'fasta' fseq[i] = os.path.join(invals['OUTROOT'], pdbid, fiseq) fiseq = pdbid + '_' + i + os.extsep + 'msa' + os.extsep + 'aln' fmsa[i] = os.path.join(invals['OUTROOT'], pdbid, 'hhblits', fiseq) if skipexec is False: iseq.dump(fseq[i]) # EXECUTION OF EXTERNAL PROGRAMS hhdir = os.path.join(invals['OUTROOT'], pdbid, 'hhblits', '') dmpdir = os.path.join(invals['OUTROOT'], pdbid, 'dmp', '') fstr = [] for file in invals['INIFS']: fstr.append( os.path.join( invals['OUTROOT'], (os.path.splitext(os.path.basename(file))[0] + os.extsep + 'crops' + os.extsep + 'seq' + os.extsep + 'pdb'))) if skipexec is False: # MSA GENERATOR ppaths.mdir(hhdir) if invals['HHBLITS_PARAMETERS'] == ['3', '0.001', 'inf', '50', '99']: logger.info( 'Generating Multiple Sequence Alignment using DeepMetaPSICOV default parameters... [AS RECOMMENDED]' ) elif invals['HHBLITS_PARAMETERS'] == ['2', '0.001', '1000', '0', '90']: logger.info( 'Generating Multiple Sequence Alignment using HHBlits default parameters...' ) else: logger.info( 'Generating Multiple Sequence Alignment using user-custom parameters...' ) for i, iseq in seq.imer.items(): sfile = fseq[i] afile = fmsa[i] logger.info(pcl.running('HHBlits')) itime = datetime.datetime.now() themsa = psm.runhhblits(sfile, invals['HHBLITS_PARAMETERS'], hhdir) logger.info(pcl.running('HHBlits', done=itime)) iseq.msa = themsa # DEEP META PSICOV RUN logger.info( 'Generating contact prediction lists via DeepMetaPSICOV...') ppaths.mdir(dmpdir) for i, iseq in seq.imer.items(): sfile = fseq[i] afile = fmsa[i] nsfile = os.path.join(dmpdir, os.path.basename(sfile)) if sfile != nsfile: copyfile(sfile, nsfile) logger.info(pcl.running('DeepMetaPSICOV')) itime = datetime.datetime.now() psd.rundmp(nsfile, afile, dmpdir) logger.info(pcl.running('DeepMetaPSICOV', done=itime)) # GENERATE INTERFACE LIST iflist = [] for filepath in fstr: ifname = os.path.splitext(os.path.basename(filepath))[0] iflist.append(pci.interface(name=ifname)) # CONTACT ANALYSIS AND MATCH logger.info('Opening output csv files...') resultdir = os.path.join(invals['OUTROOT'], pdbid, 'pisacov', '') ppaths.mdir(resultdir) csvfile = os.path.join( resultdir, (pdbid + os.extsep + "evcovsignal" + os.extsep + "full" + os.extsep + "pisacov" + os.extsep + "csv")) pic.csvheader(csvfile, cropped=False, pisascore=False) logger.info('Parsing sequence files...') for i, fpath in fseq.items(): seq.imer[i].seqs['conkit'] = ckio.read(fpath, 'fasta')[0] seq.imer[i].biotype = csq.guess_type(seq.imer[i].seqs['mainseq']) logger.info('Parsing contact predictions lists...') conpred = {} matches = [] for s in seq.imer: if s not in conpred: conpred[s] = {} for source, attribs in sources.items(): fc = os.path.splitext(os.path.basename(fseq[s]))[0] fc += attribs[1] confile = os.path.join(invals['OUTROOT'], pdbid, attribs[0], fc) conpred[s][source] = ckio.read(confile, attribs[2])[0] logger.info('Parsing crystal structure contacts...') for i in range(len(iflist)): inputmap = ckio.read(fstr[i], 'pdb') if len(inputmap) == 4: chnames = list(iflist[i].chains.keys()) chtypes = list(iflist[i].chains.values()) if (seq.whatseq(chnames[0]) != seq.whatseq(chnames[1]) or (chtypes[0] != 'Protein' or chtypes[1] != 'Protein')): if chtypes[0] != "Protein" or chtypes[1] != "Protein": logger.info( 'Interface ' + str(i) + ' is not a Protein-Protein interface. Ignoring.') else: logger.info('Interface ' + str(i) + ' is not a homodimer. Ignoring.') iflist[i].structure = None matches.append(None) continue s = seq.whatseq(chnames[0]) try: iflist[i].structure = [] for m in range(len(inputmap)): iflist[i].structure.append(inputmap[m].as_contactmap()) iflist[i].structure[m].id = inputmap[m].id except Exception: for m in range(len(inputmap)): iflist[i].structure.append(inputmap[m]) # ConKit LEGACY. matches.append({}) for source, attribs in sources.items(): matches[i][source] = pcc.contact_atlas( name=pdbid + '_' + str(s), conpredmap=conpred[s][source], strmap=iflist[i].structure, sequence=seq.imer[s], removeintra=True) else: iflist[i].structure = None matches.append(None) continue logger.info('Computing results and writing them to file...') for i in range(len(iflist)): if matches[i] is None: continue results = [pdbid, str(i + 1)] results.append(matches[i]['psicov'].chain1) results.append(matches[i]['psicov'].chain2) sid = seq.whatseq(matches[i]['psicov'].chain1) results.append(str(sid)) results.append(str(seq.imer[sid].length())) results.append(str(seq.imer[sid].cropmsa.meff)) results.append(str(seq.imer[sid].ncrops())) results.append(str(seq.imer[sid].full_length())) for source, attribs in sources.items(): appresults = pcs.list_scores(matches[i][source], tag=source) results += appresults pic.lineout(results, csvfile) pic.lineout(results, invals['OUTCSVPATH']) endmsg = pcl.ok(starttime, command=__script__) logger.info(endmsg) return
def _check_input(val, key): compmsg = { 'SIFTS_PATH': "SIFTS_PATH file not found.", 'PISA_PATH': "'PISA_PATH file not found.", 'HHBLITS_PATH': "HHBLITS_PATH file not found.", 'HHBLITS_DATABASE_NAME': "HHBLITS_DATABASE_DIR should be a string.", 'HHBLITS_DATABASE_DIR': "HHBLITS_DATABASE_DIR directory not found.", 'DMP_PATH': "DMP_PATH file not found.", 'HHBLITS_PARAMETERS': "Format should be [int, float, int or 'inf', int, int].", 'UNICLUST_FASTA_PATH': "UNICLUST_FASTA_PATH file not found.", 'NEIGHBOURS_MINDISTANCE': "NEIGHBOURS_MINDISTANCE should be an integer.", 'REMOVE_INTRA_CONTACTS': "REMOVE_INTRA_CONTACTS should be a boolean." } errormsg = compmsg[ key] + ' Please, update the configuration file using pisaconf.' if (key == 'SIFTS_PATH' or key == 'PISA_PATH' or key == 'HHBLITS_PATH' or key == 'DMP_PATH'): try: val = check_path(val, 'file') except Exception: logging.critical(errormsg) raise NameError() if (key == 'UNICLUST_FASTA_PATH'): if val == "" or val is None: val = None else: try: val = check_path(val, 'file') except Exception: logging.critical(errormsg) raise NameError() elif (key == 'HHBLITS_DATABASE_DIR'): try: val = check_path(val, 'dir') except Exception: logging.critical(errormsg) raise NameError() elif (key == 'HHBLITS_DATABASE_NAME'): if isinstance(val, str) is False: try: val = str(val) except Exception: logging.critical(errormsg) raise ValueError() else: pass elif (key == 'HHBLITS_PARAMETERS'): if val is None or val == "": val = _default_values(key) else: if isinstance(val, list) is False: logging.critical('HHBLITS_PARAMETERS is not a python list.') raise ValueError() else: for n in range(len(val)): if n == 1: if isinstance(val[n], float) is False: try: val[n] = float(val[n]) except Exception: logging.critical(errormsg) raise TypeError else: if n == 2 and val[n] == 'inf': pass else: if isinstance(val[n], int) is False: try: val[n] = int(val[n]) except Exception: logging.critical(errormsg) raise TypeError elif (key == 'NEIGHBOURS_MINDISTANCE'): if val is None or val == "": val = _default_values(key) else: if isinstance(val[n], int) is False: try: val = int(val) except Exception: logging.critical(errormsg) raise TypeError elif (key == 'REMOVE_INTRA_CONTACTS'): if val is None: val = _default_values(key) elif isinstance(val, str) is True: if val == "": val = _default_values(key) elif val.lower() == "true": val = True elif val.lower() == "false": val = False else: logging.critical(errormsg) raise TypeError elif isinstance(val, bool) is True: pass else: logging.critical(errormsg) raise TypeError return val
def main(): parser = create_argument_parser() args = parser.parse_args() global logger logger = pcl.pisacov_logger(level="info") welcomemsg, starttime = pcl.welcome(command=__script__) logger.info(welcomemsg) # PARSE CONFIGURATION FILE: invals = pco._initialise_inputs() invals['INSEQ'] = None invals['INSTR'] = None invals['ALTDB'] = None invals['OUTROOT'] = None invals['OUTCSVPATH'] = None invals['UPTHRESHOLD'] = None # READ INPUT ARGUMENTS invals['INSEQ'] = ppaths.check_path(args.seqpath[0], 'file') invals['INSTR'] = ppaths.check_path(args.crystalpath[0], 'file') if args.hhblits_arguments is not None: invals['HHBLITS_PARAMETERS'] = pco._check_hhparams( args.hhblits_arguments) else: pass if args.uniprot_threshold is not None: try: invals['UPTHRESHOLD'] = float(args.uniprot_threshold[0]) except ValueError: logger.critical('Uniprot threshold given not valid.') if invals['UNICLUST_FASTA_PATH'] is None: invals['UNICLUST_FASTA_PATH'] = pco._uniurl else: pass if args.skip_conpred is True: skipexec = True if (args.hhblits_arguments is not None or args.uniprot_threshold is not None): logger.info( 'HHblits, UniProt threshold parameters given bypassed by --skip_conpred' ) else: skipexec = False cropping = args.remove_insertions scoring = [cropping, not cropping] if args.outdir is None: invals['OUTROOT'] = ppaths.check_path(os.path.dirname(invals['INSEQ'])) else: invals['OUTROOT'] = ppaths.check_path(os.path.join(args.outdir[0], '')) ppaths.mdir(invals['OUTROOT']) invals['OUTCSVPATH'] = [] if args.collection_file is None: invals['OUTCSVPATH'].append( ppaths.check_path( os.path.join(invals['OUTROOT'], ("evcovsignal" + os.extsep + "cropped" + os.extsep + "pisacov" + os.extsep + "csv")))) invals['OUTCSVPATH'].append( ppaths.check_path( os.path.join(invals['OUTROOT'], ("evcovsignal" + os.extsep + "full" + os.extsep + "pisacov" + os.extsep + "csv")))) else: if cropping is True: invals['OUTCSVPATH'].append( ppaths.check_path(args.collection_file[0])) invals['OUTCSVPATH'].append( ppaths.check_path( os.path.splitext(args.collection_file[0])[0] + os.extsep + 'full' + os.extsep + os.path.splitext(args.collection_file[0])[1])) else: invals['OUTCSVPATH'].append(None) invals['OUTCSVPATH'].append( ppaths.check_path(args.collection_file[0])) if args.plot_formats is None: plotformats = {'png'} else: plotformats = set() for element in args.plot_formats: if element.lower() in {'png', 'eps', 'dat'}: plotformats.add(element.lower()) # Define formats used sources = pco._sources() # Parse sequence and structure files logger.info('Parsing sequence file...') # seqs = cps.parseseqfile(invals['INSEQ']) seqs = pio.read(invals['INSEQ'], 'fasta') logger.info('Parsing structure file...') # strs, filestrs = cps.parsestrfile(invals['INSTR']) strs, filestrs = pio.read(invals['INSTR'], 'pdb') if len(seqs) == 1 or len(strs) == 1: if len(seqs) == 1: for key in seqs: pdbid = key elif len(seqs) > 1 and len(strs) == 1: for key in strs: for key2 in seqs: if key.upper() == key2.upper(): pdbid = key.upper() else: if key2.upper() in key.upper(): pdbid = key2.upper() else: raise Exception( 'More than one pdbid in sequence and/or structure set.') seq = seqs[pdbid] #structure = strs[pdbid] # CROPPING AND RENUMBERING outpdbdir = os.path.join(invals['OUTROOT'], pdbid, "") instrc = os.path.join(invals['OUTROOT'], pdbid, os.path.basename(invals['INSTR'])) fseq = {} fmsa = {} if skipexec is False: if cropping is True: logger.info('Cropping and renumbering sequences, ' + 'structures according to SIFTS database.') logger.info(pcl.running('CROPS-cropstr')) itime = datetime.datetime.now() psc.runcrops(invals['INSEQ'], invals['INSTR'], invals['SIFTS_PATH'], invals['UPTHRESHOLD'], invals['UNICLUST_FASTA_PATH'], invals['OUTROOT']) logger.info(pcl.running('CROPS-cropstr', done=itime)) else: logger.info('Renumbering structure ' + 'according to position in sequence.') logger.info(pcl.running('CROPS-renumber')) itime = datetime.datetime.now() psc.renumcrops(invals['INSEQ'], invals['INSTR'], invals['OUTROOT']) logger.info(pcl.running('CROPS-renumber', done=itime)) ppaths.mdir(outpdbdir) copyfile(invals['INSTR'], instrc) for i, iseq in seq.imer.items(): fiseq = pdbid + '_' + i + '.fasta' fseq[i] = os.path.join(invals['OUTROOT'], pdbid, fiseq) fiseq = pdbid + '_' + i + '.msa.aln' fmsa[i] = os.path.join(invals['OUTROOT'], pdbid, 'hhblits', fiseq) if skipexec is False: iseq.dump(fseq[i]) # Parse cropped sequences and maps if cropping is True: amap = {} fcropseq = {} fcropmsa = {} for i, iseq in seq.imer.items(): fprefix = pdbid + '_' + i + '.crops.to_uniprot' fmap = os.path.join(invals['OUTROOT'], pdbid, fprefix + os.extsep + 'cropmap') amap.update(cps.parsemapfile(fmap)[pdbid]) fcropseq[i] = os.path.join(invals['OUTROOT'], pdbid, fprefix + os.extsep + 'fasta') fcropmsa[i] = os.path.join( invals['OUTROOT'], pdbid, 'hhblits', (fprefix + os.extsep + 'msa' + os.extsep + 'aln')) seq.set_cropmaps(amap, cropmain=True) if iseq.ncrops() == 0: logger.info(' Cropped sequence ' + iseq.oligomer_id + '_' + iseq.name + ' is identical to the original sequence.') else: logger.info(' Cropped sequence ' + iseq.oligomer_id + '_' + iseq.name + ' is ' + str(iseq.ncrops()) + ' residues ' + 'shorter than the original sequence.') # EXECUTION OF EXTERNAL PROGRAMS hhdir = os.path.join(invals['OUTROOT'], pdbid, 'hhblits', '') dmpdir = os.path.join(invals['OUTROOT'], pdbid, 'dmp', '') pisadir = os.path.join(invals['OUTROOT'], pdbid, 'pisa', '') fstr = os.path.join( invals['OUTROOT'], (pdbid + os.extsep + 'crops' + os.extsep + 'seq' + os.extsep + 'pdb')) if cropping: fcropstr = os.path.join( invals['OUTROOT'], pdbid, (pdbid + os.extsep + 'crops' + os.extsep + 'oldids' + os.extsep + 'to_uniprot' + os.path.splitext(invals['INSTR'])[1])) if skipexec is False: # MSA GENERATOR ppaths.mdir(hhdir) if invals['HHBLITS_PARAMETERS'] == ['3', '0.001', 'inf', '50', '99']: logger.info( 'Generating Multiple Sequence Alignment using DeepMetaPSICOV default parameters... [AS RECOMMENDED]' ) elif invals['HHBLITS_PARAMETERS'] == ['2', '0.001', '1000', '0', '90']: logger.info( 'Generating Multiple Sequence Alignment using HHBlits default parameters...' ) else: logger.info( 'Generating Multiple Sequence Alignment using user-custom parameters...' ) for i, iseq in seq.imer.items(): sfile = fcropseq[i] if cropping is True else fseq[i] afile = fcropmsa[i] if cropping is True else fmsa[i] logger.info(pcl.running('HHBlits')) itime = datetime.datetime.now() themsa = psm.runhhblits(sfile, invals['HHBLITS_PARAMETERS'], hhdir) logger.info(pcl.running('HHBlits', done=itime)) if cropping is True: iseq.cropmsa = themsa if iseq.ncrops() == 0: iseq.msa = iseq.cropmsa continue else: pass else: iseq.msa = themsa # DEEP META PSICOV RUN ppaths.mdir(dmpdir) if skipexec is False: logger.info( 'Generating contact prediction lists via DeepMetaPSICOV...') for i, iseq in seq.imer.items(): sfile = fcropseq[i] if cropping is True else fseq[i] afile = fcropmsa[i] if cropping is True else fmsa[i] nsfile = os.path.join(dmpdir, os.path.basename(sfile)) if sfile != nsfile: copyfile(sfile, nsfile) logger.info(pcl.running('DeepMetaPSICOV')) itime = datetime.datetime.now() psd.rundmp(nsfile, afile, dmpdir) logger.info(pcl.running('DeepMetaPSICOV', done=itime)) # INTERFACE GENERATION, PISA ppaths.mdir(pisadir) if skipexec is False: logger.info('Generating interface files via PISA...') sfile = fcropstr if cropping is True else fstr logger.info(pcl.running('PISA')) itime = datetime.datetime.now() iflist = psp.runpisa(sfile, pisadir, sessionid=pdbid) logger.info(pcl.running('PISA', done=itime)) # READ DATA IF SKIPEXEC USED: if skipexec is True: logger.info('Parsing already generated files...') for i, iseq in seq.imer.items(): sfile = fcropstr if cropping is True else fstr afile = fcropmsa[i] if cropping is True else fmsa[i] if cropping is True: # iseq.cropmsa = ckio.read(afile, 'jones') iseq.cropmsa = pio.read(afile, 'jones') if iseq.ncrops() == 0: scoring[1] = True # iseq.msa = ckio.read(afile, 'jones') iseq.msa = ckio.read(afile, 'jones') else: # iseq.msa = ckio.read(afile, 'jones') iseq.msa = pio.read(afile, 'jones') ixml = os.path.join(pisadir, (os.path.splitext(os.path.basename(sfile))[0] + os.extsep + 'interface' + os.extsep + 'xml')) axml = os.path.join(pisadir, (os.path.splitext(os.path.basename(sfile))[0] + os.extsep + 'assembly' + os.extsep + 'xml')) iflist = pci.parse_interface_xml(ixml, axml) # CONTACT ANALYSIS AND MATCH logger.info('Opening output csv files...') resultdir = os.path.join(invals['OUTROOT'], pdbid, 'pisacov', '') ppaths.mdir(resultdir) csvfile = [] csvfile.append( os.path.join(resultdir, (pdbid + os.extsep + "evcovsignal" + os.extsep + "cropped" + os.extsep + "pisacov" + os.extsep + "csv"))) csvfile.append( os.path.join(resultdir, (pdbid + os.extsep + "evcovsignal" + os.extsep + "full" + os.extsep + "pisacov" + os.extsep + "csv"))) for n in range(2): if scoring[n] is True: cpd = True if cropping else False pic.csvheader(csvfile[n], cropped=cpd, pisascore=True) if invals['OUTCSVPATH'][n] is not None: if os.path.isfile(invals['OUTCSVPATH'][n]) is False: pic.csvheader(invals['OUTCSVPATH'][n], cropped=cpd, pisascore=True) logger.info('Parsing sequence files...') for i, fpath in fseq.items(): # seq.imer[i].seqs['conkit'] = ckio.read(fpath, 'fasta')[0] seq.imer[i].seqs['conkit'] = pio.read(fpath, 'fasta', ck=True)[0] logger.info('Parsing contact predictions lists...') conpred = {} matches = [] for s in seq.imer: if s not in conpred: conpred[s] = {} fs = fcropseq[s] if cropping else fseq[s] for source, attribs in sources.items(): fc = os.path.splitext(os.path.basename(fs))[0] fc += os.extsep + attribs[1] confile = os.path.join(dmpdir, fc) # conpred[s][source] = ckio.read(confile, attribs[2])[0] conpred[s][source] = pio.read(confile, attribs[2], ck=True)[0] logger.info('Parsing crystal structure contacts...') for i in range(len(iflist)): logger.info(os.linesep + str(iflist[i])) fs = fcropstr if cropping else fstr fs = (os.path.splitext(os.path.basename(fs))[0] + os.extsep + "interface" + os.extsep + str(i + 1) + os.extsep + "pdb") spath = os.path.join(pisadir, fs) # inputmap = ckio.read(spath, 'pdb') inputmap = pio.read(spath, 'pdb', ck=True) if len(inputmap) == 4: chnames = [ iflist[i].chains[0].crystal_id, iflist[i].chains[1].crystal_id ] iflist[i].chains[0].seq_id = seq.whatseq(chnames[0]) iflist[i].chains[1].seq_id = seq.whatseq(chnames[1]) chseqs = [iflist[i].chains[0].seq_id, iflist[i].chains[1].seq_id] logger.info(iflist[i].chains) chtypes = [iflist[i].chains[0].type, iflist[i].chains[1].type] if (chseqs[0] != chseqs[1] or (chtypes[0] != 'Protein' or chtypes[1] != 'Protein')): if chtypes[0] != "Protein" or chtypes[1] != "Protein": logger.info( 'Interface ' + str(i) + ' is not a Protein-Protein interface. Ignoring.') else: logger.info('Interface ' + str(i) + ' is not a homodimer. Ignoring.') iflist[i].structure = None matches.append(None) continue s = chseqs[0] try: iflist[i].structure = [] for m in range(len(inputmap)): iflist[i].structure.append(inputmap[m].as_contactmap()) iflist[i].structure[m].id = inputmap[m].id except Exception: logger.warning('Contact Maps obtained from a legacy ConKit ' + 'version with no Distograms implemented.') for m in range(len(inputmap)): iflist[i].structure.append(inputmap[m]) # ConKit LEGACY. #fs = fcropstr if cropping else fstr #fs = (os.path.splitext(os.path.basename(fs))[0] + # os.extsep + "interface" + os.extsep + str(i+1) + os.extsep + "con") #spath = os.path.join(pisadir, fs) #pio.write(spath, 'psicov', indata=iflist[i].structure[1]) #iflist[i].contactmap = pio.read(spath, 'array') iflist[i].contactmap = iflist[i].structure[1].deepcopy() matches.append({}) for source, attribs in sources.items(): matches[i][source] = pcc.contact_atlas( name=pdbid + '_' + str(s), dimer_interface=iflist[i], conpredmap=conpred[s][source], conpredtype=source, sequence=seq.imer[s]) if cropping is True: matches[i][source].set_cropmap() matches[i][source].remove_neighbours(mindist=2) matches[i][source].set_conpred_seq() matches[i][source].remove_intra() matches[i][source].make_match(filterout=attribs[3]) for cmode, cmap in matches[i][source].conkitmatch.items(): if (len(cmap) > 0 and len( matches[i][source].interface.structure[1]) > 0): for imtype in plotformats: if len(matches[i][source].conkitmatch) > 1: pout = (os.path.splitext(fs)[0] + os.extsep + 'match' + os.extsep + cmode + os.extsep + source + os.extsep + 'con' + os.extsep + imtype) else: pout = (os.path.splitext(fs)[0] + os.extsep + 'match' + os.extsep + source + os.extsep + 'con' + os.extsep + imtype) plotpath = os.path.join( os.path.dirname(csvfile[0]), pout) matches[i][source].plot_map_alt(plotpath, mode=cmode, plot_type=imtype) else: iflist[i].structure = None iflist[i].contactmap = None matches.append(None) continue logger.info(os.linesep + 'Computing results and writing them to file...' + os.linesep) for i in range(len(iflist)): logger.info('Generating Interface ' + str(i + 1) + ' data...') if matches[i] is None: continue results = [pdbid, str(i + 1)] results.append(iflist[i].chains[0].crystal_id) results.append(iflist[i].chains[1].crystal_id) sid = iflist[i].chains[0].seq_id results.append(str(sid)) results.append(str(seq.imer[sid].length())) if cropping is True: results.append(str(seq.imer[sid].cropmsa.meff)) else: results.append(str(seq.imer[sid].msa.meff)) results.append(str(seq.imer[sid].ncrops())) results.append(str(seq.imer[sid].full_length())) results.append(str(seq.imer[sid].msa.meff)) for source, attribs in sources.items(): appresults = pcs.list_scores(matches[i][source], tag=source) results.extend(appresults) results.append(str(iflist[i].stable)) for n in range(2): if scoring[n] is True: pic.lineout(results, csvfile[n]) pic.lineout(results, invals['OUTCSVPATH'][n]) endmsg = pcl.ok(starttime, command=__script__) logger.info(endmsg) return
def main(): parser = create_argument_parser() args = parser.parse_args() global logger logger = pcl.pisacov_logger(level="info") welcomemsg, starttime = pcl.welcome(command=__script__) logger.info(welcomemsg) csvfile = ppaths.check_path(args.scores[0], 'file') outdir = ppaths.check_path(args.outdir) ppaths.mdir(outdir) # Parsing scores scores = {} names = None thraw = {} with open(csvfile, 'r') as fin: scoresin = csv.reader(fin) for entry in scoresin: if entry[0][0] != "#": if (names is None or isinstance(names, str) or (isinstance(names, list) and len(names) != len(entry))): names = [] for n in range(len(entry)): names.append('sc_' + str(n + 1)) else: if thraw == {}: for name in names[13:-1]: thraw[name] = [] if entry[0] not in scores: scores[entry[0]] = {} if entry[1] not in scores[entry[0]]: scores[entry[0]][entry[1]] = [] for sc in (entry.split(sep=', ')[13:-1]): scores[entry[0]][entry[1]].append(float(sc)) if (entry.split(sep=', ')[-1]) == 'True' or '1': scores[entry[0]][entry[1]].append(True) elif (entry.split(sep=', ')[-1]) == 'False' or '0': scores[entry[0]][entry[1]].append(False) for n in range(len(names)): thraw[names[n]].append(scores[entry[0]][entry[1]][n]) else: if entry.split( sep=', ')[13:] == scores[entry[0]][entry[1]]: pass else: raise ValueError( 'CSV file contains different values for same interface.' ) else: names = entry[1:].split(sep=', ') # Setting thresholds thr = {} FPR = {} TPR = {} for key, value in thraw.items(): thr[key] = list(set(thraw)).sort() FPR[key] = [] TPR[key] = [] for t in thr[key]: FP = 0 TP = 0 FN = 0 TN = 0 for pdbid in scores: for iface in scores[pdbid]: stable = scores[pdbid][iface][-1] for n in range(len(names)): if scores[pdbid][iface][n] < t: if stable is True: FN += 1 else: TN += 1 else: if stable is True: TP += 1 else: FP += 1 FPR[key].append(FP / (FP + TN)) TPR[key].append(TP / (TP + FN)) fnameout = os.path.join( outdir, (key + os.path.splitext(os.path.basename(csvfile))[0] + 'roc.dat')) with open(fnameout, 'w') as fout: for n in range(len(FPR[key])): fout.write(str(FPR[key][n]) + ' ' + str(TPR[key][n])) endmsg = pcl.ok(starttime, command=__script__) logger.info(endmsg) return