Exemplo n.º 1
0
 def components_provi(self,
                      color="",
                      translucent=0.0,
                      relpath=None,
                      max_atomno=None):
     if self.all_components:
         comps = self.get_components()
         if max_atomno:
             area = self.get_area(max_atomno=max_atomno)
             comps = [c for c in comps if area["ses"][c[0]]]
         if not relpath:
             relpath = self.relpath
         with open(self.datapath("_component.json"), "r") as fp:
             components_tmpl = fp.read()
         p = "tri_surface_([0-9]+)\.vert"
         components = []
         for m, filepath in dir_walker(self.output_dir, p):
             cno = int(m.group(1))
             if cno >= len(comps) or comps[cno][1] > 0:
                 continue
             components.append(
                 string.Template(components_tmpl).substitute(
                     vert_file=relpath(filepath),
                     insideout="true",
                     color=color,
                     translucent=translucent))
         if components:
             return ",\n" + ",\n".join(components)
     return ""
Exemplo n.º 2
0
 def prep(self):
     files = []
     #get all files from dir
     if len(self.pdb_files) == 1:
         v = ".*[pdb]$"
         for m, pdbfile in dir_walker(self.pdb_files[0], v):
             files.append(pdbfile)
     else:
         files = self.pdb_files
     if self.mapfile:
         fp2 = open(self.map_file, "w")
     self.new_files = []
     #generating a new pdb with only ATOM and if wanted selected chains
     for index, fi in enumerate(files):  #self.pdb_files):
         name = utils.path.stem(fi) + utils.path.ext(fi)
         if self.mapfile:
             fp2.write(fi.split("/")[-1] + " " + name + "\n")
         npdb = numpdb.NumPdb(fi)
         new_pdb = os.path.join(self.output_dir, name)
         if self.chain != "all" and self.chain[index] != "*":
             new = npdb.copy(**{
                 "record": "ATOM  ",
                 "chain": self.chain[index]
             })
         else:
             new = npdb.copy(**{"record": "ATOM  "})
         new.write(new_pdb)
         self.new_files.append(new_pdb)
     if self.mapfile:
         fp2.close()
Exemplo n.º 3
0
 def _pre_exec(self):
     if self.pdb_files != "":
         self.fasta()
         #merges all fasta files
         with open(self.fasta_file_out, "w") as fp:
             v = ".*[fst]$"
             for m, pdbfile in dir_walker(self.output_dir, v):
                 with open(pdbfile, "r") as fp3:
                     content = fp3.read()
                     fp.write(content)
                     fp.write("\n")
Exemplo n.º 4
0
 def stem_ini(self):
     #gets all stems of all files
     self.stem_list = []
     if len(self.pdb_input) == 1:
         v = ".*[pdb]$"
         for m, pdbfile in dir_walker(self.pdb_input[0], v):
             name = utils.path.stem(pdbfile) + utils.path.ext(pdbfile)
             self.stem_list.append(name)
     elif isinstance(self.pdb_input, list):
         for pdbfile in self.pdb_input:
             name = utils.path.stem(pdbfile) + utils.path.ext(pdbfile)
             self.stem_list.append(name)
