1 // This file is part of the Mussa source distribution.
2 // http://mussa.caltech.edu/
3 // Contact author: Tristan De Buysscher, tristan@caltech.edu
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA. It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
11 // This file is part of the Mussa source distribution.
12 // http://mussa.caltech.edu/
13 // Contact author: Tristan De Buysscher, tristan@caltech.edu
15 // This program and all associated source code files are Copyright (C) 2005
16 // the California Institute of Technology, Pasadena, CA, 91125 USA. It is
17 // under the GNU Public License; please see the included LICENSE.txt
18 // file for more information, or contact Tristan directly.
21 // ----------------------------------------
22 // ---------- sequence.cc -----------
23 // ----------------------------------------
25 #include "alg/sequence.hpp"
26 #include "mussa_exceptions.hpp"
41 annot::annot(int start, int end, std::string type, std::string name)
49 motif::motif(int start, std::string motif)
50 : annot(start, start+motif.size(), "motif", motif),
64 Sequence::Sequence(string seq)
66 set_filtered_sequence(seq);
69 Sequence &Sequence::operator=(const Sequence& s)
72 sequence = s.sequence;
80 Sequence &Sequence::operator=(const std::string& s)
82 set_filtered_sequence(s);
86 ostream& operator<<(ostream& out, const Sequence& seq)
88 out << "Sequence(" << seq.get_seq() << ")";
92 //! load a fasta file into a sequence
94 * \param file_path the location of the fasta file in the filesystem
95 * \param seq_num which sequence in the file to load
96 * \param start_index starting position in the fasta sequence, 0 for beginning
97 * \param end_index ending position in the fasta sequence, 0 for end
98 * \return error message, empty string if no error. (gag!)
101 Sequence::load_fasta(string file_path, int seq_num,
102 int start_index, int end_index)
105 string file_data_line;
106 int header_counter = 0;
107 bool read_seq = true;
110 string seq_tmp; // holds sequence during basic filtering
112 data_file.open(file_path.c_str(), ios::in);
116 throw mussa_load_error("Sequence File: " + file_path + " not found");
118 // if file opened okay, read it
121 // search for the header of the fasta sequence we want
122 while ( (!data_file.eof()) && (header_counter < seq_num) )
124 getline(data_file,file_data_line);
125 if (file_data_line.substr(0,1) == ">")
129 header = file_data_line.substr(1);
133 while ( !data_file.eof() && read_seq )
135 getline(data_file,file_data_line);
136 if (file_data_line.substr(0,1) == ">")
138 else sequence_raw += file_data_line;
143 // Lastly, if subselection of the sequence was specified we keep cut out
144 // and only keep that part
145 // end_index = 0 means no end was specified, so cut to the end
147 end_index = sequence_raw.size();
149 // sequence filtering for upcasing agctn and convert non AGCTN to N
150 set_filtered_sequence(sequence_raw, start_index, end_index-start_index);
154 void Sequence::set_filtered_sequence(const string &old_seq,
155 string::size_type start,
156 string::size_type count)
158 char conversionTable[257];
161 count = old_seq.size() - start;
163 sequence.reserve(count);
165 // Make a conversion table
167 // everything we don't specify below will become 'N'
168 for(int table_i=0; table_i < 256; table_i++)
170 conversionTable[table_i] = 'N';
172 // add end of string character for printing out table for testing purposes
173 conversionTable[256] = '\0';
175 // we want these to map to themselves - ie not to change
176 conversionTable[(int)'A'] = 'A';
177 conversionTable[(int)'T'] = 'T';
178 conversionTable[(int)'G'] = 'G';
179 conversionTable[(int)'C'] = 'C';
181 conversionTable[(int)'a'] = 'A';
182 conversionTable[(int)'t'] = 'T';
183 conversionTable[(int)'g'] = 'G';
184 conversionTable[(int)'c'] = 'C';
186 // finally, the actual conversion loop
187 for(string::size_type seq_index = 0; seq_index < count; seq_index++)
189 sequence += conversionTable[ (int)old_seq[seq_index+start]];
193 // this doesn't work properly under gcc 3.x ... it can't recognize toupper
194 //transform(sequence.begin(), sequence.end(), sequence.begin(), toupper);
198 Sequence::load_annot(string file_path, int start_index, int end_index)
201 string file_data_line;
203 string::size_type space_split_i;
205 list<annot>::iterator list_i;
210 data_file.open(file_path.c_str(), ios::in);
214 throw mussa_load_error("Sequence File: " + file_path + " not found");
216 // if file opened okay, read it
219 getline(data_file,file_data_line);
220 species = file_data_line;
222 // end_index = 0 means no end was specified, so cut to the end
224 end_index = sequence.length();
226 //cout << "START: " << start_index << " END: " << end_index << endl;
228 while ( !data_file.eof() )
230 getline(data_file,file_data_line);
231 if (file_data_line != "")
233 // need to get 4 values...almost same code 4 times...
234 // get annot start index
235 space_split_i = file_data_line.find(" ");
236 annot_value = file_data_line.substr(0,space_split_i);
237 an_annot.start = atoi (annot_value.c_str());
238 file_data_line = file_data_line.substr(space_split_i+1);
239 // get annot end index
240 space_split_i = file_data_line.find(" ");
241 annot_value = file_data_line.substr(0,space_split_i);
242 an_annot.end = atoi (annot_value.c_str());
243 file_data_line = file_data_line.substr(space_split_i+1);
245 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
249 space_split_i = file_data_line.find(" ");
250 if (space_split_i == string::npos) // no entries for name & type
252 cout << "seq, annots - no name or type\n";
258 annot_value = file_data_line.substr(0,space_split_i);
259 an_annot.name = annot_value;
260 file_data_line = file_data_line.substr(space_split_i+1);
262 space_split_i = file_data_line.find(" ");
263 if (space_split_i == string::npos) // no entry for type
267 annot_value = file_data_line.substr(0,space_split_i);
268 an_annot.type = annot_value;
273 // add annot to list if it falls within the range of sequence specified
274 if ((start_index <= an_annot.start) && (end_index >= an_annot.end))
276 an_annot.start -= start_index;
277 an_annot.end -= start_index;
278 annots.push_back(an_annot);
281 cout << "FAILED!!!!!!\n";
288 for(list_i = annots.begin(); list_i != annots.end(); ++list_i)
290 cout << (*list_i).start << "," << (*list_i).end << "\t";
291 cout << (*list_i).name << "\t" << (*list_i).type << endl;
297 bool Sequence::empty() const
299 return (size() == 0);
302 const std::list<annot>& Sequence::annotations() const
307 string::size_type Sequence::length() const
312 string::size_type Sequence::size() const
314 return sequence.size();
317 Sequence::iterator Sequence::begin()
319 return sequence.begin();
322 Sequence::const_iterator Sequence::begin() const
324 return sequence.begin();
327 Sequence::iterator Sequence::end()
329 return sequence.end();
332 Sequence::const_iterator Sequence::end() const
334 return sequence.end();
339 Sequence::get_seq() const
346 Sequence::subseq(int start, int end) const
348 return sequence.substr(start, end);
353 Sequence::c_seq() const
355 return sequence.c_str();
359 Sequence::rev_comp() const
362 char conversionTable[257];
363 int seq_i, table_i, len;
365 len = sequence.length();
366 rev_comp.reserve(len);
367 // make a conversion table
368 // init all parts of conversion table to '~' character
369 // '~' I doubt will ever appear in a sequence file (jeez, I hope)
370 // and may the fleas of 1000 camels infest the genitals of any biologist (and
371 // seven generations of their progeny) who decides to make it mean
372 // something special!!!
373 // PS - double the curse for any smartass non-biologist who tries it as well
374 for(table_i=0; table_i < 256; table_i++)
376 conversionTable[table_i] = '~';
378 // add end of string character for printing out table for testing purposes
379 conversionTable[256] = '\0';
381 // add in the characters for the bases we want to convert
382 conversionTable[(int)'A'] = 'T';
383 conversionTable[(int)'T'] = 'A';
384 conversionTable[(int)'G'] = 'C';
385 conversionTable[(int)'C'] = 'G';
386 conversionTable[(int)'N'] = 'N';
388 // finally, the actual conversion loop
389 for(seq_i = len - 1; seq_i >= 0; seq_i--)
391 table_i = (int) sequence[seq_i];
392 rev_comp += conversionTable[table_i];
400 Sequence::get_header() const
405 //FIXME: i don't think this code is callable
407 Sequence::sp_name() const
414 Sequence::set_seq(const string& a_seq)
416 set_filtered_sequence(a_seq);
438 Sequence::save(fstream &save_file)
439 //string save_file_path)
442 list<annot>::iterator annots_i;
444 // not sure why, or if i'm doing something wrong, but can't seem to pass
445 // file pointers down to this method from the mussa control class
446 // so each call to save a sequence appends to the file started by mussa_class
447 //save_file.open(save_file_path.c_str(), ios::app);
449 save_file << "<Sequence>" << endl;
450 save_file << sequence << endl;
451 save_file << "</Sequence>" << endl;
453 save_file << "<Annotations>" << endl;
454 save_file << species << endl;
455 for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i)
457 save_file << annots_i->start << " " << annots_i->end << " " ;
458 save_file << annots_i->name << " " << annots_i->type << endl;
460 save_file << "</Annotations>" << endl;
465 Sequence::load_museq(string load_file_path, int seq_num)
468 string file_data_line;
471 string::size_type space_split_i;
475 load_file.open(load_file_path.c_str(), ios::in);
478 // search for the seq_num-th sequence
479 while ( (!load_file.eof()) && (seq_counter < seq_num) )
481 getline(load_file,file_data_line);
482 if (file_data_line == "<Sequence>")
485 getline(load_file, file_data_line);
486 sequence = file_data_line;
487 getline(load_file, file_data_line);
488 getline(load_file, file_data_line);
489 if (file_data_line == "<Annotations>")
491 getline(load_file, file_data_line);
492 species = file_data_line;
493 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
495 getline(load_file,file_data_line);
496 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
498 // need to get 4 values...almost same code 4 times...
499 // get annot start index
500 space_split_i = file_data_line.find(" ");
501 annot_value = file_data_line.substr(0,space_split_i);
502 an_annot.start = atoi (annot_value.c_str());
503 file_data_line = file_data_line.substr(space_split_i+1);
504 // get annot end index
505 space_split_i = file_data_line.find(" ");
506 annot_value = file_data_line.substr(0,space_split_i);
507 an_annot.end = atoi (annot_value.c_str());
509 if (space_split_i == string::npos) // no entry for type or name
511 cout << "seq, annots - no type or name\n";
515 else // else get annot type
517 file_data_line = file_data_line.substr(space_split_i+1);
518 space_split_i = file_data_line.find(" ");
519 annot_value = file_data_line.substr(0,space_split_i);
520 an_annot.type = annot_value;
521 if (space_split_i == string::npos) // no entry for name
523 cout << "seq, annots - no name\n";
526 else // get annot name
528 file_data_line = file_data_line.substr(space_split_i+1);
529 space_split_i = file_data_line.find(" ");
530 annot_value = file_data_line.substr(0,space_split_i);
531 an_annot.type = annot_value;
534 annots.push_back(an_annot); // don't forget to actually add the annot
536 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
537 // << "-->" << an_annot.type << "::" << an_annot.name << endl;
545 Sequence::rc_motif(string a_motif)
548 char conversionTable[257];
549 int seq_i, table_i, len;
551 len = a_motif.length();
552 rev_comp.reserve(len);
554 for(table_i=0; table_i < 256; table_i++)
556 conversionTable[table_i] = '~';
558 // add end of string character for printing out table for testing purposes
559 conversionTable[256] = '\0';
561 // add in the characters for the bases we want to convert (IUPAC)
562 conversionTable[(int)'A'] = 'T';
563 conversionTable[(int)'T'] = 'A';
564 conversionTable[(int)'G'] = 'C';
565 conversionTable[(int)'C'] = 'G';
566 conversionTable[(int)'N'] = 'N';
567 conversionTable[(int)'M'] = 'K';
568 conversionTable[(int)'R'] = 'Y';
569 conversionTable[(int)'W'] = 'W';
570 conversionTable[(int)'S'] = 'S';
571 conversionTable[(int)'Y'] = 'R';
572 conversionTable[(int)'K'] = 'M';
573 conversionTable[(int)'V'] = 'B';
574 conversionTable[(int)'H'] = 'D';
575 conversionTable[(int)'D'] = 'H';
576 conversionTable[(int)'B'] = 'V';
578 // finally, the actual conversion loop
579 for(seq_i = len - 1; seq_i >= 0; seq_i--)
581 //cout << "** i = " << seq_i << " bp = " <<
582 table_i = (int) a_motif[seq_i];
583 rev_comp += conversionTable[table_i];
586 //cout << "seq: " << a_motif << endl;
587 //cout << "rc: " << rev_comp << endl;
593 Sequence::motif_normalize(string a_motif)
598 len = a_motif.length();
599 valid_motif.reserve(len);
601 // this just upcases IUPAC symbols. Eventually should return an error if non IUPAC is present.
602 // current nonIUPAC symbols are omitted, which is not reported atm
603 for(seq_i = 0; seq_i < len; seq_i++)
605 if ((a_motif[seq_i] == 'a') || (a_motif[seq_i] == 'A'))
607 else if ((a_motif[seq_i] == 't') || (a_motif[seq_i] == 'T'))
609 else if ((a_motif[seq_i] == 'g') || (a_motif[seq_i] == 'G'))
611 else if ((a_motif[seq_i] == 'c') || (a_motif[seq_i] == 'C'))
613 else if ((a_motif[seq_i] == 'n') || (a_motif[seq_i] == 'N'))
615 else if ((a_motif[seq_i] == 'm') || (a_motif[seq_i] == 'M'))
617 else if ((a_motif[seq_i] == 'r') || (a_motif[seq_i] == 'R'))
619 else if ((a_motif[seq_i] == 'w') || (a_motif[seq_i] == 'W'))
621 else if ((a_motif[seq_i] == 's') || (a_motif[seq_i] == 'S'))
623 else if ((a_motif[seq_i] == 'y') || (a_motif[seq_i] == 'Y'))
625 else if ((a_motif[seq_i] == 'k') || (a_motif[seq_i] == 'K'))
627 else if ((a_motif[seq_i] == 'v') || (a_motif[seq_i] == 'V'))
629 else if ((a_motif[seq_i] == 'h') || (a_motif[seq_i] == 'H'))
631 else if ((a_motif[seq_i] == 'd') || (a_motif[seq_i] == 'D'))
633 else if ((a_motif[seq_i] == 'b') || (a_motif[seq_i] == 'B'))
636 string msg = "Letter ";
637 msg += a_motif[seq_i];
638 msg += " is not a valid IUPAC symbol";
639 throw motif_normalize_error(msg);
642 //cout << "valid_motif is: " << valid_motif << endl;
646 void Sequence::add_motif(string a_motif)
648 vector<int> motif_starts = find_motif(a_motif);
650 for(vector<int>::iterator motif_start_i = motif_starts.begin();
651 motif_start_i != motif_starts.end();
654 motif_list.push_back(motif(*motif_start_i, a_motif));
658 void Sequence::clear_motifs()
663 const list<motif>& Sequence::motifs() const
669 Sequence::find_motif(string a_motif)
671 vector<int> motif_match_starts;
674 motif_match_starts.clear();
676 //cout << "motif is: " << a_motif << endl;
677 a_motif = motif_normalize(a_motif);
678 //cout << "motif is: " << a_motif << endl;
682 //cout << "Sequence: none blank motif\n";
683 motif_scan(a_motif, &motif_match_starts);
685 a_motif_rc = rc_motif(a_motif);
686 // make sure not to do search again if it is a palindrome
687 if (a_motif_rc != a_motif)
688 motif_scan(a_motif_rc, &motif_match_starts);
690 return motif_match_starts;
694 Sequence::motif_scan(string a_motif, vector<int> * motif_match_starts)
697 string::size_type seq_i;
698 int motif_i, motif_len;
700 // faster to loop thru the sequence as a old c string (ie char array)
701 seq_c = (char*)sequence.c_str();
702 //cout << "Sequence: motif, seq len = " << sequence.length() << endl;
703 motif_len = a_motif.length();
705 //cout << "motif_length: " << motif_len << endl;
706 //cout << "RAAARRRRR\n";
710 //cout << "motif: " << a_motif << endl;
712 //cout << "Sequence: motif, length= " << length << endl;
714 while (seq_i < sequence.length())
716 //cout << seq_c[seq_i];
717 //cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
718 // this is pretty much a straight translation of Nora's python code
719 // to match iupac letter codes
720 if (a_motif[motif_i] =='N')
722 else if (a_motif[motif_i] == seq_c[seq_i])
724 else if ((a_motif[motif_i] =='M') &&
725 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C')))
727 else if ((a_motif[motif_i] =='R') &&
728 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G')))
730 else if ((a_motif[motif_i] =='W') &&
731 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='T')))
733 else if ((a_motif[motif_i] =='S') &&
734 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G')))
736 else if ((a_motif[motif_i] =='Y') &&
737 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='T')))
739 else if ((a_motif[motif_i] =='K') &&
740 ((seq_c[seq_i]=='G') || (seq_c[seq_i]=='T')))
742 else if ((a_motif[motif_i] =='V') &&
743 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
744 (seq_c[seq_i]=='G')))
746 else if ((a_motif[seq_i] =='H') &&
747 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
748 (seq_c[seq_i]=='T')))
750 else if ((a_motif[motif_i] =='D') &&
751 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G') ||
752 (seq_c[seq_i]=='T')))
754 else if ((a_motif[motif_i] =='B') &&
755 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G') ||
756 (seq_c[seq_i]=='T')))
765 // end Nora stuff, now we see if a match is found this pass
766 if (motif_i == motif_len)
770 motif_match_starts->push_back(seq_i - motif_len + 1);