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 // ----------------------------------------
24 #include <boost/filesystem/fstream.hpp>
26 #include "alg/sequence.hpp"
27 #include "mussa_exceptions.hpp"
32 namespace fs = boost::filesystem;
43 annot::annot(int start, int end, std::string type, std::string name)
51 motif::motif(int start, std::string motif)
52 : annot(start, start+motif.size(), "motif", motif),
66 Sequence::Sequence(string seq)
68 set_filtered_sequence(seq);
71 Sequence &Sequence::operator=(const Sequence& s)
74 sequence = s.sequence;
82 Sequence &Sequence::operator=(const std::string& s)
84 set_filtered_sequence(s);
88 char Sequence::operator[](int index) const
90 return sequence[index];
93 ostream& operator<<(ostream& out, const Sequence& seq)
95 out << "Sequence(" << seq.get_seq() << ")";
99 //! load a fasta file into a sequence
101 * \param file_path the location of the fasta file in the filesystem
102 * \param seq_num which sequence in the file to load
103 * \param start_index starting position in the fasta sequence, 0 for beginning
104 * \param end_index ending position in the fasta sequence, 0 for end
105 * \return error message, empty string if no error. (gag!)
108 Sequence::load_fasta(fs::path file_path, int seq_num,
109 int start_index, int end_index)
111 fs::fstream data_file;
112 string file_data_line;
113 int header_counter = 0;
114 bool read_seq = true;
117 string seq_tmp; // holds sequence during basic filtering
119 data_file.open(file_path, ios::in);
122 throw mussa_load_error("fasta sequence number is 1 based (can't be 0)");
126 throw mussa_load_error("Sequence File: " + file_path.string() + " not found");
128 // if file opened okay, read it
131 // search for the header of the fasta sequence we want
132 while ( (!data_file.eof()) && (header_counter < seq_num) )
134 getline(data_file,file_data_line);
135 if (file_data_line.substr(0,1) == ">")
139 header = file_data_line.substr(1);
143 while ( !data_file.eof() && read_seq )
145 getline(data_file,file_data_line);
146 if (file_data_line.substr(0,1) == ">")
148 else sequence_raw += file_data_line;
153 // Lastly, if subselection of the sequence was specified we keep cut out
154 // and only keep that part
155 // end_index = 0 means no end was specified, so cut to the end
157 end_index = sequence_raw.size();
159 // sequence filtering for upcasing agctn and convert non AGCTN to N
160 set_filtered_sequence(sequence_raw, start_index, end_index-start_index);
164 void Sequence::set_filtered_sequence(const string &old_seq,
165 string::size_type start,
166 string::size_type count)
168 char conversionTable[257];
171 count = old_seq.size() - start;
173 sequence.reserve(count);
175 // Make a conversion table
177 // everything we don't specify below will become 'N'
178 for(int table_i=0; table_i < 256; table_i++)
180 conversionTable[table_i] = 'N';
182 // add end of string character for printing out table for testing purposes
183 conversionTable[256] = '\0';
185 // we want these to map to themselves - ie not to change
186 conversionTable[(int)'A'] = 'A';
187 conversionTable[(int)'T'] = 'T';
188 conversionTable[(int)'G'] = 'G';
189 conversionTable[(int)'C'] = 'C';
191 conversionTable[(int)'a'] = 'A';
192 conversionTable[(int)'t'] = 'T';
193 conversionTable[(int)'g'] = 'G';
194 conversionTable[(int)'c'] = 'C';
196 // finally, the actual conversion loop
197 for(string::size_type seq_index = 0; seq_index < count; seq_index++)
199 sequence += conversionTable[ (int)old_seq[seq_index+start]];
203 // this doesn't work properly under gcc 3.x ... it can't recognize toupper
204 //transform(sequence.begin(), sequence.end(), sequence.begin(), toupper);
208 Sequence::load_annot(fs::path file_path, int start_index, int end_index)
210 fs::fstream data_file;
211 string file_data_line;
213 string::size_type space_split_i;
215 list<annot>::iterator list_i;
220 data_file.open(file_path, ios::in);
224 throw mussa_load_error("Sequence File: " + file_path.string() + " not found");
226 // if file opened okay, read it
229 getline(data_file,file_data_line);
230 species = file_data_line;
232 // end_index = 0 means no end was specified, so cut to the end
234 end_index = sequence.length();
236 //cout << "START: " << start_index << " END: " << end_index << endl;
238 while ( !data_file.eof() )
240 getline(data_file,file_data_line);
241 if (file_data_line != "")
243 // need to get 4 values...almost same code 4 times...
244 // get annot start index
245 space_split_i = file_data_line.find(" ");
246 annot_value = file_data_line.substr(0,space_split_i);
247 an_annot.start = atoi (annot_value.c_str());
248 file_data_line = file_data_line.substr(space_split_i+1);
249 // get annot end index
250 space_split_i = file_data_line.find(" ");
251 annot_value = file_data_line.substr(0,space_split_i);
252 an_annot.end = atoi (annot_value.c_str());
253 file_data_line = file_data_line.substr(space_split_i+1);
255 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
259 space_split_i = file_data_line.find(" ");
260 if (space_split_i == string::npos) // no entries for name & type
262 cout << "seq, annots - no name or type\n";
268 annot_value = file_data_line.substr(0,space_split_i);
269 an_annot.name = annot_value;
270 file_data_line = file_data_line.substr(space_split_i+1);
272 space_split_i = file_data_line.find(" ");
273 if (space_split_i == string::npos) // no entry for type
277 annot_value = file_data_line.substr(0,space_split_i);
278 an_annot.type = annot_value;
283 // add annot to list if it falls within the range of sequence specified
284 if ((start_index <= an_annot.start) && (end_index >= an_annot.end))
286 an_annot.start -= start_index;
287 an_annot.end -= start_index;
288 annots.push_back(an_annot);
290 // else no (or bogus) annotations
297 for(list_i = annots.begin(); list_i != annots.end(); ++list_i)
299 cout << (*list_i).start << "," << (*list_i).end << "\t";
300 cout << (*list_i).name << "\t" << (*list_i).type << endl;
306 const std::string& Sequence::get_species() const
311 bool Sequence::empty() const
313 return (size() == 0);
316 const std::list<annot>& Sequence::annotations() const
321 string::size_type Sequence::length() const
326 string::size_type Sequence::size() const
328 return sequence.size();
331 Sequence::iterator Sequence::begin()
333 return sequence.begin();
336 Sequence::const_iterator Sequence::begin() const
338 return sequence.begin();
341 Sequence::iterator Sequence::end()
343 return sequence.end();
346 Sequence::const_iterator Sequence::end() const
348 return sequence.end();
353 Sequence::get_seq() const
360 Sequence::subseq(int start, int end) const
362 return sequence.substr(start, end);
367 Sequence::c_seq() const
369 return sequence.c_str();
373 Sequence::rev_comp() const
376 char conversionTable[257];
377 int seq_i, table_i, len;
379 len = sequence.length();
380 rev_comp.reserve(len);
381 // make a conversion table
382 // init all parts of conversion table to '~' character
383 // '~' I doubt will ever appear in a sequence file (jeez, I hope)
384 // and may the fleas of 1000 camels infest the genitals of any biologist (and
385 // seven generations of their progeny) who decides to make it mean
386 // something special!!!
387 // PS - double the curse for any smartass non-biologist who tries it as well
388 for(table_i=0; table_i < 256; table_i++)
390 conversionTable[table_i] = '~';
392 // add end of string character for printing out table for testing purposes
393 conversionTable[256] = '\0';
395 // add in the characters for the bases we want to convert
396 conversionTable[(int)'A'] = 'T';
397 conversionTable[(int)'T'] = 'A';
398 conversionTable[(int)'G'] = 'C';
399 conversionTable[(int)'C'] = 'G';
400 conversionTable[(int)'N'] = 'N';
402 // finally, the actual conversion loop
403 for(seq_i = len - 1; seq_i >= 0; seq_i--)
405 table_i = (int) sequence[seq_i];
406 rev_comp += conversionTable[table_i];
414 Sequence::get_header() const
419 //FIXME: i don't think this code is callable
421 Sequence::sp_name() const
428 Sequence::set_seq(const string& a_seq)
430 set_filtered_sequence(a_seq);
452 Sequence::save(fs::fstream &save_file)
453 //string save_file_path)
456 list<annot>::iterator annots_i;
458 // not sure why, or if i'm doing something wrong, but can't seem to pass
459 // file pointers down to this method from the mussa control class
460 // so each call to save a sequence appends to the file started by mussa_class
461 //save_file.open(save_file_path.c_str(), ios::app);
463 save_file << "<Sequence>" << endl;
464 save_file << sequence << endl;
465 save_file << "</Sequence>" << endl;
467 save_file << "<Annotations>" << endl;
468 save_file << species << endl;
469 for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i)
471 save_file << annots_i->start << " " << annots_i->end << " " ;
472 save_file << annots_i->name << " " << annots_i->type << endl;
474 save_file << "</Annotations>" << endl;
479 Sequence::load_museq(fs::path load_file_path, int seq_num)
481 fs::fstream load_file;
482 string file_data_line;
485 string::size_type space_split_i;
489 load_file.open(load_file_path, ios::in);
492 // search for the seq_num-th sequence
493 while ( (!load_file.eof()) && (seq_counter < seq_num) )
495 getline(load_file,file_data_line);
496 if (file_data_line == "<Sequence>")
499 getline(load_file, file_data_line);
500 sequence = file_data_line;
501 getline(load_file, file_data_line);
502 getline(load_file, file_data_line);
503 if (file_data_line == "<Annotations>")
505 getline(load_file, file_data_line);
506 species = file_data_line;
507 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
509 getline(load_file,file_data_line);
510 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
512 // need to get 4 values...almost same code 4 times...
513 // get annot start index
514 space_split_i = file_data_line.find(" ");
515 annot_value = file_data_line.substr(0,space_split_i);
516 an_annot.start = atoi (annot_value.c_str());
517 file_data_line = file_data_line.substr(space_split_i+1);
518 // get annot end index
519 space_split_i = file_data_line.find(" ");
520 annot_value = file_data_line.substr(0,space_split_i);
521 an_annot.end = atoi (annot_value.c_str());
523 if (space_split_i == string::npos) // no entry for type or name
525 cout << "seq, annots - no type or name\n";
529 else // else get annot type
531 file_data_line = file_data_line.substr(space_split_i+1);
532 space_split_i = file_data_line.find(" ");
533 annot_value = file_data_line.substr(0,space_split_i);
534 an_annot.type = annot_value;
535 if (space_split_i == string::npos) // no entry for name
537 cout << "seq, annots - no name\n";
540 else // get annot name
542 file_data_line = file_data_line.substr(space_split_i+1);
543 space_split_i = file_data_line.find(" ");
544 annot_value = file_data_line.substr(0,space_split_i);
545 an_annot.type = annot_value;
548 annots.push_back(an_annot); // don't forget to actually add the annot
550 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
551 // << "-->" << an_annot.type << "::" << an_annot.name << endl;
559 Sequence::rc_motif(string a_motif)
562 char conversionTable[257];
563 int seq_i, table_i, len;
565 len = a_motif.length();
566 rev_comp.reserve(len);
568 for(table_i=0; table_i < 256; table_i++)
570 conversionTable[table_i] = '~';
572 // add end of string character for printing out table for testing purposes
573 conversionTable[256] = '\0';
575 // add in the characters for the bases we want to convert (IUPAC)
576 conversionTable[(int)'A'] = 'T';
577 conversionTable[(int)'T'] = 'A';
578 conversionTable[(int)'G'] = 'C';
579 conversionTable[(int)'C'] = 'G';
580 conversionTable[(int)'N'] = 'N';
581 conversionTable[(int)'M'] = 'K';
582 conversionTable[(int)'R'] = 'Y';
583 conversionTable[(int)'W'] = 'W';
584 conversionTable[(int)'S'] = 'S';
585 conversionTable[(int)'Y'] = 'R';
586 conversionTable[(int)'K'] = 'M';
587 conversionTable[(int)'V'] = 'B';
588 conversionTable[(int)'H'] = 'D';
589 conversionTable[(int)'D'] = 'H';
590 conversionTable[(int)'B'] = 'V';
592 // finally, the actual conversion loop
593 for(seq_i = len - 1; seq_i >= 0; seq_i--)
595 //cout << "** i = " << seq_i << " bp = " <<
596 table_i = (int) a_motif[seq_i];
597 rev_comp += conversionTable[table_i];
600 //cout << "seq: " << a_motif << endl;
601 //cout << "rc: " << rev_comp << endl;
607 Sequence::motif_normalize(string a_motif)
612 len = a_motif.length();
613 valid_motif.reserve(len);
615 // this just upcases IUPAC symbols. Eventually should return an error if non IUPAC is present.
616 // current nonIUPAC symbols are omitted, which is not reported atm
617 for(seq_i = 0; seq_i < len; seq_i++)
619 if ((a_motif[seq_i] == 'a') || (a_motif[seq_i] == 'A'))
621 else if ((a_motif[seq_i] == 't') || (a_motif[seq_i] == 'T'))
623 else if ((a_motif[seq_i] == 'g') || (a_motif[seq_i] == 'G'))
625 else if ((a_motif[seq_i] == 'c') || (a_motif[seq_i] == 'C'))
627 else if ((a_motif[seq_i] == 'n') || (a_motif[seq_i] == 'N'))
629 else if ((a_motif[seq_i] == 'm') || (a_motif[seq_i] == 'M'))
631 else if ((a_motif[seq_i] == 'r') || (a_motif[seq_i] == 'R'))
633 else if ((a_motif[seq_i] == 'w') || (a_motif[seq_i] == 'W'))
635 else if ((a_motif[seq_i] == 's') || (a_motif[seq_i] == 'S'))
637 else if ((a_motif[seq_i] == 'y') || (a_motif[seq_i] == 'Y'))
639 else if ((a_motif[seq_i] == 'k') || (a_motif[seq_i] == 'K'))
641 else if ((a_motif[seq_i] == 'v') || (a_motif[seq_i] == 'V'))
643 else if ((a_motif[seq_i] == 'h') || (a_motif[seq_i] == 'H'))
645 else if ((a_motif[seq_i] == 'd') || (a_motif[seq_i] == 'D'))
647 else if ((a_motif[seq_i] == 'b') || (a_motif[seq_i] == 'B'))
650 string msg = "Letter ";
651 msg += a_motif[seq_i];
652 msg += " is not a valid IUPAC symbol";
653 throw motif_normalize_error(msg);
656 //cout << "valid_motif is: " << valid_motif << endl;
660 void Sequence::add_motif(string a_motif)
662 vector<int> motif_starts = find_motif(a_motif);
664 for(vector<int>::iterator motif_start_i = motif_starts.begin();
665 motif_start_i != motif_starts.end();
668 motif_list.push_back(motif(*motif_start_i, a_motif));
672 void Sequence::clear_motifs()
677 const list<motif>& Sequence::motifs() const
683 Sequence::find_motif(string a_motif)
685 vector<int> motif_match_starts;
688 motif_match_starts.clear();
690 //cout << "motif is: " << a_motif << endl;
691 a_motif = motif_normalize(a_motif);
692 //cout << "motif is: " << a_motif << endl;
696 //cout << "Sequence: none blank motif\n";
697 motif_scan(a_motif, &motif_match_starts);
699 a_motif_rc = rc_motif(a_motif);
700 // make sure not to do search again if it is a palindrome
701 if (a_motif_rc != a_motif)
702 motif_scan(a_motif_rc, &motif_match_starts);
704 return motif_match_starts;
708 Sequence::motif_scan(string a_motif, vector<int> * motif_match_starts)
711 string::size_type seq_i;
712 int motif_i, motif_len;
714 // faster to loop thru the sequence as a old c string (ie char array)
715 seq_c = (char*)sequence.c_str();
716 //cout << "Sequence: motif, seq len = " << sequence.length() << endl;
717 motif_len = a_motif.length();
719 //cout << "motif_length: " << motif_len << endl;
720 //cout << "RAAARRRRR\n";
724 //cout << "motif: " << a_motif << endl;
726 //cout << "Sequence: motif, length= " << length << endl;
728 while (seq_i < sequence.length())
730 //cout << seq_c[seq_i];
731 //cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
732 // this is pretty much a straight translation of Nora's python code
733 // to match iupac letter codes
734 if (a_motif[motif_i] =='N')
736 else if (a_motif[motif_i] == seq_c[seq_i])
738 else if ((a_motif[motif_i] =='M') &&
739 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C')))
741 else if ((a_motif[motif_i] =='R') &&
742 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G')))
744 else if ((a_motif[motif_i] =='W') &&
745 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='T')))
747 else if ((a_motif[motif_i] =='S') &&
748 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G')))
750 else if ((a_motif[motif_i] =='Y') &&
751 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='T')))
753 else if ((a_motif[motif_i] =='K') &&
754 ((seq_c[seq_i]=='G') || (seq_c[seq_i]=='T')))
756 else if ((a_motif[motif_i] =='V') &&
757 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
758 (seq_c[seq_i]=='G')))
760 else if ((a_motif[seq_i] =='H') &&
761 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
762 (seq_c[seq_i]=='T')))
764 else if ((a_motif[motif_i] =='D') &&
765 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G') ||
766 (seq_c[seq_i]=='T')))
768 else if ((a_motif[motif_i] =='B') &&
769 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G') ||
770 (seq_c[seq_i]=='T')))
779 // end Nora stuff, now we see if a match is found this pass
780 if (motif_i == motif_len)
784 motif_match_starts->push_back(seq_i - motif_len + 1);