Debian Codegraph @

main.cpp

raw symbols SCIP
1 // PatMaN DNA pattern matchercxx . . $ `<file>/main.cpp`/
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"cxx . . $ `<file>/prefix_tree.h`/
20 #include "fasta.h"cxx . . $ `<file>/fasta.h`/
21
22 using namespace std ;
23
24 // global variables
25 size_t cutoffcxx . . $ cutoff. = 2 ;
26 size_t allow_gapscxx . . $ allow_gaps. = 0 ;
27 size_t minlengthcxx . . $ minlength. = 0 ;
28 long long total_nodescxx . . $ total_nodes. = 0 ;
29 int only_plus_strandcxx . . $ only_plus_strand. = 0 ;
30 int discount_adeninecxx . . $ discount_adenine. = 0 ;
31 int ambi_codescxx . . $ ambi_codes. = 0 ;
32 int chop5cxx . . $ chop5. = 0, chop3cxx . . $ chop3. = 0 ;
33 int debug_flagscxx . . $ debug_flags. = debug_notquietcxx . . $ debug_notquiet. ;
34 int do_prefetchcxx . . $ do_prefetch. = 3 ;
35 ostreamcxx . . $ std/ostream# *outputcxx . . $ output. = 0 ;
36
37 long position_genomecxx . . $ position_genome. ;
38 const char* genome_namecxx . . $ genome_name. ;
39
40 enum option_flagcxx . . $ option_flag# { opt_nonecxx . . $ opt_none., opt_dbcxx . . $ opt_db., opt_patcxx . . $ opt_pat., opt_outputcxx . . $ opt_output., opt_versioncxx . . $ opt_version. } ;
41
42 struct poptOptioncxx . . $ poptOption# optionscxx . . $ options.[] = {
43 { "version", 'V', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`!, 0, opt_versioncxx . . $ opt_version., "Print version number and exit", 0 },
44 { "edits", 'e', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_SHOW_DEFAULTcxx . . $ `<external>/16232926639102580118/popt.h:66:9`!, &cutoffcxx . . $ cutoff., 0, "Set maximum edit distance to N", "N" },
45 { "gaps", 'g', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_SHOW_DEFAULTcxx . . $ `<external>/16232926639102580118/popt.h:66:9`!, &allow_gapscxx . . $ allow_gaps., 0, "Set maximum number of gaps to N", "N" },
46 { "databases", 'D', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`!, 0, opt_dbcxx . . $ opt_db., "Following files are databases", 0 },
47 { "patterns", 'P', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`!, 0, opt_patcxx . . $ opt_pat., "Following files contain patterns", 0 },
48 { "output", 'o', POPT_ARG_STRINGcxx . . $ `<external>/16232926639102580118/popt.h:19:9`!, 0, opt_outputcxx . . $ opt_output., "Write output to FILE", "FILE" },
49 { "ambicodes", 'a', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`!, &ambi_codescxx . . $ ambi_codes., 0, "Interpret ambiguity codes in patterns", 0 },
50 { "singlestrand", 's', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`!, &only_plus_strandcxx . . $ only_plus_strand., 0, "Do not match reverse-complements", 0 },
51 { "prefetch", 'p', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_SHOW_DEFAULTcxx . . $ `<external>/16232926639102580118/popt.h:66:9`!, &do_prefetchcxx . . $ do_prefetch., 0, "Prefetch N nodes", "N" },
52 { "min-length", 'l', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_DOC_HIDDENcxx . . $ `<external>/16232926639102580118/popt.h:48:9`!, &minlengthcxx . . $ minlength., 0, "Only consider patterns longer than N", "N" },
53 { "chop-3", 'x', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_DOC_HIDDENcxx . . $ `<external>/16232926639102580118/popt.h:48:9`!, &chop3cxx . . $ chop3., 0, "Chop N bp off the 3' end of patterns", "N" },
54 { "chop-5", 'X', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_DOC_HIDDENcxx . . $ `<external>/16232926639102580118/popt.h:48:9`!, &chop5cxx . . $ chop5., 0, "Chop N bp off the 5' end of patterns", "N" },
55 { "adenine-hack", 'A', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`! | POPT_ARGFLAG_DOC_HIDDENcxx . . $ `<external>/16232926639102580118/popt.h:48:9`!, &discount_adeninecxx . . $ discount_adenine., 0, "Ignore inserted adenines in db", 0 },
56 { "quiet", 'q', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`! | POPT_BIT_CLRcxx . . $ `<external>/16232926639102580118/popt.h:63:9`!, &debug_flagscxx . . $ debug_flags., debug_notquietcxx . . $ debug_notquiet., "Turn off warnings", 0 },
57 { "verbose", 'v', POPT_ARG_NONEcxx . . $ `<external>/16232926639102580118/popt.h:18:9`! | POPT_BIT_SETcxx . . $ `<external>/16232926639102580118/popt.h:61:9`!, &debug_flagscxx . . $ debug_flags., debug_verbosecxx . . $ debug_verbose., "Print additional progress reports", 0 },
58 { "debug", 'd', POPT_ARG_INTcxx . . $ `<external>/16232926639102580118/popt.h:20:9`! | POPT_ARGFLAG_DOC_HIDDENcxx . . $ `<external>/16232926639102580118/popt.h:48:9`!, &debug_flagscxx . . $ debug_flags., 0, "Set debug flags to N (see man page)", "N" },
59 POPT_AUTOHELPcxx . . $ `<external>/16232926639102580118/popt.h:158:9`! POPT_TABLEENDcxx . . $ `<external>/16232926639102580118/popt.h:161:9`!
60 } ;
61
62
63 int maincxx . . $ main(d8e43b79a186046c).(int argc, const char *argv[]) {
64
65 stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> dbfilescxx . . $ std/vector#vector(49f6e7a06ebc5aa8)., patfilescxx . . $ std/vector#vector(49f6e7a06ebc5aa8). ;
66
67 poptContextcxx . . $ poptContext# pc = poptGetContext( "patman", argc, argv, optionscxx . . $ options., 0 ) ;
68 poptSetOtherOptionHelp( pc, "[OPTION...] [input-file...]" ) ;
69 poptReadDefaultConfig( pc, 0 ) ;
70 bool isdbfiles = false ;
71
72 if( argc <= 1 ) { poptPrintHelp( pc, stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, 0 ) ; return 1 ; }
73 int rc = poptGetNextOpt(pc) ;
74 for(;;) {
75 if( rc == opt_versioncxx . . $ opt_version. ) {
76 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). poptGetInvocationName(pc) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ", revision " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). VERSIONcxx . . $ `<built-in>:481:9`! <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9). ;
77 return 0 ;
78 }
79 else if( rc == opt_dbcxx . . $ opt_db. || rc == opt_patcxx . . $ opt_pat. || rc == -1 ) {
80 while( poptPeekArg(pc) )
81 ( isdbfiles ? dbfiles : patfiles ).push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( poptGetArgcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).(pc) ) ;
82 isdbfiles = (rc == opt_dbcxx . . $ opt_db.) ;
83 }
84 else if( rc == opt_outputcxx . . $ opt_output. )
85 {
86 delete outputcxx . . $ output. ;
87 const char *arg = poptGetOptArg(pc) ;
88 if( 0 == strcmp( arg, "-" ) ) outputcxx . . $ output. = 0 ;
89 else outputcxx . . $ output. = new stdcxx . . $ std/::ofstreamcxx . . $ std/ofstream#( arg ) ;
90 }
91 else {
92 stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). argv[0] <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ": " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). poptStrerror( rc )
93 <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ' <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). poptBadOption( pc, 0 ) <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9). ;
94 return 1 ;
95 }
96 if( rc == -1 ) break ; else rc = poptGetNextOpt(pc) ;
97 }
98
99 if( dbfiles.emptycxx . . $ std/vector#empty(3482b152b9333168).() )
100 {
101 dbfiles.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( "-"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d). ) ;
102 if( debug_flagscxx . . $ debug_flags. & debug_notquietcxx . . $ debug_notquiet. )
103 clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "No databases given, using stdin." <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
104 }
105 if( patfiles.emptycxx . . $ std/vector#empty(3482b152b9333168).() )
106 {
107 clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "No patterns given, aborting." <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
108 exit(1) ;
109 }
110
111 prefix_treecxx . . $ prefix_tree# triecxx . . $ prefix_tree#prefix_tree(49f6e7a06ebc5aa8). ;
112 for( size_t i = 0 ; i != patfiles.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() ; ++i )
113 {
114 ifstreamcxx . . $ std/ifstream# *infile = 0 ;
115 if( patfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] !=cxx . . $ std/`operator!=`(4b17aa87c9f300c6). "-" ) {
116 infile = new ifstreamcxx . . $ std/ifstream#( patfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() ) ;
117 if( debug_flagscxx . . $ debug_flags. & debug_verbosecxx . . $ debug_verbose. ) clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "reading patterns from " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). patfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
118 if( !infile || !cxx . . $ std/basic_ios#`operator!`(50ce9a9e25b4a850).*infile ) { cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "cannot read " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). patfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ; return 1 ; }
119 }
120 else if( debug_flagscxx . . $ debug_flags. & debug_verbosecxx . . $ debug_verbose. ) clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "reading patterns from STDIN" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
121
122 // add pattern to prefix tree
123 fasta_faccxx . . $ fasta_fac# fcxx . . $ fasta_fac#fasta_fac(21eaf1e8e1bd68f5).( infile ? *infile : cincxx . . $ std/cin. ) ;
124 fastacxx . . $ fasta#* ff ;
125 while ( (ff = f.getcxx . . $ fasta_fac#get(e7de4abdd4bb2f0).()) && !ff->nullcxx . . $ fasta#null. ) {
126 if( minlengthcxx . . $ minlength. <= ff->seqcxx . . $ fasta#seq..sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).() )
127 trie.add_seqcxx . . $ prefix_tree#add_seq(63deecd705778e94).(
128 ff->seqcxx . . $ fasta#seq..substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).( chop5cxx . . $ chop5., ff->seqcxx . . $ fasta#seq..sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).() - chop5cxx . . $ chop5. - chop3cxx . . $ chop3. ),
129 ff->headerlinecxx . . $ fasta#headerline., !!ambi_codescxx . . $ ambi_codes. ) ;
130 delete ff ;
131 }
132 delete infile ;
133 }
134
135 if( debug_flagscxx . . $ debug_flags. & debug_verbosecxx . . $ debug_verbose. ) clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "post-processing trie" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
136 trie.add_suffix_linkscxx . . $ prefix_tree#add_suffix_links(49f6e7a06ebc5aa8).() ;
137
138 #ifndef NDEBUGcxx . . $ `<built-in>:482:9`!
139 trie.debug( cerr ) ;
140 #endif
141
142 mismatch_ptrcxx . . $ mismatch_ptr# *ptrs = 0 ;
143
144 // read database
145 stringcxx . . $ std/string# headerlinecxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391). ;
146 for( size_t i = 0 ; i != dbfiles.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() ; ++i ) {
147 ifstreamcxx . . $ std/ifstream# *dbfile = 0 ;
148 if( dbfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] !=cxx . . $ std/`operator!=`(4b17aa87c9f300c6). "-" ) {
149 dbfile = new ifstreamcxx . . $ std/ifstream#( dbfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() ) ;
150 if( debug_flagscxx . . $ debug_flags. & debug_verbosecxx . . $ debug_verbose. ) clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "processing database from " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). dbfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
151 if( !dbfile || !cxx . . $ std/basic_ios#`operator!`(50ce9a9e25b4a850).*dbfile ) { cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "cannot read " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). dbfiles[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ; return 1 ; }
152 }
153 else if( debug_flagscxx . . $ debug_flags. & debug_verbosecxx . . $ debug_verbose. ) clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "processing database from STDIN" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
154
155 getlinecxx . . $ std/getline(4ec722ed0b69c2ab).( dbfile ? *dbfile : cincxx . . $ std/cin., headerline ) ;
156 while( dbfile ? *dbfile : cincxx . . $ std/cin. ) {
157 if ( headerline[cxx . . $ std/__cxx11/basic_string#`operator[]`(7b73c3179b3cb3ad).0] != '>' ) {
158 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "database is not in fasta format" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
159 exit( 2 ) ;
160 }
161 headerline =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). headerline.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).( 1, headerline.sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).()-1 ) ;
162 genome_namecxx . . $ genome_name. = headerline.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() ; // global variable
163
164 stringcxx . . $ std/string# seqcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391). ;
165 bool eat_n = 0 ;
166 size_t last_n = 0 ;
167 position_genomecxx . . $ position_genome. = 1 ; // global variable
168
169 ptrs = trie.initcxx . . $ prefix_tree#init(9f4eb692c23f96cc).( ptrs ) ;
170
171 while ( getlinecxx . . $ std/getline(4ec722ed0b69c2ab).( dbfile ? *dbfile : cincxx . . $ std/cin., seq )
172 && ( seq.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).() || seq[cxx . . $ std/__cxx11/basic_string#`operator[]`(7b73c3179b3cb3ad).0] != '>' ) ) {
173 for ( size_t i = 0 ; i < seq.sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).() ; i++,position_genomecxx . . $ position_genome.++ )
174 {
175 if ( toupper( seq[cxx . . $ std/__cxx11/basic_string#`operator[]`(7b73c3179b3cb3ad).i] ) == 'N' )
176 if ( eat_n ) continue ;
177 else {
178 last_n ++ ;
179 if ( last_n == cutoffcxx . . $ cutoff. + 1 ) eat_n = 1 ;
180 }
181 else if ( eat_n ) { eat_n = 0 ; last_n = 0 ; }
182
183 if( debug_flagscxx . . $ debug_flags. & debug_sequencecxx . . $ debug_sequence. ) stdcxx . . $ std/::clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). seq[cxx . . $ std/__cxx11/basic_string#`operator[]`(7b73c3179b3cb3ad).i] <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::flushcxx . . $ std/flush(345bf97a19cc83a9). ;
184 ptrs = trie.comparecxx . . $ prefix_tree#compare(b3dcdb8feb03de9).( seq[cxx . . $ std/__cxx11/basic_string#`operator[]`(7b73c3179b3cb3ad).i], ptrs ) ;
185 }
186 }
187 trie.comparecxx . . $ prefix_tree#compare(b3dcdb8feb03de9).( 0, ptrs ) ;
188 headerline =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). seq ;
189 }
190
191 delete dbfile ;
192 }
193 ( outputcxx . . $ output. ? *outputcxx . . $ output. : coutcxx . . $ std/cout. ) <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::flushcxx . . $ std/flush(345bf97a19cc83a9). ;
194 delete outputcxx . . $ output. ;
195
196 if( debug_flagscxx . . $ debug_flags. & debug_countnodescxx . . $ debug_countnodes. )
197 clogcxx . . $ std/clog. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Fetched " <<cxx . . $ std/basic_ostream#`operator<<`(c462fdeeaabf6eaa). total_nodescxx . . $ total_nodes. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " nodes in total" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9). ;
198
199 poptFreeContext( pc ) ;
200 return 0 ;
201 }