Debian Codegraph @

main.cpp

raw symbols SCIP
1 // PatMaN DNA pattern matcher
2 // (C) 2007 Kay Pruefer, Udo Stenzel
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 2 of the License, or (at
7 // your option) any later version. See the LICENSE file for details.
8
9
10 #include <iostream>
11 #include <string>
12 #include <vector>
13 #include <fstream>
14 #include <cstdlib>
15 #include <cstring>
16
17 #include <popt.h>
18
19 #include "prefix_tree.h"
20 #include "fasta.h"
21
22 using namespace std ;
23
24 // global variables
25 size_t cutoff = 2 ;
26 size_t allow_gaps = 0 ;
27 size_t minlength = 0 ;
28 long long total_nodes = 0 ;
29 int only_plus_strand = 0 ;
30 int discount_adenine = 0 ;
31 int ambi_codes = 0 ;
32 int chop5 = 0, chop3 = 0 ;
33 int debug_flags = debug_notquiet ;
34 int do_prefetch = 3 ;
35 ostream *output = 0 ;
36
37 long position_genome ;
38 const char* genome_name ;
39
40 enum option_flag { opt_none, opt_db, opt_pat, opt_output, opt_version } ;
41
42 struct poptOption options[] = {
43 { "version", 'V', POPT_ARG_NONE, 0, opt_version, "Print version number and exit", 0 },
44 { "edits", 'e', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &cutoff, 0, "Set maximum edit distance to N", "N" },
45 { "gaps", 'g', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &allow_gaps, 0, "Set maximum number of gaps to N", "N" },
46 { "databases", 'D', POPT_ARG_NONE, 0, opt_db, "Following files are databases", 0 },
47 { "patterns", 'P', POPT_ARG_NONE, 0, opt_pat, "Following files contain patterns", 0 },
48 { "output", 'o', POPT_ARG_STRING, 0, opt_output, "Write output to FILE", "FILE" },
49 { "ambicodes", 'a', POPT_ARG_NONE, &ambi_codes, 0, "Interpret ambiguity codes in patterns", 0 },
50 { "singlestrand", 's', POPT_ARG_NONE, &only_plus_strand, 0, "Do not match reverse-complements", 0 },
51 { "prefetch", 'p', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT, &do_prefetch, 0, "Prefetch N nodes", "N" },
52 { "min-length", 'l', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &minlength, 0, "Only consider patterns longer than N", "N" },
53 { "chop-3", 'x', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &chop3, 0, "Chop N bp off the 3' end of patterns", "N" },
54 { "chop-5", 'X', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &chop5, 0, "Chop N bp off the 5' end of patterns", "N" },
55 { "adenine-hack", 'A', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &discount_adenine, 0, "Ignore inserted adenines in db", 0 },
56 { "quiet", 'q', POPT_ARG_NONE | POPT_BIT_CLR, &debug_flags, debug_notquiet, "Turn off warnings", 0 },
57 { "verbose", 'v', POPT_ARG_NONE | POPT_BIT_SET, &debug_flags, debug_verbose, "Print additional progress reports", 0 },
58 { "debug", 'd', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &debug_flags, 0, "Set debug flags to N (see man page)", "N" },
59 POPT_AUTOHELP POPT_TABLEEND
60 } ;
61
62
63 int main(int argc, const char *argv[]) {
64
65 std::vector<std::string> dbfiles, patfiles ;
66
67 poptContext pc = poptGetContext( "patman", argc, argv, options, 0 ) ;
68 poptSetOtherOptionHelp( pc, "[OPTION...] [input-file...]" ) ;
69 poptReadDefaultConfig( pc, 0 ) ;
70 bool isdbfiles = false ;
71
72 if( argc <= 1 ) { poptPrintHelp( pc, stderr, 0 ) ; return 1 ; }
73 int rc = poptGetNextOpt(pc) ;
74 for(;;) {
75 if( rc == opt_version ) {
76 std::cout << poptGetInvocationName(pc) << ", revision " << VERSION << std::endl ;
77 return 0 ;
78 }
79 else if( rc == opt_db || rc == opt_pat || rc == -1 ) {
80 while( poptPeekArg(pc) )
81 ( isdbfiles ? dbfiles : patfiles ).push_back( poptGetArg(pc) ) ;
82 isdbfiles = (rc == opt_db) ;
83 }
84 else if( rc == opt_output )
85 {
86 delete output ;
87 const char *arg = poptGetOptArg(pc) ;
88 if( 0 == strcmp( arg, "-" ) ) output = 0 ;
89 else output = new std::ofstream( arg ) ;
90 }
91 else {
92 std::cerr << argv[0] << ": " << poptStrerror( rc )
93 << ' ' << poptBadOption( pc, 0 ) << std::endl ;
94 return 1 ;
95 }
96 if( rc == -1 ) break ; else rc = poptGetNextOpt(pc) ;
97 }
98
99 if( dbfiles.empty() )
100 {
101 dbfiles.push_back( "-" ) ;
102 if( debug_flags & debug_notquiet )
103 clog << "No databases given, using stdin." << endl ;
104 }
105 if( patfiles.empty() )
106 {
107 clog << "No patterns given, aborting." << endl ;
108 exit(1) ;
109 }
110
111 prefix_tree trie ;
112 for( size_t i = 0 ; i != patfiles.size() ; ++i )
113 {
114 ifstream *infile = 0 ;
115 if( patfiles[i] != "-" ) {
116 infile = new ifstream( patfiles[i].c_str() ) ;
117 if( debug_flags & debug_verbose ) clog << "reading patterns from " << patfiles[i] << endl ;
118 if( !infile || !*infile ) { cerr << "cannot read " << patfiles[i] << endl ; return 1 ; }
119 }
120 else if( debug_flags & debug_verbose ) clog << "reading patterns from STDIN" << endl ;
121
122 // add pattern to prefix tree
123 fasta_fac f( infile ? *infile : cin ) ;
124 fasta* ff ;
125 while ( (ff = f.get()) && !ff->null ) {
126 if( minlength <= ff->seq.size() )
127 trie.add_seq(
128 ff->seq.substr( chop5, ff->seq.size() - chop5 - chop3 ),
129 ff->headerline, !!ambi_codes ) ;
130 delete ff ;
131 }
132 delete infile ;
133 }
134
135 if( debug_flags & debug_verbose ) clog << "post-processing trie" << endl ;
136 trie.add_suffix_links() ;
137
138 #ifndef NDEBUG
139 trie.debug( cerr ) ;
140 #endif
141
142 mismatch_ptr *ptrs = 0 ;
143
144 // read database
145 string headerline ;
146 for( size_t i = 0 ; i != dbfiles.size() ; ++i ) {
147 ifstream *dbfile = 0 ;
148 if( dbfiles[i] != "-" ) {
149 dbfile = new ifstream( dbfiles[i].c_str() ) ;
150 if( debug_flags & debug_verbose ) clog << "processing database from " << dbfiles[i] << endl ;
151 if( !dbfile || !*dbfile ) { cerr << "cannot read " << dbfiles[i] << endl ; return 1 ; }
152 }
153 else if( debug_flags & debug_verbose ) clog << "processing database from STDIN" << endl ;
154
155 getline( dbfile ? *dbfile : cin, headerline ) ;
156 while( dbfile ? *dbfile : cin ) {
157 if ( headerline[0] != '>' ) {
158 cerr << "database is not in fasta format" << endl ;
159 exit( 2 ) ;
160 }
161 headerline = headerline.substr( 1, headerline.size()-1 ) ;
162 genome_name = headerline.c_str() ; // global variable
163
164 string seq ;
165 bool eat_n = 0 ;
166 size_t last_n = 0 ;
167 position_genome = 1 ; // global variable
168
169 ptrs = trie.init( ptrs ) ;
170
171 while ( getline( dbfile ? *dbfile : cin, seq )
172 && ( seq.empty() || seq[0] != '>' ) ) {
173 for ( size_t i = 0 ; i < seq.size() ; i++,position_genome++ )
174 {
175 if ( toupper( seq[i] ) == 'N' )
176 if ( eat_n ) continue ;
177 else {
178 last_n ++ ;
179 if ( last_n == cutoff + 1 ) eat_n = 1 ;
180 }
181 else if ( eat_n ) { eat_n = 0 ; last_n = 0 ; }
182
183 if( debug_flags & debug_sequence ) std::clog << seq[i] << std::flush ;
184 ptrs = trie.compare( seq[i], ptrs ) ;
185 }
186 }
187 trie.compare( 0, ptrs ) ;
188 headerline = seq ;
189 }
190
191 delete dbfile ;
192 }
193 ( output ? *output : cout ) << std::flush ;
194 delete output ;
195
196 if( debug_flags & debug_countnodes )
197 clog << "Fetched " << total_nodes << " nodes in total" << endl ;
198
199 poptFreeContext( pc ) ;
200 return 0 ;
201 }