forked from pylelab/USalign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addChainID.cpp
129 lines (117 loc) · 3 KB
/
addChainID.cpp
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
#include <fstream>
#include <map>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include "pstream.h"
using namespace std;
void print_help()
{
cout <<
"Add chain ID to PDB format file.\n"
"\n"
"Usage: addChainID input.pdb output.pdb chainID\n"
<<endl;
exit(EXIT_SUCCESS);
}
void splitlines(const string &line, vector<string> &lines,
const char delimiter='\n')
{
bool within_word = false;
for (size_t pos=0;pos<line.size();pos++)
{
if (line[pos]==delimiter)
{
within_word = false;
continue;
}
if (!within_word)
{
within_word = true;
lines.push_back("");
}
lines.back()+=line[pos];
}
}
void addChainID(const string &infile,const string &outfile,
const string &chainID)
{
stringstream buf;
if (infile=="-") buf<<cin.rdbuf();
#if defined(REDI_PSTREAM_H_SEEN)
else if (infile.size()>3 && infile.substr(infile.size()-3)==".gz")
{
redi::ipstream fp_gz; // if file is compressed
fp_gz.open("gunzip -c "+infile);
buf<<fp_gz.rdbuf();
fp_gz.close();
}
#endif
else
{
ifstream fp;
fp.open(infile.c_str(),ios::in); //ifstream fp(filename,ios::in);
buf<<fp.rdbuf();
fp.close();
}
vector<string> lines;
splitlines(buf.str(),lines);
buf.str(string());
size_t l;
for (l=0;l<lines.size();l++)
{
if (lines[l].substr(0,6)=="ATOM " ||
lines[l].substr(0,6)=="HETATM")
{
if (lines[l].size()<22)
{
cerr<<"incomplete:"<<lines[l]<<endl;
continue;
}
buf<<lines[l].substr(0,20)<<chainID<<lines[l].substr(22)<<endl;
}
else if (lines[l].size())
buf<<lines[l]<<endl;
lines[l].clear();
}
if (outfile=="-")
cout<<buf.str();
else
{
ofstream fout;
fout.open(outfile.c_str(),ios::out);
fout<<buf.str();
fout.close();
}
buf.str(string());
vector<string>().swap(lines);
return;
}
int main(int argc, char *argv[])
{
if (argc < 2) print_help();
string infile ="";
string outfile="";
string chainID="";
for (int i=1; i<argc; i++)
{
if ( infile.size()==0) infile =argv[i];
else if (outfile.size()==0) outfile=argv[i];
else if (chainID.size()==0) chainID=argv[i];
else
{
cerr<<"ERROR! no such option "<<argv[i]<<endl;
exit(1);
}
}
if (outfile.size()==0) outfile="-";
if (chainID.size()==0) chainID=" ";
else if (chainID.size()==1) chainID=" "+chainID;
else if (chainID.size()>2) chainID=chainID.substr(chainID.size()-2,2);
addChainID(infile,outfile,chainID);
infile.clear();
outfile.clear();
return 0;
}