annotate match.cpp @ 15:0e221dbd17b2 default tip

Uploaded
author xuebing
date Sat, 31 Mar 2012 08:53:06 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
1 #include <string>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
2 #include <sstream>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
3 #include <iostream>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
4 #include <fstream>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
5 #include <vector>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
6 #include <set>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
7 #include <map>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
8
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
9 #include <string.h>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
10 #include <stdio.h>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
11 #include <stdlib.h>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
12 #include <time.h>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
13
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
14 using namespace std;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
15
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
16 string current_time()
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
17 {// get current time, in the format:
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
18 // Thu Mar 15 21:06:57 2012
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
19 time_t rawtime;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
20 struct tm * timeinfo;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
21 time ( &rawtime );
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
22 timeinfo = localtime ( &rawtime );
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
23 string str = asctime (timeinfo);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
24 return str.substr(0,str.size()-1);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
25 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
26
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
27 template<class key,class val>
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
28 void PrintMap(map<key,val> m)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
29 {// print out a map
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
30 for (typename map<key,val>::iterator it = m.begin();it!=m.end(); it++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
31 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
32 cout << (*it).first << "\t=>\t" <<(*it).second<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
33 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
34 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
35
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
36 char complement(char ch){
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
37 switch (ch)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
38 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
39 case 'A':return 'T';
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
40 case 'C':return 'G';
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
41 case 'G':return 'C';
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
42 case 'T':return 'A';
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
43 default:return 'N';
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
44 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
45 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
46
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
47 string reverseComplement(string seq){
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
48 int L = seq.length();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
49 string rc (L,'0');
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
50 for (int i=0;i<L; i++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
51 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
52 rc[L-i-1] = complement(seq[i]);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
53 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
54 return rc;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
55 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
56
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
57 void ReadOneSeqFromFasta(ifstream& infile, string& name, string& seq)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
58 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
59 // read one sequence from fasta file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
60 getline(infile,name); // read identifier line
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
61 name = name.substr(1);// remove leading '>'
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
62 seq = ""; // initialize sequence
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
63 string str;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
64 while(infile.peek() != '>' && infile.good())
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
65 {// before next '>' and before hitting the end of the file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
66 getline(infile,str);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
67 seq.append(str);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
68 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
69 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
70
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
71 vector<int> findall(string seq, string motif)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
72 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
73 // find all match positions of motif in seq
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
74 vector<int> allpos;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
75 size_t pos = 0;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
76 while(pos != string::npos)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
77 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
78 pos = seq.find(motif,pos+1);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
79 allpos.push_back(pos);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
80 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
81 return allpos;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
82 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
83
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
84 string int2str(int number)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
85 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
86 stringstream ss;//create a stringstream
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
87 ss << number;//add number to the stream
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
88 return ss.str();//return a string with the contents of the stream
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
89 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
90
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
91
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
92
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
93 map<string,string> ReadFasta(string filename)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
94 {//read all sequences in a fasta file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
95 ifstream fin(filename.c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
96
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
97 map<string,string> seqs;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
98 string name,seq;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
99 while(fin.good())
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
100 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
101 ReadOneSeqFromFasta(fin,name,seq);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
102 seqs[name] = seq;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
103 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
104 fin.close();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
105 return seqs;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
106 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
107
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
108 void mismatches(map<string,string>& mutant,map<string,int>& dist, string motif, int n, set<char> alphabet)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
109 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
110 set<char>::iterator it;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
111 if (mutant.count(motif) == 0)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
112 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
113 mutant[motif] = "";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
114 dist[motif]=n;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
115 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
116 if(n==0){return;}
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
117 for (int i=0;i<motif.length();i++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
118 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
119 string str=motif;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
120 set<char> ab = alphabet;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
121 ab.erase(str[i]);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
122 for (it = ab.begin(); it!=ab.end(); it++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
123 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
124 str[i] = *it;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
125 //cout << "mutate "<<motif<<" to "<<str<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
126 if (mutant.count(str) >0)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
127 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
128 if(dist[str] >= n)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
129 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
130 //cout << mutant[str] <<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
131 continue;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
132 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
133 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
134
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
135 //mutated to a new sequence
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
136 //cout <<"new mutation"<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
137 mutant[str] = mutant[motif];
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
138 mutant[str].push_back(',');
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
139 mutant[str].push_back(motif[i]);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
140 mutant[str].append(int2str(i+1));
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
141 mutant[str].push_back(str[i]);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
142 dist[str]=n;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
143 //cout << "tag="<<mutant[str]<<" dist="<<n<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
144
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
145 if (n>1)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
146 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
147 //cout << "subproc" <<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
148 mismatches(mutant,dist,str,n-1,alphabet);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
149 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
150 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
151
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
152 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
153 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
154
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
155 map<string,string> ExpandMotifs(map<string,string>& motifs, int nmismatch, bool rc, set<char> alphabet)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
156 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
157 map<string,string> expandedmotifs;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
158 // generate mismatched motifs
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
159 map<string,string> mutants;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
160 map<string,int> dist;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
161 map<string,string>::iterator it; // iterator for motifs
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
162 map<string,int>::iterator it2; // iterator for mutants
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
163 string name,seq,tmp;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
164 //cout<<"input motifs"<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
165 //PrintMap(motifs);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
166 for(it=motifs.begin();it!=motifs.end();it++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
167 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
168 name = (*it).first;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
169 seq = (*it).second;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
170
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
171 mismatches(mutants,dist,seq,nmismatch,alphabet);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
172 //cout << mutants.size()<<" mutants identified" <<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
173 //PrintMap(mutants);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
174 // add mutants to motifs
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
175 for(it2=dist.begin();it2!=dist.end();it2++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
176 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
177 string tmp = name;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
178 tmp.append(",").append((*it2).first);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
179 tmp.append(mutants[(*it2).first]);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
180 expandedmotifs[tmp] = (*it2).first;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
181 //cout << name <<","<<tmp<<","<<expandedmotifs[tmp]<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
182 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
183 // clear the mutants list
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
184 mutants.clear();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
185 dist.clear();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
186 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
187 //PrintMap(expandedmotifs);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
188 //cout << expandedmotifs.size() <<" expanded motifs"<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
189 //cout <<"add reverse complement"<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
190 map<string,string> expandedmotifs_rc = expandedmotifs;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
191 if (rc)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
192 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
193 for(it=expandedmotifs.begin();it!=expandedmotifs.end();it++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
194 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
195 name = (*it).first;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
196 expandedmotifs_rc[name.append(",rc")] = reverseComplement((*it).second);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
197 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
198 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
199
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
200 return expandedmotifs_rc;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
201
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
202 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
203
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
204
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
205
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
206 int* match(string motiffile, string seqfile, string outfile, int nmismatch, bool rc, set<char> alphabet)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
207 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
208 int nsite = 0; // total number of matches to report
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
209 int nseq = 0;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
210 ifstream fmotif, fseq;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
211 ofstream fout;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
212
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
213 // load motifs
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
214 map<string,string> motifs = ReadFasta(motiffile);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
215 cout <<"["<<current_time()<<"] "<<motifs.size()<< " motifs loaded from "<<motiffile<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
216
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
217 // expand motifs
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
218 map<string,string> expandedmotifs = ExpandMotifs(motifs,nmismatch,rc,alphabet);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
219 cout <<"["<<current_time()<<"] "<<expandedmotifs.size()<< " motifs after expanding (mismatch/reverse-complement)"<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
220
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
221 //PrintMap(expandedmotifs);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
222
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
223 // searching motifs in each sequence
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
224 fseq.open(seqfile.c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
225 fout.open(outfile.c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
226
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
227 string seqname,seq,motifname,motif;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
228 while(fseq.good())
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
229 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
230 // read one sequence
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
231 ReadOneSeqFromFasta(fseq,seqname,seq);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
232 nseq = nseq + 1;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
233
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
234 cout.flush();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
235 // iterate over motifs
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
236 map<string,string>::iterator it;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
237 for(it=expandedmotifs.begin();it!=expandedmotifs.end();it++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
238 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
239 motifname = (*it).first;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
240 motif = (*it).second;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
241 //cout << "searching for "<<motifname<<":"<< motif <<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
242 vector<int> found = findall(seq,motif);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
243 for (int i =0;i<found.size()-1;i++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
244 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
245 fout <<seqname<<"\t"<<found[i]<< "\t"<< motifname <<"\t"<<motif<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
246 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
247 nsite = nsite + found.size()-1;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
248 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
249 cout <<"\r["<<current_time()<<"] " << nsite << " sites found in "<< nseq << " sequences " ;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
250 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
251
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
252 cout << endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
253 fseq.close();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
254 fout.close();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
255
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
256 int* res = new int[2];
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
257 res[0] = nsite;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
258 res[1] = nseq;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
259
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
260 return res;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
261 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
262
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
263 vector<string> StringExplode(string str, string separator){
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
264 int found;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
265 vector<string> results;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
266 found = str.find_first_of(separator);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
267 while(found != string::npos){
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
268 if(found > 0){
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
269 results.push_back(str.substr(0,found));
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
270 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
271 str = str.substr(found+1);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
272 found = str.find_first_of(separator);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
273 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
274 if(str.length() > 0){
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
275 results.push_back(str);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
276 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
277 return results;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
278 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
279
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
280 int tab2bed(string infile, string outfile)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
281 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
282 //hg18_chr6_122208322_122209078_+ 635 5ss,A7C,G8T-rc AAGTACCTG
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
283 //hg18_chr6_122208322_122209078_+ 553 5ss,C1G,G3A GAAGTAAGT
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
284 ifstream fin;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
285 ofstream fout;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
286 fin.open(infile.c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
287 fout.open(outfile.c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
288 string line;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
289 vector<string> flds;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
290 vector<string> pos;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
291 vector<string> nm;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
292 while(fin)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
293 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
294 getline(fin,line);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
295 if (line.length() == 0)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
296 continue;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
297 flds = StringExplode(line,"\t");
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
298 pos = StringExplode(flds[0],"_");
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
299 if (pos.size() < 5)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
300 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
301 cout << "\n!! incorrect sequence name format!\n make sure sequence name looks like: hg18_chr6_122208322_122209078_+\n\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
302 return 0;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
303 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
304 string chr = pos[1];
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
305 int start = atoi(pos[2].c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
306 int end = atoi(pos[3].c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
307 int match_start = atoi(flds[1].c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
308 int motifLen = flds[3].length();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
309 // check if match on the other strand
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
310 string strandness = "sense";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
311 if (flds[2].find("rc") !=string::npos)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
312 strandness = "antisense";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
313 string strand = pos[4];
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
314
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
315 if (strand== "+")
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
316 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
317 start = start + match_start;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
318 if (strandness == "antisense")
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
319 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
320 strand = "-";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
321 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
322 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
323 else//sequence on the - strand of the genome
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
324 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
325 start = end - match_start - motifLen;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
326 if (strandness == "antisense")
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
327 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
328 strand = "+";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
329 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
330 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
331 end = start + motifLen;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
332 // number of mismatches
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
333 nm = StringExplode(flds[2],",");
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
334 int score = nm.size()-1;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
335
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
336 fout << chr <<"\t"<<start<<"\t" <<end<<"\t"<<flds[3]<< "\t"<<score <<"\t"<<strand<< "\t"<<strandness<<"\t"<<flds[2]<<"\t"<<flds[0]<<"\t"<<flds[1]<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
337
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
338 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
339 fin.close();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
340 fout.close();
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
341 return 1;//return 1 if successfully created bed file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
342 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
343
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
344 int main(int argc, char* argv[]) {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
345
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
346 if (argc < 7)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
347 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
348 cout << "usage\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
349 cout << " match motif.fa seq.fa outfile nmismatch rc/norc bed/nobed" << endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
350 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
351 cout << "arguments:" <<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
352 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
353 cout << " 1. motif file, fasta format, could include multiple sequences\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
354 cout << " 2. sequence file, fasta format, could include multiple sequences\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
355 cout << " 3. output file name\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
356 cout << " 4. to search on reverse complement strand or not. use 'rc' or 'norc'\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
357 cout << " 5. to output BED format or not. use 'bed' only if sequence have id like: hg18_chr12_52642148_52644699_+\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
358 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
359 cout << "output format\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
360 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
361 cout << " tabular:\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
362 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
363 cout << " hg18_chr10_20048197_20048851_+ 21 5ss,T5A:CAGGAAAGT-rc ACTTTCCTG\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
364 cout << " column 1: sequence id\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
365 cout << " column 2: start of the match in sequence\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
366 cout << " column 3: motif variant id, format: motif_id,mismatch:motif_sequence[-reverse_complement]\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
367 cout << " column 4: actual match in target sequence\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
368 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
369 cout << " BED format:\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
370 cout << "==========\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
371 cout << " column 1-3: genomic coordinates of the match, calculated using sequence ID: hg18_chr12_52642148_52644699_+ and match start in the sequence\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
372 cout << " column 4: number of mismatches\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
373 cout << " column 6: strand of the match sequence\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
374 cout << " column 7: strandness of the match relative to the target sequence\n";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
375 return 1;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
376 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
377
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
378
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
379 string motiffile = argv[1]; // motif file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
380 string seqfile = argv[2]; // sequence file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
381 string outfile = argv[3]; // output file
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
382 int nmismatch = atoi(argv[4]);// # of mismatches allowed
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
383 string _rc = argv[5]; // rc or norc: to search reverse complement strand or not
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
384 string _bed = argv[6]; // to make bed output or not. only valid if sequence have id like: hg18_chr12_52642148_52644699_+
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
385 bool rc = false;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
386 if (_rc == "rc") {rc=true;}
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
387 bool bed = false;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
388 if (_bed == "bed") {bed=true;}
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
389
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
390 // alphabet
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
391 string ACGT_str = "ACGT";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
392 set<char> alphabet;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
393 for (int i=0;i<4;i++)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
394 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
395 alphabet.insert(ACGT_str[i]);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
396 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
397
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
398 int *res = match(motiffile, seqfile, outfile, nmismatch, rc, alphabet);
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
399
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
400 if (_bed == "bed" && res[0]>0)
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
401 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
402 cout <<"["<<current_time()<<"] creating BED format output..."<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
403 string bedfile = outfile;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
404 bedfile += ".bed";
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
405 if (tab2bed(outfile,bedfile))
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
406 {
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
407 rename(bedfile.c_str(),outfile.c_str());
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
408 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
409 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
410 cout <<"["<<current_time()<<"] Done"<<endl;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
411 return 0;
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
412 }
0e221dbd17b2 Uploaded
xuebing
parents:
diff changeset
413