outf = General.changeExt(pdbf, args.o)

	if args.wgap != None: # specific to gap
		assert args.conR == False, 'wgap and conR cannot be specified simultaneously'
		dirname = General.getBase(pdbf)
		pdbf = args.wgap + '/' + dirname + '/'+ pdbf

	index = PDB.findPositionInPDB(pdbf, resnum, cid)
	aacol = Analyze.readColumn(seqf, index, top = args.uplimit)

	if args.conR: # should contacting residue be constrained?
		conid = General.getBase(seqf).split('_')[-1]
		ccid, cresnum = conid[0], conid[1:]
		cindex = PDB.findPositionInPDB(pdbf, cresnum, ccid)
		cres = PDB.getResByInd(pdbf, ccid, cresnum).getResname()
		cres = PDB.t2s(cres)
		caacol = Analyze.readColumn(seqf, cindex, top = args.uplimit)

	if args.env != None: # environment corrected counts
		envf = General.getBase(seqf.replace(args.head, args.envhead)) + '.' + args.env
		if not os.path.isfile(envf):
			print(envf + ' doesn\'t exist!')
			continue

		### this is a dynamic part
		if not args.conR:
			envcolInd = range(2, 7)
			envcols = Analyze.readMultiColumn(envf, envcolInd, top = args.uplimit)
			if not (len(aacol) == len(envcols[0])):
				envcols = [x[:len(aacol)] for x in envcols]
dirs = [x for x in os.listdir('.') if os.path.isdir(x)]
dirs.sort()
odir = os.getcwd()

for d in dirs:
    os.chdir(odir)
    os.chdir(d)
    pdbs = glob.glob('*.pdb')
    if len(pdbs) <= 1:
        continue
    pdbid = d.split('_')[0].lower()
    opdbf = args.sdir + '/' + pdbid + '.clean.pdb'

    cen = d.split('_')[1]
    cenres = PDB.getResByInd(opdbf, cen[0], cen[2:])
    if cenres.getResname() == 'GLY':
        carbon = 'CA'
    else:
        carbon = 'CB'
    cencoords = cenres.getAtom(carbon).getCoords()

    out = d + '.' + args.o
    outfh = open(out, 'w')
    cons = []
    for pdb in pdbs:
        con = General.getBase(pdb).split('_')[-1]
        cons.append([con[0], con[1:]])
    for i in range(len(cons)):
        conres = PDB.getResByInd(opdbf, cons[i][0], cons[i][1])
        carbon = 'CB'