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 char Sequence::operator[](int index) const
88 return sequence[index];
91 ostream& operator<<(ostream& out, const Sequence& seq)
93 out << "Sequence(" << seq.get_seq() << ")";
97 //! load a fasta file into a sequence
99 * \param file_path the location of the fasta file in the filesystem
100 * \param seq_num which sequence in the file to load
101 * \param start_index starting position in the fasta sequence, 0 for beginning
102 * \param end_index ending position in the fasta sequence, 0 for end
103 * \return error message, empty string if no error. (gag!)
106 Sequence::load_fasta(string file_path, int seq_num,
107 int start_index, int end_index)
110 string file_data_line;
111 int header_counter = 0;
112 bool read_seq = true;
115 string seq_tmp; // holds sequence during basic filtering
117 data_file.open(file_path.c_str(), ios::in);
120 throw mussa_load_error("fasta sequence number is 1 based (can't be 0)");
124 throw mussa_load_error("Sequence File: " + file_path + " not found");
126 // if file opened okay, read it
129 // search for the header of the fasta sequence we want
130 while ( (!data_file.eof()) && (header_counter < seq_num) )
132 getline(data_file,file_data_line);
133 if (file_data_line.substr(0,1) == ">")
137 header = file_data_line.substr(1);
141 while ( !data_file.eof() && read_seq )
143 getline(data_file,file_data_line);
144 if (file_data_line.substr(0,1) == ">")
146 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);
162 void Sequence::set_filtered_sequence(const string &old_seq,
163 string::size_type start,
164 string::size_type count)
166 char conversionTable[257];
169 count = old_seq.size() - start;
171 sequence.reserve(count);
173 // Make a conversion table
175 // everything we don't specify below will become 'N'
176 for(int table_i=0; table_i < 256; table_i++)
178 conversionTable[table_i] = 'N';
180 // add end of string character for printing out table for testing purposes
181 conversionTable[256] = '\0';
183 // we want these to map to themselves - ie not to change
184 conversionTable[(int)'A'] = 'A';
185 conversionTable[(int)'T'] = 'T';
186 conversionTable[(int)'G'] = 'G';
187 conversionTable[(int)'C'] = 'C';
189 conversionTable[(int)'a'] = 'A';
190 conversionTable[(int)'t'] = 'T';
191 conversionTable[(int)'g'] = 'G';
192 conversionTable[(int)'c'] = 'C';
194 // finally, the actual conversion loop
195 for(string::size_type seq_index = 0; seq_index < count; seq_index++)
197 sequence += conversionTable[ (int)old_seq[seq_index+start]];
201 // this doesn't work properly under gcc 3.x ... it can't recognize toupper
202 //transform(sequence.begin(), sequence.end(), sequence.begin(), toupper);
206 Sequence::load_annot(string file_path, int start_index, int end_index)
209 string file_data_line;
211 string::size_type space_split_i;
213 list<annot>::iterator list_i;
218 data_file.open(file_path.c_str(), ios::in);
222 throw mussa_load_error("Sequence File: " + file_path + " not found");
224 // if file opened okay, read it
227 getline(data_file,file_data_line);
228 species = file_data_line;
230 // end_index = 0 means no end was specified, so cut to the end
232 end_index = sequence.length();
234 //cout << "START: " << start_index << " END: " << end_index << endl;
236 while ( !data_file.eof() )
238 getline(data_file,file_data_line);
239 if (file_data_line != "")
241 // need to get 4 values...almost same code 4 times...
242 // get annot start index
243 space_split_i = file_data_line.find(" ");
244 annot_value = file_data_line.substr(0,space_split_i);
245 an_annot.start = atoi (annot_value.c_str());
246 file_data_line = file_data_line.substr(space_split_i+1);
247 // get annot end index
248 space_split_i = file_data_line.find(" ");
249 annot_value = file_data_line.substr(0,space_split_i);
250 an_annot.end = atoi (annot_value.c_str());
251 file_data_line = file_data_line.substr(space_split_i+1);
253 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
257 space_split_i = file_data_line.find(" ");
258 if (space_split_i == string::npos) // no entries for name & type
260 cout << "seq, annots - no name or type\n";
266 annot_value = file_data_line.substr(0,space_split_i);
267 an_annot.name = annot_value;
268 file_data_line = file_data_line.substr(space_split_i+1);
270 space_split_i = file_data_line.find(" ");
271 if (space_split_i == string::npos) // no entry for type
275 annot_value = file_data_line.substr(0,space_split_i);
276 an_annot.type = annot_value;
281 // add annot to list if it falls within the range of sequence specified
282 if ((start_index <= an_annot.start) && (end_index >= an_annot.end))
284 an_annot.start -= start_index;
285 an_annot.end -= start_index;
286 annots.push_back(an_annot);
289 cout << "FAILED!!!!!!\n";
296 for(list_i = annots.begin(); list_i != annots.end(); ++list_i)
298 cout << (*list_i).start << "," << (*list_i).end << "\t";
299 cout << (*list_i).name << "\t" << (*list_i).type << endl;
305 bool Sequence::empty() const
307 return (size() == 0);
310 const std::list<annot>& Sequence::annotations() const
315 string::size_type Sequence::length() const
320 string::size_type Sequence::size() const
322 return sequence.size();
325 Sequence::iterator Sequence::begin()
327 return sequence.begin();
330 Sequence::const_iterator Sequence::begin() const
332 return sequence.begin();
335 Sequence::iterator Sequence::end()
337 return sequence.end();
340 Sequence::const_iterator Sequence::end() const
342 return sequence.end();
347 Sequence::get_seq() const
354 Sequence::subseq(int start, int end) const
356 return sequence.substr(start, end);
361 Sequence::c_seq() const
363 return sequence.c_str();
367 Sequence::rev_comp() const
370 char conversionTable[257];
371 int seq_i, table_i, len;
373 len = sequence.length();
374 rev_comp.reserve(len);
375 // make a conversion table
376 // init all parts of conversion table to '~' character
377 // '~' I doubt will ever appear in a sequence file (jeez, I hope)
378 // and may the fleas of 1000 camels infest the genitals of any biologist (and
379 // seven generations of their progeny) who decides to make it mean
380 // something special!!!
381 // PS - double the curse for any smartass non-biologist who tries it as well
382 for(table_i=0; table_i < 256; table_i++)
384 conversionTable[table_i] = '~';
386 // add end of string character for printing out table for testing purposes
387 conversionTable[256] = '\0';
389 // add in the characters for the bases we want to convert
390 conversionTable[(int)'A'] = 'T';
391 conversionTable[(int)'T'] = 'A';
392 conversionTable[(int)'G'] = 'C';
393 conversionTable[(int)'C'] = 'G';
394 conversionTable[(int)'N'] = 'N';
396 // finally, the actual conversion loop
397 for(seq_i = len - 1; seq_i >= 0; seq_i--)
399 table_i = (int) sequence[seq_i];
400 rev_comp += conversionTable[table_i];
408 Sequence::get_header() const
413 //FIXME: i don't think this code is callable
415 Sequence::sp_name() const
422 Sequence::set_seq(const string& a_seq)
424 set_filtered_sequence(a_seq);
446 Sequence::save(fstream &save_file)
447 //string save_file_path)
450 list<annot>::iterator annots_i;
452 // not sure why, or if i'm doing something wrong, but can't seem to pass
453 // file pointers down to this method from the mussa control class
454 // so each call to save a sequence appends to the file started by mussa_class
455 //save_file.open(save_file_path.c_str(), ios::app);
457 save_file << "<Sequence>" << endl;
458 save_file << sequence << endl;
459 save_file << "</Sequence>" << endl;
461 save_file << "<Annotations>" << endl;
462 save_file << species << endl;
463 for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i)
465 save_file << annots_i->start << " " << annots_i->end << " " ;
466 save_file << annots_i->name << " " << annots_i->type << endl;
468 save_file << "</Annotations>" << endl;
473 Sequence::load_museq(string load_file_path, int seq_num)
476 string file_data_line;
479 string::size_type space_split_i;
483 load_file.open(load_file_path.c_str(), ios::in);
486 // search for the seq_num-th sequence
487 while ( (!load_file.eof()) && (seq_counter < seq_num) )
489 getline(load_file,file_data_line);
490 if (file_data_line == "<Sequence>")
493 getline(load_file, file_data_line);
494 sequence = file_data_line;
495 getline(load_file, file_data_line);
496 getline(load_file, file_data_line);
497 if (file_data_line == "<Annotations>")
499 getline(load_file, file_data_line);
500 species = file_data_line;
501 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
503 getline(load_file,file_data_line);
504 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
506 // need to get 4 values...almost same code 4 times...
507 // get annot start index
508 space_split_i = file_data_line.find(" ");
509 annot_value = file_data_line.substr(0,space_split_i);
510 an_annot.start = atoi (annot_value.c_str());
511 file_data_line = file_data_line.substr(space_split_i+1);
512 // get annot end index
513 space_split_i = file_data_line.find(" ");
514 annot_value = file_data_line.substr(0,space_split_i);
515 an_annot.end = atoi (annot_value.c_str());
517 if (space_split_i == string::npos) // no entry for type or name
519 cout << "seq, annots - no type or name\n";
523 else // else get annot type
525 file_data_line = file_data_line.substr(space_split_i+1);
526 space_split_i = file_data_line.find(" ");
527 annot_value = file_data_line.substr(0,space_split_i);
528 an_annot.type = annot_value;
529 if (space_split_i == string::npos) // no entry for name
531 cout << "seq, annots - no name\n";
534 else // get annot name
536 file_data_line = file_data_line.substr(space_split_i+1);
537 space_split_i = file_data_line.find(" ");
538 annot_value = file_data_line.substr(0,space_split_i);
539 an_annot.type = annot_value;
542 annots.push_back(an_annot); // don't forget to actually add the annot
544 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
545 // << "-->" << an_annot.type << "::" << an_annot.name << endl;
553 Sequence::rc_motif(string a_motif)
556 char conversionTable[257];
557 int seq_i, table_i, len;
559 len = a_motif.length();
560 rev_comp.reserve(len);
562 for(table_i=0; table_i < 256; table_i++)
564 conversionTable[table_i] = '~';
566 // add end of string character for printing out table for testing purposes
567 conversionTable[256] = '\0';
569 // add in the characters for the bases we want to convert (IUPAC)
570 conversionTable[(int)'A'] = 'T';
571 conversionTable[(int)'T'] = 'A';
572 conversionTable[(int)'G'] = 'C';
573 conversionTable[(int)'C'] = 'G';
574 conversionTable[(int)'N'] = 'N';
575 conversionTable[(int)'M'] = 'K';
576 conversionTable[(int)'R'] = 'Y';
577 conversionTable[(int)'W'] = 'W';
578 conversionTable[(int)'S'] = 'S';
579 conversionTable[(int)'Y'] = 'R';
580 conversionTable[(int)'K'] = 'M';
581 conversionTable[(int)'V'] = 'B';
582 conversionTable[(int)'H'] = 'D';
583 conversionTable[(int)'D'] = 'H';
584 conversionTable[(int)'B'] = 'V';
586 // finally, the actual conversion loop
587 for(seq_i = len - 1; seq_i >= 0; seq_i--)
589 //cout << "** i = " << seq_i << " bp = " <<
590 table_i = (int) a_motif[seq_i];
591 rev_comp += conversionTable[table_i];
594 //cout << "seq: " << a_motif << endl;
595 //cout << "rc: " << rev_comp << endl;
601 Sequence::motif_normalize(string a_motif)
606 len = a_motif.length();
607 valid_motif.reserve(len);
609 // this just upcases IUPAC symbols. Eventually should return an error if non IUPAC is present.
610 // current nonIUPAC symbols are omitted, which is not reported atm
611 for(seq_i = 0; seq_i < len; seq_i++)
613 if ((a_motif[seq_i] == 'a') || (a_motif[seq_i] == 'A'))
615 else if ((a_motif[seq_i] == 't') || (a_motif[seq_i] == 'T'))
617 else if ((a_motif[seq_i] == 'g') || (a_motif[seq_i] == 'G'))
619 else if ((a_motif[seq_i] == 'c') || (a_motif[seq_i] == 'C'))
621 else if ((a_motif[seq_i] == 'n') || (a_motif[seq_i] == 'N'))
623 else if ((a_motif[seq_i] == 'm') || (a_motif[seq_i] == 'M'))
625 else if ((a_motif[seq_i] == 'r') || (a_motif[seq_i] == 'R'))
627 else if ((a_motif[seq_i] == 'w') || (a_motif[seq_i] == 'W'))
629 else if ((a_motif[seq_i] == 's') || (a_motif[seq_i] == 'S'))
631 else if ((a_motif[seq_i] == 'y') || (a_motif[seq_i] == 'Y'))
633 else if ((a_motif[seq_i] == 'k') || (a_motif[seq_i] == 'K'))
635 else if ((a_motif[seq_i] == 'v') || (a_motif[seq_i] == 'V'))
637 else if ((a_motif[seq_i] == 'h') || (a_motif[seq_i] == 'H'))
639 else if ((a_motif[seq_i] == 'd') || (a_motif[seq_i] == 'D'))
641 else if ((a_motif[seq_i] == 'b') || (a_motif[seq_i] == 'B'))
644 string msg = "Letter ";
645 msg += a_motif[seq_i];
646 msg += " is not a valid IUPAC symbol";
647 throw motif_normalize_error(msg);
650 //cout << "valid_motif is: " << valid_motif << endl;
654 void Sequence::add_motif(string a_motif)
656 vector<int> motif_starts = find_motif(a_motif);
658 for(vector<int>::iterator motif_start_i = motif_starts.begin();
659 motif_start_i != motif_starts.end();
662 motif_list.push_back(motif(*motif_start_i, a_motif));
666 void Sequence::clear_motifs()
671 const list<motif>& Sequence::motifs() const
677 Sequence::find_motif(string a_motif)
679 vector<int> motif_match_starts;
682 motif_match_starts.clear();
684 //cout << "motif is: " << a_motif << endl;
685 a_motif = motif_normalize(a_motif);
686 //cout << "motif is: " << a_motif << endl;
690 //cout << "Sequence: none blank motif\n";
691 motif_scan(a_motif, &motif_match_starts);
693 a_motif_rc = rc_motif(a_motif);
694 // make sure not to do search again if it is a palindrome
695 if (a_motif_rc != a_motif)
696 motif_scan(a_motif_rc, &motif_match_starts);
698 return motif_match_starts;
702 Sequence::motif_scan(string a_motif, vector<int> * motif_match_starts)
705 string::size_type seq_i;
706 int motif_i, motif_len;
708 // faster to loop thru the sequence as a old c string (ie char array)
709 seq_c = (char*)sequence.c_str();
710 //cout << "Sequence: motif, seq len = " << sequence.length() << endl;
711 motif_len = a_motif.length();
713 //cout << "motif_length: " << motif_len << endl;
714 //cout << "RAAARRRRR\n";
718 //cout << "motif: " << a_motif << endl;
720 //cout << "Sequence: motif, length= " << length << endl;
722 while (seq_i < sequence.length())
724 //cout << seq_c[seq_i];
725 //cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
726 // this is pretty much a straight translation of Nora's python code
727 // to match iupac letter codes
728 if (a_motif[motif_i] =='N')
730 else if (a_motif[motif_i] == seq_c[seq_i])
732 else if ((a_motif[motif_i] =='M') &&
733 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C')))
735 else if ((a_motif[motif_i] =='R') &&
736 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G')))
738 else if ((a_motif[motif_i] =='W') &&
739 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='T')))
741 else if ((a_motif[motif_i] =='S') &&
742 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G')))
744 else if ((a_motif[motif_i] =='Y') &&
745 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='T')))
747 else if ((a_motif[motif_i] =='K') &&
748 ((seq_c[seq_i]=='G') || (seq_c[seq_i]=='T')))
750 else if ((a_motif[motif_i] =='V') &&
751 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
752 (seq_c[seq_i]=='G')))
754 else if ((a_motif[seq_i] =='H') &&
755 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
756 (seq_c[seq_i]=='T')))
758 else if ((a_motif[motif_i] =='D') &&
759 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G') ||
760 (seq_c[seq_i]=='T')))
762 else if ((a_motif[motif_i] =='B') &&
763 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G') ||
764 (seq_c[seq_i]=='T')))
773 // end Nora stuff, now we see if a match is found this pass
774 if (motif_i == motif_len)
778 motif_match_starts->push_back(seq_i - motif_len + 1);