Exemplo n.º 1
0
def write(infile, ftype, indata, ck=False):
    """

    :param infile: Path to input file.
    :type infile: str
    :param ftype: One of 'psicov', 'ccmpred', 'fasta', 'pdb', 'a3m', 'jones', 'xml'.
    :type ftype: str
    :param ck: Open alternative conkit version instead of default, defaults to False.
    :type ck: bool, optional
    :return: Parsed file (and, for 'pdb', list of filenames).
    :rtype: One or two of list[str], :class:`~crops.elements.sequences.sequence`, :class:`~conkit.core.sequence.Sequence`,

    """
    if (ftype.lower() not in _ftypelist() or
            isinstance(ftype, str) is not True):
        logging.critical('Specified type not valid.')
        raise ValueError

    if ck is True and ftype.lower() != 'xml':
        output = ckio.write(infile, ftype.lower(), hyerarchy=indata)
    else:
        if ftype.lower() == 'psicov':
            pass
        if ftype.lower() == 'ccmpred':
            pass
        elif ftype.lower() == 'fasta':
            output = cps.parseseqfile(infile)
        elif ftype.lower() == 'pdb':
            output1, output2 = cps.parsestrfile(infile)
            return output1, output2
        elif ftype.lower() == 'a3m' or 'jones':
            output = ckio.read(infile, ftype.lower())
        elif ftype.lower() == 'xml':
            output = ET.parse(infile)
    return output
Exemplo n.º 2
0
def msafilesgen(inpath_a3m):
    """
    Convert a3m format alignment to "jones" format (.aln) and print msa coverage graph

    Parameters
    ----------
    inpath_a3m : str
        Multiple Sequence Alignment (.a3m) path

    Returns
    -------
    parsemsa : msa
        ConKit parsed MSA.
    outpath_aln : str
        MSA (jones format) file path.

    """
    outpath_aln = os.path.join(os.path.splitext(inpath_a3m)[0] + ".aln")

    biotag = os.path.splitext(os.path.splitext(inpath_a3m)[0])[1]

    msacoveragefile = pdbid(
    ) + biotag + ".msa.coverage.png" if biotag == ".bio" else pdbid(
    ) + ".msa.coverage.png"
    msacoveragepath = os.path.join(output_dir(), msacoveragefile)

    parsemsa = ckio.read(inpath_a3m, 'a3m')
    ckio.write(outpath_aln, 'jones', parsemsa)

    ckplot.SequenceCoverageFigure(parsemsa, file_name=msacoveragepath)

    return parsemsa, outpath_aln
Exemplo n.º 3
0
def _compute_single(args):
    decoy, decoy_format, cmap = args
    dmap = read(decoy, decoy_format).top_map
    matched = cmap.match(dmap)
    shortrange = matched.short_range_contacts
    mediumrange = matched.medium_range_contacts
    longrange = matched.long_range_contacts
    sprec, mprec, lprec = float("NaN"), float("NaN"), float("NaN")
    if shortrange.ncontacts > 0:
        sprec = shortrange.precision
    if mediumrange.ncontacts > 0:
        mprec = mediumrange.precision
    if longrange.ncontacts > 0:
        lprec = longrange.precision
    return sprec, mprec, lprec
Exemplo n.º 4
0
def msafilesgen(inpath_a3m):
    """Convert a3m format alignment to "jones" format (.aln) and print msa coverage graph.

    :param inpath_a3m: Multiple Sequence Alignment (.a3m) path
    :type inpath_a3m: str
    :return: ConKit parsed MSA
    :rtype: :obj:`~conkit.core.sequencefile.SequenceFile`

    """
    parsedmsa = ckio.read(inpath_a3m, 'a3m')

    # Convert to 'jones' format
    outpath_aln = os.path.join(os.path.splitext(inpath_a3m)[0], ".aln")
    ckio.write(outpath_aln, 'jones', parsedmsa)

    # Plot Coverage
    msacoveragepath = os.path.join(
        os.path.splitext(inpath_a3m)[0], ".coverage.png")
    fig = ckplot.SequenceCoverageFigure(parsedmsa)
    fig.savefig(msacoveragepath, overwrite=True)

    neff = parsedmsa.meff

    return neff
