Categories: BIO, C/C++ | Tags: | Views: 816

有时候总需要对一些pdb文件进行操作,特别是读数据,所以就写了这个库文件但是这个只是个比较粗略的,一时也不能补得很全,只是仅仅对ATOM的行进行了操作并进行了归类,其他的行暂时还没有进行操作,这些等到实际用到时,再进行补充吧 另外,关于atom_id和chain_id发现有些不是整型,因为在x-ray或者mnr测结构时无法确定而加的abcd之类的也没有考虑在内,如果要考虑这个的话,我想是否可以用map将id与整个原子的信息对应起来。 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef _PDB_H_  
#define _PDB_H_ 
 
#include<iostream> 
#include<fstream> 
#include<cstdio> 
#include<string>  
#include<vector>  
 
#define atoms vector<atom>  
#define chains vector<chain>  
#define aint vector<atom>::size_type  
#define cint vector<chain>::size_type   
 
using namespace std;   
 
class atom{   
private:   
    short atom_id;   
    string atom_nick;   
    string atom_res;   
    char atom_chain;   
    short atom_res_id;   
    float atom_x, atom_y, atom_z, atom_w, atom_bfactor;   
    char atom_name;   
 
public:   
    atom(short, string, string, char, short, float, float, float, float, float, char);   
    atom(string);   
    string line();   
};   
 
atom::atom( short a_id,   
            string a_nick,   
            string r,   
            char c,   
            short r_id,   
            float a_x,   
            float a_y,   
            float a_z,   
            float a_w,   
            float b,   
            char a_name){   
    atom_id = a_id;   
    atom_nick = a_nick;   
    atom_res = r;   
    atom_chain = c;   
    atom_res_id = r_id;   
    atom_x = a_x;   
    atom_y = a_y;   
    atom_z = a_z;   
    atom_w = a_w;   
    atom_bfactor = b;   
    atom_name = a_name;   
}   
 
atom::atom(string l){   
    atom_id = atoi(l.substr(6,5).c_str());   
    atom_nick = l.substr(13,3);   
    atom_res = l.substr(17,3);   
    atom_chain = l[21];   
    atom_res_id = atoi(l.substr(22,4).c_str());   
    atom_x = atof(l.substr(30,8).c_str());   
    atom_y = atof(l.substr(38,8).c_str());   
    atom_z = atof(l.substr(46,8).c_str());   
    atom_w = atof(l.substr(54,6).c_str());   
    atom_bfactor = atof(l.substr(60,6).c_str());   
    atom_name = l.size()>77 ? l[77] : ' ';   
}   
 
string atom::line(){   
    char line[255];   
    (void)sprintf(  line,   
                    "ATOM  %5u  %-3s %-3s %c%4u    %8.3f%8.3f%8.3f%6.2f%6.2f           %c",   
                    atom_id,   
                    atom_nick.c_str(),   
                    atom_res.c_str(),   
                    atom_chain,   
                    atom_res_id,   
                    atom_x,   
                    atom_y,   
                    atom_z,   
                    atom_w,   
                    atom_bfactor,   
                    atom_name);   
    return string(line);   
}   
 
//===============================================   
class chain{   
private:   
    atoms chain_atms;   
    char chain_label;   
public:   
    chain(char);   
    void addAtom(atom);   
    void saveToFile(string);   
    aint size();   
    char label();   
    atom operator[](aint);   
};   
 
chain::chain(char l){ chain_label = l; }   
 
aint chain::size(){   
    return chain_atms.size();   
}   
 
atom chain::operator[](aint i){   
    if(i>=0 and i<chain_atms.size())   
        return chain_atms[i];   
}   
 
char chain::label(){   
    return chain_label;   
}   
 
void chain::addAtom(atom a){   
    chain_atms.push_back(a);   
    return;   
}   
 
void chain::saveToFile(string f){   
    ofstream in(f.c_str());   
    for(cint i=0; i<chain_atms.size(); i++){   
        in << chain_atms[i].line() << endl;   
    }   
    in << "TER" << endl;   
    in.close();   
    return;   
}   
 
//===============================================   
class pdb{   
private:   
    chains pdb_chns;   
 
public:   
    pdb(string);   
    cint size();   
    chain operator[](cint);   
};   
 
cint pdb::size(){   
    return pdb_chns.size();   
}   
 
chain pdb::operator[](cint i){   
    if(i>=0 and i<pdb_chns.size()) return pdb_chns[i];   
}   
 
pdb::pdb(string pdbfile){   
    string l;   
    char flag, chain_label;   
    ifstream out1(pdbfile.c_str());   
    while(getline(out1, l)){    //get the chain labels first   
        if(l.substr(0,4) == "ATOM"){   
            chain_label = l[21];   
            if(flag != chain_label){   
                chain chn(chain_label);   
                pdb_chns.push_back(chn);   
                flag = chain_label;   
            }   
        }   
    }   
    out1.close();   
    ifstream out2(pdbfile.c_str());   
    while(getline(out2, l)){   
        if(l.substr(0,4) == "ATOM"){   
            atom atm(l);   
            chain_label = l[21];   
            for(cint i=0; i<pdb_chns.size(); i++){   
                if(chain_label == pdb_chns[i].label())   
                    pdb_chns[i].addAtom(atm);   
            }   
        }   
    }   
    out2.close();   
}  
#endif
这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明

2 trackbacks

  1. 操作pdb文件的类Perl版 | 迷途知返 Pingback | 2009/03/18
  2. cstdio Pingback | 2010/03/29
;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

你可以使用@somebody:开头, 来邮件通知somebody你回复了他的留言(用户名区分大小写).