File size: 4,415 Bytes
8efb4bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "CrossLink.h"

#include <boost/algorithm/string.hpp>

#include <iostream>

int readCrossLinkFile(const std::string& fileName,

                      std::vector<CrossLink>& crossLinks,

                      bool addReverse) {
  std::ifstream s(fileName);
  if (!s) {
    std::cerr << "Can't find cross links file " << fileName << std::endl;
    exit(0);
  }
  CrossLink cl;
  while (s >> cl) {
    if(! cl.isInList(crossLinks)) { // check for duplicates

      if(!addReverse) {
        // check reversed for duplicates
        CrossLink clReversed = cl.getReversed();
        if(! clReversed.isInList(crossLinks)) {
          crossLinks.push_back(cl);
        }
      } else {
        crossLinks.push_back(cl);
      }

    } else {
      // TODO: check if shorter distance restraint and replace if needed
      std::cerr << "Duplicate cross link " << cl << std::endl;
    }

    // add reverse
    if(addReverse) {
      CrossLink clReversed = cl.getReversed();
      if(! clReversed.isInList(crossLinks)) {
        crossLinks.push_back(clReversed);
      }
    }
  }
  return crossLinks.size();
}

void writeCrossLinkFile(const std::string& fileName,

                        const std::vector<CrossLink>& crossLinks) {
  std::ofstream ofile(fileName);
  for (unsigned int i = 0; i < crossLinks.size(); i++) {
    ofile << crossLinks[i];
    ofile << std::endl;
  }
  ofile.close();
}

void writeXLAnalizerFile(const std::string& fileName,

                         const std::vector<CrossLink>& crossLinks) {
  std::ofstream ofile(fileName);
  ofile << "id,Protein1,Protein2,AbsPos1,AbsPos2,score" << std::endl;
  for (unsigned int i = 0; i < crossLinks.size(); i++) {
    ofile << i+1 << "," << crossLinks[i].getChain1() << "," << crossLinks[i].getChain2() <<  ","
          << crossLinks[i].getResidue1() << "," << crossLinks[i].getResidue2()
          << ",100" << std::endl;
    ofile << std::endl;
  }
  ofile.close();
}

std::ostream& operator<<(std::ostream& s, const CrossLink& cl) {
  s << cl.residueNumber1_ << " ";
  if (cl.chainId1_ == " ")
    s << "-";
  else
    s << cl.chainId1_;

  s << " " << cl.residueNumber2_ << " ";
  if (cl.chainId2_ == " ")
    s << "-";
  else
    s << cl.chainId2_;
  s << " " << cl.minDistance_ << " " << cl.maxDistance_;
  return s;
}

std::istream& operator>>(std::istream& s, CrossLink& cl) {

  std::string line;
  std::getline(s, line);
  boost::trim(line); // remove spaces at the beginning/end of the line
  if(line.length()==0) return s;
  // skip comments
  if (line[0] == '#' || line[0] == '\0' || !isdigit(line[0])) return s;

  std::vector<std::string> split_results;
  boost::split(split_results, line, boost::is_any_of("\t "),
               boost::token_compress_on);

  if (split_results.size() >= 5 && split_results.size() <= 7) {
    cl.residueSequenceID1_ = split_results[0];
    cl.chainId1_ = split_results[1];
    cl.residueSequenceID2_ = split_results[2];
    cl.chainId2_ = split_results[3];
    cl.residueNumber1_ = std::stoi(cl.residueSequenceID1_);
    cl.residueNumber2_ = std::stoi(cl.residueSequenceID2_);
    cl.maxDistance_ = std::stof(split_results[4]);

    if(split_results.size() >= 6) {
      cl.minDistance_ = std::stof(split_results[4]);
      cl.maxDistance_ = std::stof(split_results[5]);
    }

    if(split_results.size() == 7) {
      cl.weight_ = std::stof(split_results[6]);
    }
  }

  if (cl.chainId1_ == "-") cl.chainId1_ = " ";
  if (cl.chainId2_ == "-") cl.chainId2_ = " ";
  //  std::cerr << cl << std::endl;
  return s;
}

bool CrossLink::operator==(const CrossLink& cl) const {
  return (residueNumber1_ == cl.residueNumber1_ && residueNumber2_ == cl.residueNumber2_ &&
          residueSequenceID1_ == cl.residueSequenceID1_ && residueSequenceID2_ == cl.residueSequenceID2_ &&
          chainId1_ == cl.chainId1_ && chainId2_ == cl.chainId2_);
}

bool CrossLink::isInList(const std::vector<CrossLink>& crossLinks) const {
  for(unsigned int i = 0; i<crossLinks.size(); i++)
    if(*this == crossLinks[i])
      return true;
  return false;
}

CrossLink CrossLink::getReversed() const {
  CrossLink reversed(residueNumber2_, chainId2_,

                     residueNumber1_, chainId1_,

                     minDistance_, maxDistance_, weight_);
  return reversed;
}