Exemplo n.º 5
0
def main():
    starttime = time.time()
    parser = create_argument_parser()
    args = parser.parse_args()

    global logger
    logger = pcl.pisacov_logger(level="info")
    logger.info(pcl.welcome())

    # READ INPUT ARGUMENTS
    if args.initialise is not None:
        inseq = pio.check_path(args.initialise[0], 'file')
        instr = pio.check_path(args.initialise[1], 'file')
        indb = pio.check_path(pio.conf.CSV_CHAIN_PATH, 'file')
        skipexec = [False, True] if not args.add_noncropped else [False, False]
        scoring = [True, False] if not args.add_noncropped else [True, True]
    elif args.skip_conpred is not None:
        inseq = pio.check_path(args.skip_conpred[0], 'file')
        instr = pio.check_path(args.skip_conpred[1], 'file')
        skipexec = [True, True]
        scoring = [True, False] if not args.add_noncropped else [True, True]
    elif args.skip_default_conpred is not None:
        inseq = pio.check_path(args.skip_default_conpred[0], 'file')
        instr = pio.check_path(args.skip_default_conpred[1], 'file')
        skipexec = [True, True] if not args.add_noncropped else [True, False]
        scoring = [True, False] if not args.add_noncropped else [True, True]

    if args.outdir is None:
        outrootdir = pio.check_path(os.path.dirname(inseq))
    else:
        outrootdir = pio.check_path(os.path.join(args.outdir[0], ''))
    pio.mdir(outrootdir)

    if args.collection_file is None:
        outcsvfile = pio.check_path(
            os.path.join(outrootdir, "pisacov_data.csv"))
    else:
        outcsvfile = pio.check_path(args.collection_file[0])
    try:
        pio.check_path(outcsvfile, 'file')
        csvexists = True
    except:
        csvexists = False

    if args.uniprot_threshold is not None:
        thuprot, dbuprot = pio.check_uniprot(args.uniprot_threshold[0])
    else:
        thuprot = 0.0
        dbuprot = None

    if args.hhparams is not None:
        hhparameters = pio.check_hhparams(args.hhparams)
    else:
        try:
            hhparameters = pio.check_hhparams(pio.conf.HHBLITS_PARAMETERS)
        except:
            hhparameters = pio.check_hhparams('dmp')

    # Define formats used
    sources = pio.paths.sources()
    n_sources = len(sources)

    # Parse sequence and structure files
    logger.info('Parsing sequence file...')
    seq = cps.parseseqfile(inseq)
    if len(seq) == 1:
        for key in seq.keys():
            pdbid = key.lower()
            if len(seq[key].imer) == 1:
                for key2 in seq[key].imer[key2]:
                    chid = key2
            else:
                raise Exception('More than one pdbid in sequence set.')
    else:
        raise Exception('More than one pdbid in sequence set.')

    logger.info('Parsing structure file...')
    structure = cps.parsestrfile(instr)[0][pdbid]

    #logger.info('Parsing SIFTS database file...')
    #sifts = cps.import_db(indb, pdb_in=pdbid)

    # CROPPING AND RENUMBERING
    if not skipexec[0]:
        logger.info(
            'Cropping and renumbering sequences, structures according to SIFTS database.'
        )
        psys.crops.runcrops(inseq, instr, indb, thuprot, dbuprot, outrootdir)

        outpdbdir = os.path.join(outrootdir, pdbid, "")
        pio.mdir(outpdbdir)

        inseqc = os.path.join(outpdbdir, os.path.basename(inseq))
        instrc = os.path.join(outpdbdir, os.path.basename(instr))

        copyfile(inseq, inseqc)
        copyfile(instr, instrc)

        cmappath = os.path.join(os.path.splitext(inseqc), '.cropmap')

    # MSA GENERATOR
    cseqpath = os.path.join(outpdbdir, pdbid + '.crops.to_uniprot.fasta')
    hhdir = os.path.join(outpdbdir, 'hhblits', '')
    pio.mdir(hhdir)
    neff = {}
    if not skipexec[0] or not skipexec[1]:
        if hhparameters == ['3', '0.001', 'inf', '50', '99']:
            logger.info(
                'Generating Multiple Sequence Alignment using DeepMetaPSICOV default parameters... [AS RECOMMENDED]'
            )
        elif hhparameters == ['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...'
            )

        if os.path.isfile(cmappath) and not skipexec[0]:
            psys.msagen.runhhblits(cseqpath, hhparameters, hhdir)
            cmsaa3mfile = os.path.splitext(
                os.path.basename(cseqpath))[0] + ".msa.a3m"
            cmsaa3mpath = os.path.join(hhdir, cmsaa3mfile)
            neff['cropped'] = psys.msagen.msafilesgen(cmsaa3mpath)
            neff['original'] = None
            if not skipexec[1]:
                logger.info(
                    '    Repeating process for non-default sequence...')
                neff['cropped'] = None
        else:
            logger.info(
                '    No cropped sequence found, using original sequence instead...'
            )

        if not os.path.isfile(cmappath) or not skipexec[1]:
            psys.msagen.runhhblits(inseq, hhparameters, hhdir)
            msaa3mfile = os.path.splitext(
                os.path.basename(inseq))[0] + ".msa.a3m"
            msaa3mpath = os.path.join(hhdir, msaa3mfile)
            neff['original'] = psys.msagen.msafilesgen(msaa3mpath)

    # DEEP META PSICOV RUN
    if not skipexec[0] or not skipexec[1]:
        logger.info(
            'Generating contact prediction lists via DeepMetaPSICOV...')
        dmpdir = os.path.join(outpdbdir, 'dmp', '')
        pio.mdir(dmpdir)
        if os.path.isfile(cmappath) and not skipexec[0]:
            psys.dmp.rundmp(cseqpath, cmsaa3mpath, dmpdir)
            if not skipexec[1]:
                logger.info(
                    '    Repeating process for non-default sequence...')
        else:
            logger.info(
                '    No cropped sequence found, using original sequence instead...'
            )

        if not os.path.isfile(cmappath) or not skipexec[1]:
            psys.dmp.rundmp(inseq, msaa3mpath, dmpdir)

    # INTERFACE GENERATION, PISA
    cstrpath = os.path.join(outpdbdir, pdbid + '.oldids.crops.to_uniprot.pdb')
    pisadir = os.path.join(outpdbdir, 'pisa', '')
    n_ifaces = {}
    if not skipexec[0] or not skipexec[1]:
        logger.info('Generating interface files via PISA...')

        if os.path.isfile(cmappath) and not skipexec[0]:
            n_ifaces['cropped'] = psys.pisa.runpisa(cstrpath, pisadir)
            if not skipexec[1]:
                logger.info(
                    '    Repeating process for non-default sequence...')
        else:
            n_ifaces['cropped'] = None
            logger.info(
                '    No cropped sequence found, using original sequence instead...'
            )

        if not os.path.isfile(cmappath) or not skipexec[1]:
            n_ifaces['original'] = psys.pisa.runpisa(instr, pisadir)
        else:
            n_ifaces['original'] = None

    # CONTACT ANALYSIS AND MATCH
    resultdir = os.path.join(outpdbdir, 'results', '')
    logger.info('Opening output csv files...')
    pdbcsvfile = os.path.join(resultdir, pdbid + ".evcovsignal.csv")
    pio.outcsv.csvheader(pdbcsvfile)
    if not csvexists:
        pio.outcsv.csvheader(outcsvfile)

    logger.info('Parsing contact predictions lists...')
    ckseq = ckio.read(inseq, 'fasta')
    conpred = {}
    for source, attribs in sources.items():
        for mode in ['cropped', 'original']:
            seqfile = cseqpath if mode == 'cropped' else inseq
            confile = (os.path.splitext(os.path.basename(seqfile))[0] +
                       attribs[1])
            conpath = os.path.join(outpdbdir, attribs[0], confile)
            cropmapping = cps.parsemapfile(cmappath)
            if os.path.isfile(conpath):
                conpred[mode][source] = ckio.read(conpath, attribs[2])[0]
                for contact in conpred[mode][source]:
                    contact.res1_seq = cropmapping[pdbid][chid]['cropbackmap'][
                        contact.res1_seq]
                    contact.res2_seq = cropmapping[pdbid][chid]['cropbackmap'][
                        contact.res2_seq]
            else:
                conpred[mode][source] = None

                # NOT SURE IT IS WORKING WITH SEVERAL CHAINS OF SAME SEQUENCE. CHECK EVERYTHING.

    logger.info('    Parsing PISA interfaces...')
    interfaces = {}

    for mode in ['cropped', 'original']:
        if n_ifaces[mode] is not None:
            interfaces[mode] = []
            for i in range(int(n_ifaces[mode])):
                strfile = cstrpath if mode == 'cropped' else instr
                pdbfilei = os.path.splitext(strfile)[0] + ".interface." + str(
                    i + 1) + ".pdb"
                interfaces[mode].append(ckio.read(pdbfilei, 'pdb'))
        else:
            interfaces[mode] = None

    # OUTPUT


# CODE TO PRINT NON-REPEATED LINES
#    import csv
#    rows = csv.reader(open("file.csv", "rb"))
#    newrows = []
#    for row in rows:
#        if row not in newrows:
#            newrows.append(row)
#    writer = csv.writer(open("file.csv", "wb"))
#    writer.writerows(newrows)

    return
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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