Exemplo n.º 5
0
 def _pre_exec(self):
     p = "tri_surface_([0-9]+)\.(vert|face)"
     for m, filepath in dir_walker(self.output_dir, p):
         utils.path.remove(filepath)
     self.pdb2xyzr()
     if self.envelope:
         # calc envelope; append to self.pdb2xyzr.xyzr_file
         self.envelope_msms()
         vert = self.envelope_msms.get_vert(filt=True)
         pr = self.envelope_msms.probe_radius
         if self.envelope_hclust:
             coords = np.array([[d['x'], d['y'], d['z']] for d in vert])
             clust = hclust(coords, 2 * pr, method=self.envelope_hclust)
             print len(coords), len(clust)
             with open(self.pdb2xyzr.xyzr_file, "a") as fp:
                 for d in clust.itervalues():
                     d3 = np.array(d).mean(axis=0)
                     l = "%0.3f\t%0.3f\t%0.3f\t%0.2f\n" % (d3[0], d3[1],
                                                           d3[2], pr)
                     fp.write(l)
         else:
             with open(self.pdb2xyzr.xyzr_file, "a") as fp:
                 dct = {}
                 for d in vert:
                     coords = (d['x'], d['y'], d['z'])
                     if coords in dct:
                         continue
                     dct[coords] = True
                     l = "%0.3f\t%0.3f\t%0.3f\t%0.2f\n" % (d['x'], d['y'],
                                                           d['z'], pr)
                     fp.write(l)
         if self.atom_radius_add:
             with open(self.pdb2xyzr.xyzr_file, "r+") as fp:
                 lines = fp.readlines()
                 fp.seek(0)
                 fp.truncate()
                 for l in lines:
                     x, y, z, r = l.split()
                     l = "%s\t%s\t%s\t%0.2f\n" % (x, y, z, float(r) +
                                                  self.atom_radius_add)
                     fp.write(l)
Exemplo n.º 6
0
 def component_files(self):
     files = []
     p = "tri_surface_([0-9]+)\.(vert|face)"
     for m, filepath in dir_walker(self.output_dir, p):
         files.append(os.path.abspath(filepath))
     return files
Exemplo n.º 7
0
    def _post_exec(self):
        self.get_components()
        self._make_provi_file(pdb_file=self.relpath(self.pdb_file),
                              vert_file=self.relpath(self.vert_file),
                              color="",
                              components_data=self.components_provi())
        vert = self.get_vert()
        with open(self.surf_file, "w") as fp:
            for d in vert:
                l = "%0.3f\t%0.3f\t%0.3f\t%0.2f\n" % (d['x'], d['y'], d['z'],
                                                      self.probe_radius)
                fp.write(l)
        vert2 = filter(lambda x: x[-3] < 0, vert)
        with open(self.surf_file2, "w") as fp:
            fp.write("%i\ncomment\n" % len(vert2))
            for d in vert2:
                l = "F\t%0.3f\t%0.3f\t%0.3f\n" % (d['x'], d['y'], d['z'])
                fp.write(l)
        with open(self.surf_file3, "w") as fp:
            fp.write("%i\ncomment\n" % len(vert))
            for d in vert:
                l = "F\t%0.3f\t%0.3f\t%0.3f\n" % (d['x'], d['y'], d['z'])
                fp.write(l)
        if self.envelope_hclust:
            coords = np.array([[d['x'], d['y'], d['z']] for d in vert2])
            clust = hclust(coords,
                           2 * self.probe_radius,
                           method=self.envelope_hclust)
            with open(self.surf_file4, "w") as fp:
                fp.write("%i\ncomment\n" % len(clust))
                for d in clust.itervalues():
                    d3 = np.array(d).mean(axis=0)
                    l = "F\t%0.3f\t%0.3f\t%0.3f\n" % (d3[0], d3[1], d3[2])
                    fp.write(l)
        self.get_infos()
        v = "tri_surface(_?[0-9]*)\.(vert)"
        self.obj_list = []

        for m, vertfile in dir_walker(self.output_dir, v):
            facefile = vertfile[:-4] + 'face'
            meshfile = vertfile[:-4] + 'obj'
            num = meshfile.split('_')[-1].split('.')[0]
            a = self.get_vert(vertfile=vertfile)
            b = self.get_face(facefile=facefile)

            if num == "surface":
                face_vertex_to_obj(b, a, meshfile, flip=False)
            else:
                self.obj_list.append([int(num), meshfile])
                face_vertex_to_obj(b, a, meshfile, flip=True)

        # get the nrholes and the pymol script
        if self.all_components:
            area = parse_msms_area(self.area_file)
            mean_dct, neighbours, self.obj_list, last_hetresno, mean_lst = make_nrhole_pdb(
                self.pdb_file, area, self.nh_file, self.mean_file,
                self.pymol_file, self.obj_list)
            values_dict = {
                'neighbours': neighbours,
                'obj_list': self.obj_list,
                'mean_dct': mean_dct,
                'nh_file': self.nh_file,
                'TMPL_DIR': self.tmpl_dir,
                'pref': self.prefix,
                'last_hetresno': last_hetresno,
                'mean_list': mean_lst
            }
            self._make_file_from_tmpl(self.pymol_tmpl, **values_dict)
