libpappsomspp
Library for mass spectrometry
grpgroup.cpp
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * Copyright (c) 2015 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
4  *
5  * This file is part of the PAPPSOms++ library.
6  *
7  * PAPPSOms++ is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * PAPPSOms++ is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Contributors:
21  * Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and
22  *implementation
23  ******************************************************************************/
24 
25 #include <QDebug>
26 #include "grppeptideset.h"
27 #include "grpsubgroup.h"
28 #include "grppeptide.h"
29 #include "grpsubgroupset.h"
30 #include "grpgroup.h"
31 #include "grpexperiment.h"
32 #include "../pappsoexception.h"
33 #include "../utils.h"
34 
35 using namespace pappso;
36 
38 {
39  m_peptideSet.addAll(grpSubGroupSp.get()->getPeptideSet());
40  m_subGroupList.push_back(grpSubGroupSp);
41  m_mapPeptideToSubGroupSet.add(grpSubGroupSp.get());
42 }
44  : m_subGroupList(other.m_subGroupList),
45  m_peptideSet(other.m_peptideSet),
46  m_mapPeptideToSubGroupSet(other.m_mapPeptideToSubGroupSet)
47 {
48 }
49 
51 {
52 }
53 
54 std::vector<GrpSubGroupSpConst>
56 {
57  std::vector<GrpSubGroupSpConst> subgroup_list;
58  for(GrpSubGroupSp subgroup : m_subGroupList)
59  {
60  subgroup_list.push_back(subgroup);
61  }
62  return subgroup_list;
63 };
64 
65 const std::list<GrpSubGroupSp> &
67 {
68  return m_subGroupList;
69 }
70 
71 const GrpPeptideSet &
73 {
74  return m_peptideSet;
75 }
76 unsigned int
78 {
79  return m_groupNumber;
80 }
81 const QString
83 {
84  if(m_groupNumber == 0)
85  {
86  return "";
87  }
88  return QString("%1").arg(Utils::getLexicalOrderedString(m_groupNumber));
89 }
90 bool
91 GrpGroup::operator<(const GrpGroup &other) const
92 {
93  return ((*(m_subGroupList.begin()->get())) <
94  (*(other.m_subGroupList.begin()->get())));
95 }
96 
99 {
100  return std::make_shared<GrpGroup>(*this);
101 }
102 
103 bool
104 GrpGroup::containsAny(const GrpPeptideSet &peptideSet) const
105 {
106 
107  return m_peptideSet.containsAny(peptideSet);
108 }
109 
110 void
111 GrpGroup::addGroup(GrpGroup *p_group_to_add)
112 {
113  if(this == p_group_to_add)
114  {
115  throw PappsoException(
116  QObject::tr("addGroup ERROR, this == p_group_to_add"));
117  }
118 
119  for(GrpSubGroupSp &sgToAdd : p_group_to_add->m_subGroupList)
120  {
121  addSubGroupSp(sgToAdd);
122  }
123 }
124 
125 void
127 {
128  qDebug() << "GrpGroup::check begin ";
129  GrpSubGroupSet impacted_subgroup_list;
131  impacted_subgroup_list);
132 
133  // if (impacted_subgroup_list.size() != this->m_subGroupList.size()) {
134  qDebug() << "GrpGroup::check impacted_subgroup_list.size() != "
135  "this->m_subGroupList.size()";
136  qDebug() << impacted_subgroup_list.printInfos();
137 
138  for(auto sg : m_subGroupList)
139  {
140  qDebug() << sg->getFirstAccession() << " " << sg.get();
141  }
142  //}
144  qDebug() << m_peptideSet.printInfos();
145  qDebug() << "GrpGroup::check end ";
146 }
147 
148 
149 void
151 {
152 
153  qDebug() << "GrpGroup::addSubGroupSp begin "
154  << grpSubGroupSp.get()->getFirstAccession();
155 
156 
157  // look for impacted subgroups (containing peptides alsoe present in incoming
158  // subgroup
159  GrpSubGroupSet impacted_subgroup_list;
160  m_mapPeptideToSubGroupSet.getSubGroupSet(grpSubGroupSp.get()->getPeptideSet(),
161  impacted_subgroup_list);
162  if(impacted_subgroup_list.contains(grpSubGroupSp.get()))
163  {
164  throw PappsoException(
165  QObject::tr("addSubGroupSp ERROR, subgroup %1 is already in group")
166  .arg(grpSubGroupSp.get()->getFirstAccession()));
167  }
168 
169  // look for impacted subgroup equal to incoming subgroup and merge it and exit
170  qDebug() << "GrpGroup::addSubGroupSp look for impacted subgroup equal to "
171  "incoming subgroup and merge it and exit";
172  auto it_impacted_sg = impacted_subgroup_list.m_grpSubGroupPtrList.begin();
173  auto it_impacted_sg_end = impacted_subgroup_list.m_grpSubGroupPtrList.end();
174  while(it_impacted_sg != it_impacted_sg_end)
175  {
176 
177  if((*it_impacted_sg)->merge(grpSubGroupSp.get()))
178  {
179  qDebug() << "GrpGroup::addSubGroupSp merge";
180  return;
181  }
182  it_impacted_sg++;
183  }
184 
185  // look for impacted subgroup including totally the incoming subgroup and exit
186  qDebug() << "GrpGroup::addSubGroupSp look for impacted subgroup including "
187  "totally the incoming subgroup and exit";
188  it_impacted_sg = impacted_subgroup_list.m_grpSubGroupPtrList.begin();
189  while(it_impacted_sg != it_impacted_sg_end)
190  {
191  if((*it_impacted_sg)->includes(grpSubGroupSp.get()))
192  {
193  qDebug() << "GrpGroup::addSubGroupSp includes";
194  return;
195  }
196  it_impacted_sg++;
197  }
198 
199  // look for impacted subgroup totally included in incoming subgroup and remove
200  // it
201  qDebug() << "GrpGroup::addSubGroupSp look for impacted subgroup totally "
202  "included in incoming subgroup and remove it";
203  it_impacted_sg = impacted_subgroup_list.m_grpSubGroupPtrList.begin();
204  while(it_impacted_sg != it_impacted_sg_end)
205  {
206  if(grpSubGroupSp.get()->includes((*it_impacted_sg)))
207  {
208  // this->removeSubGroup(*it_impacted_sg);
209  // it_impacted_sg = impacted_subgroup_list.erase(it_impacted_sg);
210  GrpSubGroup *sg_to_remove = *it_impacted_sg;
211  it_impacted_sg = impacted_subgroup_list.erase(it_impacted_sg);
212  m_mapPeptideToSubGroupSet.remove(sg_to_remove);
213  m_subGroupList.remove_if([sg_to_remove](GrpSubGroupSp &sub_groupSp) {
214  return (sg_to_remove == sub_groupSp.get());
215  });
216  // m_mapPeptideToSubGroupSet.check(m_subGroupList);
217  }
218  else
219  {
220  it_impacted_sg++;
221  }
222  }
223  qDebug()
224  << "GrpGroup::addSubGroupSp finally add the new subgroup to current group";
225 
226  // finally add the new subgroup to current group
227  m_subGroupList.push_back(grpSubGroupSp);
228  m_mapPeptideToSubGroupSet.add(grpSubGroupSp.get());
229  m_peptideSet.addAll(grpSubGroupSp.get()->getPeptideSet());
230 
231 
232  // check();
233  // m_mapPeptideToSubGroupSet.check(m_subGroupList);
234 
235  qDebug() << "GrpGroup::addSubGroupSp end";
236 }
237 
238 
239 void
241 {
242  qDebug() << "GrpGroup::setGroupNumber begin";
243  m_groupNumber = i;
244  for(auto &&sub_group_sp : m_subGroupList)
245  {
246  sub_group_sp->setGroupNumber(i);
247  }
249  qDebug() << "GrpGroup::setGroupNumber end";
250 }
251 
252 
253 void
255 {
256  qDebug() << "GrpGroup::numbering begin";
257 
258  for(auto &&sub_group_sp : m_subGroupList)
259  {
260  sub_group_sp.get()->numbering();
261  }
262  m_subGroupList.sort([](GrpSubGroupSp &first, GrpSubGroupSp &second) {
263  return ((*first.get()) < (*second.get()));
264  });
265  unsigned int i = 1;
266  for(auto &&sub_group_sp : m_subGroupList)
267  {
268  sub_group_sp.get()->setSubGroupNumber(i);
269  i++;
270  }
271 
273  qDebug() << "GrpGroup::numbering end";
274 }
275 
276 
277 bool
279 {
280  qDebug() << "GrpGroup::removeFirstNonInformativeSubGroup begin";
281  std::list<GrpSubGroup *> subgroup_list_to_remove;
282 
283  for(auto &&sub_group_sp : m_subGroupList)
284  {
285  if(m_mapPeptideToSubGroupSet.hasSpecificPeptide(sub_group_sp.get()))
286  {
287  }
288  else
289  {
290  subgroup_list_to_remove.push_back(sub_group_sp.get());
291  }
292  }
293  if(subgroup_list_to_remove.size() == 0)
294  {
295  qDebug() << "GrpGroup::removeFirstNonInformativeSubGroup end false";
296  return false;
297  }
298 
299  // sort subgroup to remove
300  subgroup_list_to_remove.sort([](GrpSubGroup *first, GrpSubGroup *second) {
301  return ((*first) < (*second));
302  });
303 
304  // remove the last one
305  qDebug() << "GrpGroup::removeFirstNonInformativeSubGroup "
306  "subgroup_list_to_remove.front()->peptideListSize() "
307  << subgroup_list_to_remove.front()->peptideListSize();
308  qDebug() << "GrpGroup::removeFirstNonInformativeSubGroup "
309  "subgroup_list_to_remove.back()->peptideListSize() "
310  << subgroup_list_to_remove.back()->peptideListSize();
311 
312  // the first subgroup is weaker (less peptides or less protein than others)
313  GrpSubGroup *sg_to_remove = subgroup_list_to_remove.front();
314  m_mapPeptideToSubGroupSet.remove(sg_to_remove);
315  qDebug()
316  << "GrpGroup::removeFirstNonInformativeSubGroup m_subGroupList.size() "
317  << m_subGroupList.size();
318  m_subGroupList.remove_if([sg_to_remove](GrpSubGroupSp &sub_groupSp) {
319  return (sg_to_remove == sub_groupSp.get());
320  });
321  qDebug() << "GrpGroup::removeFirstNonInformativeSubGroup after remove if "
322  "m_subGroupList.size() "
323  << m_subGroupList.size();
324  qDebug() << "GrpGroup::removeFirstNonInformativeSubGroup end true";
325  return true;
326 }
327 
328 bool
330 {
331  qDebug() << "GrpGroup::removeNonInformativeSubGroups begin";
333  {
335  {
336  qDebug() << "GrpGroup::removeNonInformativeSubGroups while";
337  }
338  }
339  else
340  {
341  return false;
342  }
343  return true;
344 }
GrpGroupSp makeGrpGroupSp()
Definition: grpgroup.cpp:98
void check() const
Definition: grpgroup.cpp:126
bool removeNonInformativeSubGroups()
Definition: grpgroup.cpp:329
void setGroupNumber(unsigned int i)
Definition: grpgroup.cpp:240
unsigned int getGroupNumber() const
Definition: grpgroup.cpp:77
bool containsAny(const GrpPeptideSet &peptideSet) const
Definition: grpgroup.cpp:104
bool removeFirstNonInformativeSubGroup()
Definition: grpgroup.cpp:278
std::list< GrpSubGroupSp > m_subGroupList
Definition: grpgroup.h:48
GrpGroup(GrpSubGroupSp &grpSubGroupSp)
Definition: grpgroup.cpp:37
unsigned int m_groupNumber
Definition: grpgroup.h:49
GrpMapPeptideToSubGroupSet m_mapPeptideToSubGroupSet
Definition: grpgroup.h:52
const GrpPeptideSet & getGrpPeptideSet() const
Definition: grpgroup.cpp:72
const std::list< GrpSubGroupSp > & getSubGroupSpList() const
Definition: grpgroup.cpp:66
void addSubGroupSp(const GrpSubGroupSp &grpSubGroupSp)
Definition: grpgroup.cpp:150
void addGroup(GrpGroup *p_group_to_add)
Definition: grpgroup.cpp:111
bool operator<(const GrpGroup &other) const
Definition: grpgroup.cpp:91
GrpPeptideSet m_peptideSet
Definition: grpgroup.h:50
const QString getGroupingId() const
Definition: grpgroup.cpp:82
std::vector< GrpSubGroupSpConst > getGrpSubGroupSpList() const
Definition: grpgroup.cpp:55
bool hasSpecificPeptide(const GrpSubGroup *get) const
tells if this subgroup contains a specific peptide
void getSubGroupSet(const GrpPeptideSet &peptide_set_in, GrpSubGroupSet &impacted_subgroup_set) const
get all subgroups concerned by a list of peptides
void remove(GrpSubGroup *p_remove_sub_group)
removes in the map all references of the group to remove (p_remove_sub_group)
void add(GrpSubGroup *p_add_sub_group)
add in the map all peptides of the subgroup to add
const QString printInfos() const
void addAll(const GrpPeptideSet &peptideSet)
bool containsAny(const GrpPeptideSet &peptideSet) const
void setGroupNumber(unsigned int i)
bool contains(GrpSubGroup *get) const
const QString printInfos() const
std::list< GrpSubGroup * >::iterator erase(std::list< GrpSubGroup * >::iterator it)
std::list< GrpSubGroup * > m_grpSubGroupPtrList
static const QString getLexicalOrderedString(unsigned int num)
Definition: utils.cpp:52
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< GrpSubGroup > GrpSubGroupSp
Definition: grpsubgroup.h:39
std::shared_ptr< GrpGroup > GrpGroupSp
Definition: grpgroup.h:38