1 //
2 //  proteindb.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 6/3/21.
6 //  Copyright © 2021 Schloss Lab. All rights reserved.
7 //
8 
9 #include "proteindb.hpp"
10 
11 /***********************************************************************/
12 
ProteinDB()13 ProteinDB::ProteinDB() : StorageDatabase() { }
14 /***********************************************************************/
15 //the clear function free's the memory
~ProteinDB()16 ProteinDB::~ProteinDB() { data.clear(); }
17 
18 /***********************************************************************/
19 
ProteinDB(int newSize)20 ProteinDB::ProteinDB(int newSize) : StorageDatabase() {  data.resize(newSize, Protein()); }
21 
22 /***********************************************************************/
23 
ProteinDB(ifstream & filehandle)24 ProteinDB::ProteinDB(ifstream& filehandle) : StorageDatabase() {
25     try{
26 
27         //read through file
28         while (!filehandle.eof()) {
29 
30             Protein newProteinSequence(filehandle);  util.gobble(filehandle);
31 
32             if (newProteinSequence.getName() != "") {
33                 if (length == 0) { length = newProteinSequence.getAligned().size(); }
34                 if (length != newProteinSequence.getAligned().size()) { samelength = false;  }
35                 data.push_back(newProteinSequence);
36             }
37         }
38 
39         filehandle.close();
40     }
41     catch(exception& e) {
42         m->errorOut(e, "ProteinDB", "ProteinDB");
43         exit(1);
44     }
45 }
46 /***********************************************************************/
47 
getNumSeqs()48 int ProteinDB::getNumSeqs() { return data.size(); }
49 
50 /***********************************************************************/
getProt(int index)51 Protein ProteinDB::getProt(int index) {
52     if ((index >= 0) && (index < data.size()) ) { return data[index]; }
53     else { m->mothurOut("[ERROR]: invalid database index, please correct.\n"); m->setControl_pressed(true); Protein p; return p; }
54 }
55 /***********************************************************************/
56 
push_back(Protein newProteinSequence)57 void ProteinDB::push_back(Protein newProteinSequence) {
58     try {
59         if (length == 0) { length = newProteinSequence.getAligned().size(); }
60         if (length != newProteinSequence.getAligned().size()) { samelength = false; }
61 
62         data.push_back(newProteinSequence);
63     }
64     catch(exception& e) {
65         m->errorOut(e, "ProteinDB", "push_back");
66         exit(1);
67     }
68 }
69 /***********************************************************************/
70 
print(string outputFileName)71 void ProteinDB::print(string outputFileName) {
72     try {
73         ofstream out; util.openOutputFile(outputFileName, out);
74 
75         for (int i = 0; i < data.size(); i++) {
76             data[i].printProtein(out);
77         }
78         out.close();
79     }
80     catch(exception& e) {
81         m->errorOut(e, "ProteinDB", "print");
82         exit(1);
83     }
84 }
85 /***********************************************************************/
86 
87