Exemplo n.º 8
0
    def _post_exec(self):
        #generates a file with the variances in the bfactor-field
        if self.dif_las:
            #writes variances into a vector
            variances = []
            with open(self.variances_file, "r") as fp:
                for line in fp:
                    if line.startswith("RES"):
                        line = line.split()
                        variances.append([line[2], line[3], line[4]])
            #gets the alignments needed for matching the variances to the residues
            aligns = []
            ids = []
            with open(self.muscle.alignment_file, "r") as fp:
                counter = 0
                alin = []
                for line in fp:
                    if line.startswith('>'):
                        fi = str.strip(line).split('>')[1]
                        if fi == self.sstem:
                            pos_leading = counter
                        ids.append(fi)
                        if counter != 0:
                            aligns.append(alin)
                        counter = counter + 1
                        alin = []
                    else:
                        for elem in str.strip(line):
                            alin.append(elem)
                aligns.append(alin)
            #generates a list containing a list and id for each file:
            # the list contains the var for each residue, depending,
            # if in the alignment of the leading structure (self.sstrem)
            # '-' or a residue is given, because there are only variances
            # for the leading structure

            #open question: how to set '-'? for very unequal files the variances
            # are very small - problems with comparison?
            rightordervarlist = []
            for fi in range(len(ids)):
                position = 0
                i = 0
                rightordervar = []
                for index, residue in enumerate(aligns[pos_leading]):
                    if residue == '-':
                        if aligns[fi][index] != '-':
                            rightordervar.append('    -1')
                    else:
                        if variances[position][1] == 1:
                            i = 1
                        if aligns[fi][index] != '-':
                            if str(i) == variances[position][1]:
                                var = "%.2f" % float(variances[position][2])
                                if len(var) < 6:
                                    leerz = " " * (6 - len(var))
                                    var = leerz + var
                                rightordervar.append(var)
                                position += 1
                            else:
                                rightordervar.append('    -1')
                        i += 1
                rightordervarlist.append([rightordervar, ids[fi]])
            #writes the variances into the *var.pdb-file
            with open(self.test_sup, "r") as fp, open(self.out_test_sup,
                                                      "w") as fp2:
                nums = []
                for lin in fp:
                    if lin.startswith('ATOM'):
                        act = lin[22:26]
                        if not (old == '' or act == old):
                            count += 1
                        old = act
                        var = mod_variances[count]
                        line = lin[:60] + var + lin[66:]
                        fp2.write(line)
                    elif lin.startswith('MODEL'):
                        model = nums[int(lin.split()[1]) - 1]
                        for mod in rightordervarlist:
                            if model == mod[1]:
                                mod_variances = mod[0]
                        count = 0
                        old = ''
                        fp2.write(lin)
                    elif lin.startswith('REMARK   MODEL'):
                        nums.append(lin.split()[3])
                        fp2.write(lin)
                    else:
                        fp2.write(lin)
            #sorts files into additional dir
            newout = self.subdir("additional_files")
            for m, pdbfile in dir_walker(self.output_dir, ""):
                stem = utils.path.stem(pdbfile) + utils.path.ext(pdbfile)
                if "muscle" in pdbfile or pdbfile.endswith(
                        self.prefix + "_var.pdb") or pdbfile.endswith(
                            self.prefix + "_sup.pdb") or pdbfile.endswith(
                                self.prefix +
                                "_sup_var.pdb") or pdbfile.endswith(
                                    "theseus_cmd.log") or pdbfile.endswith(
                                        "_variances.txt"):
                    pass
                else:
                    shutil.move(pdbfile, os.path.join(newout, stem))