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 const std::string& Sequence::get_species() const
310 bool Sequence::empty() const
312 return (size() == 0);
315 const std::list<annot>& Sequence::annotations() const
320 string::size_type Sequence::length() const
325 string::size_type Sequence::size() const
327 return sequence.size();
330 Sequence::iterator Sequence::begin()
332 return sequence.begin();
335 Sequence::const_iterator Sequence::begin() const
337 return sequence.begin();
340 Sequence::iterator Sequence::end()
342 return sequence.end();
345 Sequence::const_iterator Sequence::end() const
347 return sequence.end();
352 Sequence::get_seq() const
359 Sequence::subseq(int start, int end) const
361 return sequence.substr(start, end);
366 Sequence::c_seq() const
368 return sequence.c_str();
372 Sequence::rev_comp() const
375 char conversionTable[257];
376 int seq_i, table_i, len;
378 len = sequence.length();
379 rev_comp.reserve(len);
380 // make a conversion table
381 // init all parts of conversion table to '~' character
382 // '~' I doubt will ever appear in a sequence file (jeez, I hope)
383 // and may the fleas of 1000 camels infest the genitals of any biologist (and
384 // seven generations of their progeny) who decides to make it mean
385 // something special!!!
386 // PS - double the curse for any smartass non-biologist who tries it as well
387 for(table_i=0; table_i < 256; table_i++)
389 conversionTable[table_i] = '~';
391 // add end of string character for printing out table for testing purposes
392 conversionTable[256] = '\0';
394 // add in the characters for the bases we want to convert
395 conversionTable[(int)'A'] = 'T';
396 conversionTable[(int)'T'] = 'A';
397 conversionTable[(int)'G'] = 'C';
398 conversionTable[(int)'C'] = 'G';
399 conversionTable[(int)'N'] = 'N';
401 // finally, the actual conversion loop
402 for(seq_i = len - 1; seq_i >= 0; seq_i--)
404 table_i = (int) sequence[seq_i];
405 rev_comp += conversionTable[table_i];
413 Sequence::get_header() const
418 //FIXME: i don't think this code is callable
420 Sequence::sp_name() const
427 Sequence::set_seq(const string& a_seq)
429 set_filtered_sequence(a_seq);
451 Sequence::save(fstream &save_file)
452 //string save_file_path)
455 list<annot>::iterator annots_i;
457 // not sure why, or if i'm doing something wrong, but can't seem to pass
458 // file pointers down to this method from the mussa control class
459 // so each call to save a sequence appends to the file started by mussa_class
460 //save_file.open(save_file_path.c_str(), ios::app);
462 save_file << "<Sequence>" << endl;
463 save_file << sequence << endl;
464 save_file << "</Sequence>" << endl;
466 save_file << "<Annotations>" << endl;
467 save_file << species << endl;
468 for (annots_i = annots.begin(); annots_i != annots.end(); ++annots_i)
470 save_file << annots_i->start << " " << annots_i->end << " " ;
471 save_file << annots_i->name << " " << annots_i->type << endl;
473 save_file << "</Annotations>" << endl;
478 Sequence::load_museq(string load_file_path, int seq_num)
481 string file_data_line;
484 string::size_type space_split_i;
488 load_file.open(load_file_path.c_str(), ios::in);
491 // search for the seq_num-th sequence
492 while ( (!load_file.eof()) && (seq_counter < seq_num) )
494 getline(load_file,file_data_line);
495 if (file_data_line == "<Sequence>")
498 getline(load_file, file_data_line);
499 sequence = file_data_line;
500 getline(load_file, file_data_line);
501 getline(load_file, file_data_line);
502 if (file_data_line == "<Annotations>")
504 getline(load_file, file_data_line);
505 species = file_data_line;
506 while ( (!load_file.eof()) && (file_data_line != "</Annotations>") )
508 getline(load_file,file_data_line);
509 if ((file_data_line != "") && (file_data_line != "</Annotations>"))
511 // need to get 4 values...almost same code 4 times...
512 // get annot start index
513 space_split_i = file_data_line.find(" ");
514 annot_value = file_data_line.substr(0,space_split_i);
515 an_annot.start = atoi (annot_value.c_str());
516 file_data_line = file_data_line.substr(space_split_i+1);
517 // get annot end index
518 space_split_i = file_data_line.find(" ");
519 annot_value = file_data_line.substr(0,space_split_i);
520 an_annot.end = atoi (annot_value.c_str());
522 if (space_split_i == string::npos) // no entry for type or name
524 cout << "seq, annots - no type or name\n";
528 else // else get annot type
530 file_data_line = file_data_line.substr(space_split_i+1);
531 space_split_i = file_data_line.find(" ");
532 annot_value = file_data_line.substr(0,space_split_i);
533 an_annot.type = annot_value;
534 if (space_split_i == string::npos) // no entry for name
536 cout << "seq, annots - no name\n";
539 else // get annot name
541 file_data_line = file_data_line.substr(space_split_i+1);
542 space_split_i = file_data_line.find(" ");
543 annot_value = file_data_line.substr(0,space_split_i);
544 an_annot.type = annot_value;
547 annots.push_back(an_annot); // don't forget to actually add the annot
549 //cout << "seq, annots: " << an_annot.start << ", " << an_annot.end
550 // << "-->" << an_annot.type << "::" << an_annot.name << endl;
558 Sequence::rc_motif(string a_motif)
561 char conversionTable[257];
562 int seq_i, table_i, len;
564 len = a_motif.length();
565 rev_comp.reserve(len);
567 for(table_i=0; table_i < 256; table_i++)
569 conversionTable[table_i] = '~';
571 // add end of string character for printing out table for testing purposes
572 conversionTable[256] = '\0';
574 // add in the characters for the bases we want to convert (IUPAC)
575 conversionTable[(int)'A'] = 'T';
576 conversionTable[(int)'T'] = 'A';
577 conversionTable[(int)'G'] = 'C';
578 conversionTable[(int)'C'] = 'G';
579 conversionTable[(int)'N'] = 'N';
580 conversionTable[(int)'M'] = 'K';
581 conversionTable[(int)'R'] = 'Y';
582 conversionTable[(int)'W'] = 'W';
583 conversionTable[(int)'S'] = 'S';
584 conversionTable[(int)'Y'] = 'R';
585 conversionTable[(int)'K'] = 'M';
586 conversionTable[(int)'V'] = 'B';
587 conversionTable[(int)'H'] = 'D';
588 conversionTable[(int)'D'] = 'H';
589 conversionTable[(int)'B'] = 'V';
591 // finally, the actual conversion loop
592 for(seq_i = len - 1; seq_i >= 0; seq_i--)
594 //cout << "** i = " << seq_i << " bp = " <<
595 table_i = (int) a_motif[seq_i];
596 rev_comp += conversionTable[table_i];
599 //cout << "seq: " << a_motif << endl;
600 //cout << "rc: " << rev_comp << endl;
606 Sequence::motif_normalize(string a_motif)
611 len = a_motif.length();
612 valid_motif.reserve(len);
614 // this just upcases IUPAC symbols. Eventually should return an error if non IUPAC is present.
615 // current nonIUPAC symbols are omitted, which is not reported atm
616 for(seq_i = 0; seq_i < len; seq_i++)
618 if ((a_motif[seq_i] == 'a') || (a_motif[seq_i] == 'A'))
620 else if ((a_motif[seq_i] == 't') || (a_motif[seq_i] == 'T'))
622 else if ((a_motif[seq_i] == 'g') || (a_motif[seq_i] == 'G'))
624 else if ((a_motif[seq_i] == 'c') || (a_motif[seq_i] == 'C'))
626 else if ((a_motif[seq_i] == 'n') || (a_motif[seq_i] == 'N'))
628 else if ((a_motif[seq_i] == 'm') || (a_motif[seq_i] == 'M'))
630 else if ((a_motif[seq_i] == 'r') || (a_motif[seq_i] == 'R'))
632 else if ((a_motif[seq_i] == 'w') || (a_motif[seq_i] == 'W'))
634 else if ((a_motif[seq_i] == 's') || (a_motif[seq_i] == 'S'))
636 else if ((a_motif[seq_i] == 'y') || (a_motif[seq_i] == 'Y'))
638 else if ((a_motif[seq_i] == 'k') || (a_motif[seq_i] == 'K'))
640 else if ((a_motif[seq_i] == 'v') || (a_motif[seq_i] == 'V'))
642 else if ((a_motif[seq_i] == 'h') || (a_motif[seq_i] == 'H'))
644 else if ((a_motif[seq_i] == 'd') || (a_motif[seq_i] == 'D'))
646 else if ((a_motif[seq_i] == 'b') || (a_motif[seq_i] == 'B'))
649 string msg = "Letter ";
650 msg += a_motif[seq_i];
651 msg += " is not a valid IUPAC symbol";
652 throw motif_normalize_error(msg);
655 //cout << "valid_motif is: " << valid_motif << endl;
659 void Sequence::add_motif(string a_motif)
661 vector<int> motif_starts = find_motif(a_motif);
663 for(vector<int>::iterator motif_start_i = motif_starts.begin();
664 motif_start_i != motif_starts.end();
667 motif_list.push_back(motif(*motif_start_i, a_motif));
671 void Sequence::clear_motifs()
676 const list<motif>& Sequence::motifs() const
682 Sequence::find_motif(string a_motif)
684 vector<int> motif_match_starts;
687 motif_match_starts.clear();
689 //cout << "motif is: " << a_motif << endl;
690 a_motif = motif_normalize(a_motif);
691 //cout << "motif is: " << a_motif << endl;
695 //cout << "Sequence: none blank motif\n";
696 motif_scan(a_motif, &motif_match_starts);
698 a_motif_rc = rc_motif(a_motif);
699 // make sure not to do search again if it is a palindrome
700 if (a_motif_rc != a_motif)
701 motif_scan(a_motif_rc, &motif_match_starts);
703 return motif_match_starts;
707 Sequence::motif_scan(string a_motif, vector<int> * motif_match_starts)
710 string::size_type seq_i;
711 int motif_i, motif_len;
713 // faster to loop thru the sequence as a old c string (ie char array)
714 seq_c = (char*)sequence.c_str();
715 //cout << "Sequence: motif, seq len = " << sequence.length() << endl;
716 motif_len = a_motif.length();
718 //cout << "motif_length: " << motif_len << endl;
719 //cout << "RAAARRRRR\n";
723 //cout << "motif: " << a_motif << endl;
725 //cout << "Sequence: motif, length= " << length << endl;
727 while (seq_i < sequence.length())
729 //cout << seq_c[seq_i];
730 //cout << seq_c[seq_i] << "?" << a_motif[motif_i] << ":" << motif_i << " ";
731 // this is pretty much a straight translation of Nora's python code
732 // to match iupac letter codes
733 if (a_motif[motif_i] =='N')
735 else if (a_motif[motif_i] == seq_c[seq_i])
737 else if ((a_motif[motif_i] =='M') &&
738 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C')))
740 else if ((a_motif[motif_i] =='R') &&
741 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G')))
743 else if ((a_motif[motif_i] =='W') &&
744 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='T')))
746 else if ((a_motif[motif_i] =='S') &&
747 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G')))
749 else if ((a_motif[motif_i] =='Y') &&
750 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='T')))
752 else if ((a_motif[motif_i] =='K') &&
753 ((seq_c[seq_i]=='G') || (seq_c[seq_i]=='T')))
755 else if ((a_motif[motif_i] =='V') &&
756 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
757 (seq_c[seq_i]=='G')))
759 else if ((a_motif[seq_i] =='H') &&
760 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='C') ||
761 (seq_c[seq_i]=='T')))
763 else if ((a_motif[motif_i] =='D') &&
764 ((seq_c[seq_i]=='A') || (seq_c[seq_i]=='G') ||
765 (seq_c[seq_i]=='T')))
767 else if ((a_motif[motif_i] =='B') &&
768 ((seq_c[seq_i]=='C') || (seq_c[seq_i]=='G') ||
769 (seq_c[seq_i]=='T')))
778 // end Nora stuff, now we see if a match is found this pass
779 if (motif_i == motif_len)
783 motif_match_starts->push_back(seq_i - motif_len + 1);