1 /*
2 * Copyright (C) 2013 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "VcfRecordDiscardRules.h"
19
reset()20 void VcfRecordDiscardRules::reset()
21 {
22 myExcludeIDs.clear();
23 myIncludeIDs.clear();
24 myNumDiscarded = 0;
25 }
26
27
setExcludeIDs(const char * filename)28 bool VcfRecordDiscardRules::setExcludeIDs(const char* filename)
29 {
30 return(setIDs(myExcludeIDs, filename));
31 }
32
33
setIncludeIDs(const char * filename)34 bool VcfRecordDiscardRules::setIncludeIDs(const char* filename)
35 {
36 return(setIDs(myIncludeIDs, filename));
37 }
38
39
discardForID(std::string & myID)40 bool VcfRecordDiscardRules::discardForID(std::string& myID)
41 {
42 if(!myExcludeIDs.empty())
43 {
44 if(myExcludeIDs.find(myID) != myExcludeIDs.end())
45 {
46 // The ID is in the exclude list,
47 // so return true, discard the record.
48 // increment the discard counter.
49 ++myNumDiscarded;
50 return(true);
51 }
52 }
53 else if(!myIncludeIDs.empty())
54 {
55 if(myIncludeIDs.find(myID) == myIncludeIDs.end())
56 {
57 // The ID is not in the include list,
58 // so return false, discard the record.
59 // increment the discard counter.
60 ++myNumDiscarded;
61 return(true);
62 }
63 }
64 return(false);
65 }
66
67
setIDs(IDList & idlist,const char * filename)68 bool VcfRecordDiscardRules::setIDs(IDList& idlist, const char* filename)
69 {
70 // Open the file nad read in all the exclude ids.
71 IFILE idFile = ifopen(filename, "r");
72 if(idFile == NULL)
73 {
74 return(false);
75 }
76 std::string line;
77 while(idFile->readLine(line) == 0)
78 {
79 idlist.insert(line);
80 line.clear();
81 }
82 if(!line.empty())
83 {
84 idlist.insert(line);
85 line.clear();
86 }
87 return(true);
88 }
89