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 if (header_counter > 0) {
140 header = file_data_line.substr(1);
144 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;
151 // Lastly, if subselection of the sequence was specified we keep cut out
152 // and only keep that part
153 // end_index = 0 means no end was specified, so cut to the end
155 end_index = sequence_raw.size();
157 // sequence filtering for upcasing agctn and convert non AGCTN to N
158 set_filtered_sequence(sequence_raw, start_index, end_index-start_index);
160 throw mussa_load_error("%s did not have a fasta header");
166 void Sequence::set_filtered_sequence(const string &old_seq,
167 string::size_type start,
168 string::size_type count)
170 char conversionTable[257];
173 count = old_seq.size() - start;
175 sequence.reserve(count);
177 // Make a conversion table
179 // everything we don't specify below will become 'N'
180 for(int table_i=0; table_i < 256; table_i++)
182 conversionTable[table_i] = 'N';
184 // add end of string character for printing out table for testing purposes
185 conversionTable[256] = '\0';
187 // we want these to map to themselves - ie not to change
188 conversionTable[(int)'A'] = 'A';
189 conversionTable[(int)'T'] = 'T';
190 conversionTable[(int)'G'] = 'G';
191 conversionTable[(int)'C'] = 'C';
193 conversionTable[(int)'a'] = 'A';
194 conversionTable[(int)'t'] = 'T';
195 conversionTable[(int)'g'] = 'G';
196 conversionTable[(int)'c'] = 'C';
198 // finally, the actual conversion loop
199 for(string::size_type seq_index = 0; seq_index < count; seq_index++)
201 sequence += conversionTable[ (int)old_seq[seq_index+start]];
205 // this doesn't work properly under gcc 3.x ... it can't recognize toupper
206 //transform(sequence.begin(), sequence.end(), sequence.begin(), toupper);
210 Sequence::load_annot(fs::path file_path, int start_index, int end_index)
212 fs::fstream data_file;
213 string file_data_line;
215 string::size_type space_split_i;
217 list<annot>::iterator list_i;
222 data_file.open(file_path, ios::in);
226 throw mussa_load_error("Sequence File: " + file_path.string() + " not found");
228 // if file opened okay, read it
231 getline(data_file,file_data_line);
232 species = file_data_line;
234 // end_index = 0 means no end was specified, so cut to the end
236 end_index = sequence.length();
238 //cout << "START: " << start_index << " END: " << end_index << endl;
240 while ( !data_file.eof() )
242 getline(data_file,file_data_line);
243 if (file_data_line != "")
245 // need to get 4 values...almost same code 4 times...
246 // get annot start index
247 space_split_i = file_data_line.find(" ");
248 annot_value = file_data_line.substr(0,space_split_i);
249 an_annot.start = atoi (annot_value.c_str());
250 file_data_line = file_data_line.substr(space_split_i+1);
251 // get annot end index
252 space_split_i = file_data_line.find(" ");
253 annot_value = file_data_line.substr(0,space_split_i);
254 an_annot.end = atoi (annot_value.c_str());
255 file_data_line = file_data_line.substr(space_split_i+1);
257 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
261 space_split_i = file_data_line.find(" ");
262 if (space_split_i == string::npos) // no entries for name & type
264 cout << "seq, annots - no name or type\n";
270 annot_value = file_data_line.substr(0,space_split_i);
271 an_annot.name = annot_value;
272 file_data_line = file_data_line.substr(space_split_i+1);
274 space_split_i = file_data_line.find(" ");
275 if (space_split_i == string::npos) // no entry for type
279 annot_value = file_data_line.substr(0,space_split_i);
280 an_annot.type = annot_value;
285 // add annot to list if it falls within the range of sequence specified
286 if ((start_index <= an_annot.start) && (end_index >= an_annot.end))
288 an_annot.start -= start_index;
289 an_annot.end -= start_index;
290 annots.push_back(an_annot);
292 // else no (or bogus) annotations
299 for(list_i = annots.begin(); list_i != annots.end(); ++list_i)
301 cout << (*list_i).start << "," << (*list_i).end << "\t";
302 cout << (*list_i).name << "\t" << (*list_i).type << endl;
308 const std::string& Sequence::get_species() const
313 bool Sequence::empty() const
315 return (size() == 0);
318 const std::list<annot>& Sequence::annotations() const
323 string::size_type Sequence::length() const
328 string::size_type Sequence::size() const
330 return sequence.size();
333 Sequence::iterator Sequence::begin()
335 return sequence.begin();
338 Sequence::const_iterator Sequence::begin() const
340 return sequence.begin();
343 Sequence::iterator Sequence::end()
345 return sequence.end();
348 Sequence::const_iterator Sequence::end() const
350 return sequence.end();
355 Sequence::get_seq() const
362 Sequence::subseq(int start, int end) const
364 return sequence.substr(start, end);
369 Sequence::c_seq() const
371 return sequence.c_str();
375 Sequence::rev_comp() const
378 char conversionTable[257];
379 int seq_i, table_i, len;
381 len = sequence.length();
382 rev_comp.reserve(len);
383 // make a conversion table
384 // init all parts of conversion table to '~' character
385 // '~' I doubt will ever appear in a sequence file (jeez, I hope)
386 // and may the fleas of 1000 camels infest the genitals of any biologist (and
387 // seven generations of their progeny) who decides to make it mean
388 // something special!!!
389 // PS - double the curse for any smartass non-biologist who tries it as well
390 for(table_i=0; table_i < 256; table_i++)
392 conversionTable[table_i] = '~';
394 // add end of string character for printing out table for testing purposes
395 conversionTable[256] = '\0';
397 // add in the characters for the bases we want to convert
398 conversionTable[(int)'A'] = 'T';
399 conversionTable[(int)'T'] = 'A';
400 conversionTable[(int)'G'] = 'C';
401 conversionTable[(int)'C'] = 'G';
402 conversionTable[(int)'N'] = 'N';
404 // finally, the actual conversion loop
405 for(seq_i = len - 1; seq_i >= 0; seq_i--)
407 table_i = (int) sequence[seq_i];
408 rev_comp += conversionTable[table_i];
416 Sequence::get_header() const
421 //FIXME: i don't think this code is callable
423 Sequence::sp_name() const
430 Sequence::set_seq(const string& a_seq)
432 set_filtered_sequence(a_seq);
454 Sequence::save(fs::fstream &save_file)
455 //string save_file_path)
458 list<annot>::iterator annots_i;
460 // not sure why, or if i'm doing something wrong, but can't seem to pass
461 // file pointers down to this method from the mussa control class
462 // so each call to save a sequence appends to the file started by mussa_class
463 //save_file.open(save_file_path.c_str(), ios::app);
465 save_file << "<Sequence>" << endl;
466 save_file << sequence << endl;
467 save_file << "</Sequence>" << endl;
469 save_file << "<Annotations>" << endl;
470 save_file << species << endl;
471 for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i)
473 save_file << annots_i->start << " " << annots_i->end << " " ;
474 save_file << annots_i->name << " " << annots_i->type << endl;
476 save_file << "</Annotations>" << endl;
481 Sequence::load_museq(fs::path load_file_path, int seq_num)
483 fs::fstream load_file;
484 string file_data_line;
487 string::size_type space_split_i;
491 load_file.open(load_file_path, ios::in);
494 // search for the seq_num-th sequence
495 while ( (!load_file.eof()) && (seq_counter < seq_num) )
497 getline(load_file,file_data_line);
498 if (file_data_line == "<Sequence>")
501 getline(load_file, file_data_line);
502 sequence = file_data_line;
503 getline(load_file, file_data_line);
504 getline(load_file, file_data_line);
505 if (file_data_line == "<Annotations>")
507 getline(load_file, file_data_line);
508 species = file_data_line;
509 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
511 getline(load_file,file_data_line);
512 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
514 // need to get 4 values...almost same code 4 times...
515 // get annot start index
516 space_split_i = file_data_line.find(" ");
517 annot_value = file_data_line.substr(0,space_split_i);
518 an_annot.start = atoi (annot_value.c_str());
519 file_data_line = file_data_line.substr(space_split_i+1);
520 // get annot end index
521 space_split_i = file_data_line.find(" ");
522 annot_value = file_data_line.substr(0,space_split_i);
523 an_annot.end = atoi (annot_value.c_str());
525 if (space_split_i == string::npos) // no entry for type or name
527 cout << "seq, annots - no type or name\n";
531 else // else get annot type
533 file_data_line = file_data_line.substr(space_split_i+1);
534 space_split_i = file_data_line.find(" ");
535 annot_value = file_data_line.substr(0,space_split_i);
536 an_annot.type = annot_value;
537 if (space_split_i == string::npos) // no entry for name
539 cout << "seq, annots - no name\n";
542 else // get annot name
544 file_data_line = file_data_line.substr(space_split_i+1);
545 space_split_i = file_data_line.find(" ");
546 annot_value = file_data_line.substr(0,space_split_i);
547 an_annot.type = annot_value;
550 annots.push_back(an_annot); // don't forget to actually add the annot
552 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
553 // << "-->" << an_annot.type << "::" << an_annot.name << endl;
561 Sequence::rc_motif(string a_motif)
564 char conversionTable[257];
565 int seq_i, table_i, len;
567 len = a_motif.length();
568 rev_comp.reserve(len);
570 for(table_i=0; table_i < 256; table_i++)
572 conversionTable[table_i] = '~';
574 // add end of string character for printing out table for testing purposes
575 conversionTable[256] = '\0';
577 // add in the characters for the bases we want to convert (IUPAC)
578 conversionTable[(int)'A'] = 'T';
579 conversionTable[(int)'T'] = 'A';
580 conversionTable[(int)'G'] = 'C';
581 conversionTable[(int)'C'] = 'G';
582 conversionTable[(int)'N'] = 'N';
583 conversionTable[(int)'M'] = 'K';
584 conversionTable[(int)'R'] = 'Y';
585 conversionTable[(int)'W'] = 'W';
586 conversionTable[(int)'S'] = 'S';
587 conversionTable[(int)'Y'] = 'R';
588 conversionTable[(int)'K'] = 'M';
589 conversionTable[(int)'V'] = 'B';
590 conversionTable[(int)'H'] = 'D';
591 conversionTable[(int)'D'] = 'H';
592 conversionTable[(int)'B'] = 'V';
594 // finally, the actual conversion loop
595 for(seq_i = len - 1; seq_i >= 0; seq_i--)
597 //cout << "** i = " << seq_i << " bp = " <<
598 table_i = (int) a_motif[seq_i];
599 rev_comp += conversionTable[table_i];
602 //cout << "seq: " << a_motif << endl;
603 //cout << "rc: " << rev_comp << endl;
609 Sequence::motif_normalize(string a_motif)
614 len = a_motif.length();
615 valid_motif.reserve(len);
617 // this just upcases IUPAC symbols. Eventually should return an error if non IUPAC is present.
618 // current nonIUPAC symbols are omitted, which is not reported atm
619 for(seq_i = 0; seq_i < len; seq_i++)
621 if ((a_motif[seq_i] == 'a') || (a_motif[seq_i] == 'A'))
623 else if ((a_motif[seq_i] == 't') || (a_motif[seq_i] == 'T'))
625 else if ((a_motif[seq_i] == 'g') || (a_motif[seq_i] == 'G'))
627 else if ((a_motif[seq_i] == 'c') || (a_motif[seq_i] == 'C'))
629 else if ((a_motif[seq_i] == 'n') || (a_motif[seq_i] == 'N'))
631 else if ((a_motif[seq_i] == 'm') || (a_motif[seq_i] == 'M'))
633 else if ((a_motif[seq_i] == 'r') || (a_motif[seq_i] == 'R'))
635 else if ((a_motif[seq_i] == 'w') || (a_motif[seq_i] == 'W'))
637 else if ((a_motif[seq_i] == 's') || (a_motif[seq_i] == 'S'))
639 else if ((a_motif[seq_i] == 'y') || (a_motif[seq_i] == 'Y'))
641 else if ((a_motif[seq_i] == 'k') || (a_motif[seq_i] == 'K'))
643 else if ((a_motif[seq_i] == 'v') || (a_motif[seq_i] == 'V'))
645 else if ((a_motif[seq_i] == 'h') || (a_motif[seq_i] == 'H'))
647 else if ((a_motif[seq_i] == 'd') || (a_motif[seq_i] == 'D'))
649 else if ((a_motif[seq_i] == 'b') || (a_motif[seq_i] == 'B'))
652 string msg = "Letter ";
653 msg += a_motif[seq_i];
654 msg += " is not a valid IUPAC symbol";
655 throw motif_normalize_error(msg);
658 //cout << "valid_motif is: " << valid_motif << endl;
662 void Sequence::add_motif(string a_motif)
664 vector<int> motif_starts = find_motif(a_motif);
666 for(vector<int>::iterator motif_start_i = motif_starts.begin();
667 motif_start_i != motif_starts.end();
670 motif_list.push_back(motif(*motif_start_i, a_motif));
674 void Sequence::clear_motifs()
679 const list<motif>& Sequence::motifs() const
685 Sequence::find_motif(string a_motif)
687 vector<int> motif_match_starts;
690 motif_match_starts.clear();
692 //cout << "motif is: " << a_motif << endl;
693 a_motif = motif_normalize(a_motif);
694 //cout << "motif is: " << a_motif << endl;
698 //cout << "Sequence: none blank motif\n";
699 motif_scan(a_motif, &motif_match_starts);
701 a_motif_rc = rc_motif(a_motif);
702 // make sure not to do search again if it is a palindrome
703 if (a_motif_rc != a_motif)
704 motif_scan(a_motif_rc, &motif_match_starts);
706 return motif_match_starts;
710 Sequence::motif_scan(string a_motif, vector<int> * motif_match_starts)
713 string::size_type seq_i;
714 int motif_i, motif_len;
716 // faster to loop thru the sequence as a old c string (ie char array)
717 seq_c = (char*)sequence.c_str();
718 //cout << "Sequence: motif, seq len = " << sequence.length() << endl;
719 motif_len = a_motif.length();
721 //cout << "motif_length: " << motif_len << endl;
722 //cout << "RAAARRRRR\n";
726 //cout << "motif: " << a_motif << endl;
728 //cout << "Sequence: motif, length= " << length << endl;
730 while (seq_i < sequence.length())
732 //cout << seq_c[seq_i];
733 //cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
734 // this is pretty much a straight translation of Nora's python code
735 // to match iupac letter codes
736 if (a_motif[motif_i] =='N')
738 else if (a_motif[motif_i] == seq_c[seq_i])
740 else if ((a_motif[motif_i] =='M') &&
741 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C')))
743 else if ((a_motif[motif_i] =='R') &&
744 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G')))
746 else if ((a_motif[motif_i] =='W') &&
747 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='T')))
749 else if ((a_motif[motif_i] =='S') &&
750 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G')))
752 else if ((a_motif[motif_i] =='Y') &&
753 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='T')))
755 else if ((a_motif[motif_i] =='K') &&
756 ((seq_c[seq_i]=='G') || (seq_c[seq_i]=='T')))
758 else if ((a_motif[motif_i] =='V') &&
759 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
760 (seq_c[seq_i]=='G')))
762 else if ((a_motif[seq_i] =='H') &&
763 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
764 (seq_c[seq_i]=='T')))
766 else if ((a_motif[motif_i] =='D') &&
767 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G') ||
768 (seq_c[seq_i]=='T')))
770 else if ((a_motif[motif_i] =='B') &&
771 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G') ||
772 (seq_c[seq_i]=='T')))
781 // end Nora stuff, now we see if a match is found this pass
782 if (motif_i == motif_len)
786 motif_match_starts->push_back(seq_i - motif_len + 1);