Debian Codegraph @

GFF-collection.h

raw
1 /* BaitFisher (version 1.2.7) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27 #ifndef GFF_COLLECTION_H
28 #define GFF_COLLECTION_H
29
30 #include "cstdio"
31 #include "easystring.h"
32 #include <vector>
33 #include <list>
34 #include <set>
35 #include <map>
36 #include "range_functions.h"
37 #include "GFF-class.h"
38
39
40 using namespace std;
41
42 static int ____verbosity = 1;
43
44
45 // Currently, GFF_record_list has no destructor.
46 // This can lead to memory leaks, since this class stores
47 // pointers to GFF_record, which will never be destructed explicitly.
48 // TODO!!!!
49
50 class GFF_record_list : public list<GFF_record*>
51 {
52 public:
53 void add(GFF_record *pf)
54 {
55 // cout << "Adding: "; pf->print(cout); cout << endl;
56 push_back(pf);
57 }
58
59 // Keep only entries with a feature value f.
60 void filter_by_feature(easystring &f)
61 {
62 list<GFF_record*>::iterator it, it_end;
63 it = begin();
64 it_end = end();
65
66 while (it != it_end)
67 {
68 if ((**it).getFeature() != f )
69 it = erase(it);
70 else
71 ++it;
72 }
73 }
74
75 // Find all attribute values for a given set of attribute names:
76 set<easystring> getSetOfAttributeValues(vector<easystring> att_name_vec, bool case_sensitive = true)
77 {
78 set<easystring> s;
79 /* list<GFF_record*>::iterator it, it_end; */
80
81 /* it = begin(); */
82 /* it_end = end(); */
83
84 /* while (it != it_end) */
85 /* { */
86 /* s.insert((**it).getGeneName()); */
87 /* ++it; */
88 /* } */
89 return s;
90 }
91
92 void getListOfGeneLocations(GFF_record_list &logl)
93 {
94 /* list<GeneLocation*> logl; */
95 /* GeneLocation *gl; */
96 /* set<string> s = getSetOfGeneNames(); */
97 /* set<string>::iterator s_it = s.begin(); */
98 /* set<string>::iterator s_it_end = s.end(); */
99 /* string geneName; */
100 /* CfeatureList::iterator l_it; */
101 /* CfeatureList::iterator l_it_end; */
102 /* unsigned range_start, range_end; */
103 /* int direction = 0; */
104 /* bool found; */
105
106 /* while(s_it != s_it_end) */
107 /* { */
108 /* geneName = *s_it; */
109 /* l_it = begin(); */
110 /* l_it_end = end(); */
111 /* found = false; */
112
113 /* // Find first occurence: */
114 /* while(l_it != l_it_end) */
115 /* { */
116 /* if ((**l_it).getGeneName() == geneName) */
117 /* { */
118 /* range_start = (**l_it).getStart(); */
119 /* range_end = (**l_it).getEnd(); */
120 /* direction = (**l_it).getDirection(); */
121 /* found = true; */
122 /* break; */
123 /* } */
124 /* ++l_it; */
125 /* } */
126
127 /* // Find other occurences: */
128 /* while(l_it != l_it_end) */
129 /* { */
130 /* if ((**l_it).getGeneName() == geneName) */
131 /* { */
132 /* range_union( range_start, range_end, */
133 /* (**l_it).getStart(), (**l_it).getEnd(), */
134 /* range_start, range_end); */
135 /* } */
136 /* ++l_it; */
137 /* } */
138
139 /* if (!found) */
140 /* { */
141 /* cerr << "Fatal error in element function getListOfGeneLocations. Gene with name " */
142 /* << geneName << " not found." << endl; */
143 /* exit(0); */
144 /* } */
145
146 /* gl = new GeneLocation(geneName, range_start, range_end, direction); */
147 /* logl.push_back(gl); */
148 /* ++s_it; */
149 /* } */
150 /* return logl; */
151 }
152
153
154
155 void getVectorOfFeatures_withGivenName(const easystring &the_geneName,
156 vector<GFF_record*> &vogffr )
157 {
158 /* CfeatureList::iterator it, it_end; */
159
160 /* vogf.clear(); */
161
162 /* it = begin(); */
163 /* it_end = end(); */
164
165 /* while (it != it_end) */
166 /* { */
167 /* if ( (**it).getGeneName() == the_geneName ) */
168 /* { */
169 /* vogf.push_back(*it); */
170 /* // list_of_features.push_back(*it); */
171 /* } */
172 /* ++it; */
173 /* } */
174 /* stable_sort(vogf.begin(), vogf.end(), lessThan_genomeFeature); */
175 }
176
177 /* CfeatureList::iterator find_equal_feature(CgenomeFeature &gf) */
178 /* { */
179 /* CfeatureList::iterator it = begin(); */
180 /* CfeatureList::iterator it_end = end(); */
181
182 /* it = lower_bound(it, it_end, &gf, lessThan_genomeFeature); */
183 /* it_end = upper_bound(it, it_end, &gf, lessThan_genomeFeature); */
184
185 /* while (it != it_end) */
186 /* { */
187 /* if ( **it == gf ) */
188 /* return it; */
189 /* ++it; */
190 /* } */
191 /* return end(); */
192 /* } */
193
194 void print(ostream &os, int flag = 0)
195 {
196 list<GFF_record*>::iterator it, it_end;
197
198 it = begin();
199 it_end = end();
200
201 while (it != it_end)
202 {
203 (**it).print(os, flag);
204 os << endl;
205 ++it;
206 }
207 }
208
209 unsigned print_sequences(FILE *of, easystring &format, CSequence_Mol &seq, int char_per_line,
210 bool ref_comp_if_neg_direction = true)
211 {
212 GFF_record_list::iterator it, it_end;
213 easystring seq_name = seq.getName();
214 unsigned count = 0;
215
216 it = begin();
217 it_end = end();
218
219 while (it != it_end)
220 {
221 // if (it->getSeqid == seq_name) // This is also checked in the print_sequence member function of the GFF_record.
222 {
223 if ( (**it).print_sequence(of, format, seq, char_per_line, ref_comp_if_neg_direction) )
224 ++count;
225 }
226 ++it;
227 }
228 return count;
229 }
230
231 }; // End class GFF_record_list : public list<GFF_record*>
232
233
234
235
236
237
238
239 class GFF_collection
240 {
241 map<easystring, int> sequences; // counts the GFF-records associated to sequences
242 map<easystring, int> features; // counts the GFF-records associated to features
243 unsigned total_number;
244 unsigned max_seq_name_length;
245
246 // Type reminder: class GFF_record_list : public list<GFF_record*>
247
248 GFF_record_list gff_record_list; // not inherited since then we can lose control
249 // over features added to the list.
250
251 public:
252 GFF_collection(int v=0):total_number(0), max_seq_name_length(0)
253 {}
254
255 void reset()
256 {
257 sequences.clear();
258 features.clear();
259 gff_record_list.clear();
260
261 total_number = 0;
262 max_seq_name_length = 0;
263 }
264
265
266 void add(GFF_record* a)
267 {
268 map<easystring, int>::iterator it;
269
270 easystring sid = a->getSeqid();
271 easystring fea = a->getFeature();
272
273 ++total_number;
274 if ( max_seq_name_length < sid.length() )
275 max_seq_name_length = sid.length();
276
277 // Search for sequnece name in map
278 it = sequences.find(sid);
279 if (it != sequences.end() )
280 {
281 ++(it->second); // Count the feature for this list
282 }
283 else // Add seq to map and set counter to 1
284 {
285 sequences[sid] = 1;
286 }
287
288 // Search for feature name in map
289 it = features.find(fea);
290 if (it != features.end() )
291 {
292 ++(it->second); // Count the feature for this list
293 }
294 else // Add seq to map and set counter to 1
295 {
296 features[fea] = 1;
297 }
298
299 gff_record_list.add(a);
300 }
301
302 unsigned size()
303 {
304 return gff_record_list.size();
305 }
306
307
308 void add(GFF_collection &col)
309 {
310 GFF_record_list::iterator it, it_end;
311
312 it = col.gff_record_list.begin();
313 it_end = col.gff_record_list.end();
314
315 while (it != it_end)
316 {
317 add(*it);
318 ++it;
319 }
320
321 }
322
323 static void set_verbosity(int v)
324 {
325 ____verbosity = v;
326 }
327
328
329 const GFF_record_list& getGFFrecordList()
330 {
331 return gff_record_list;
332 }
333
334 unsigned getNumGFFrecords()
335 {
336 return total_number;
337 }
338
339 unsigned getMaxSeqidLength()
340 {
341 return max_seq_name_length;
342 }
343
344 void print(ostream &os, int flag = 0)
345 {
346 gff_record_list.print(os, flag);
347 }
348
349 void sort()
350 {
351 gff_record_list.sort(position_lessThan_GFF_record_pointer);
352 }
353
354 void get_List_of_gff_records_of_a_sequence(const easystring &seq,
355 GFF_record_list &list_of_records)
356 {
357 GFF_record_list::iterator it, it_end;
358
359 it = gff_record_list.begin();
360 it_end = gff_record_list.end();
361
362 while (it != it_end)
363 {
364 if ( (**it).getSeqid() == seq )
365 {
366 list_of_records.push_back(*it);
367 }
368 ++it;
369 }
370 }
371
372
373 // Worries: Perfomrance.
374 // Note: attribute_key must be lower case, since we only do a lower case comparison.
375 void get_List_of_gff_records_for_attribute(const easystring &attribute_key,
376 const easystring &attribute_value,
377 GFF_record_list &list_of_records)
378 {
379 GFF_record_list::iterator it, it_end;
380
381 it = gff_record_list.begin();
382 it_end = gff_record_list.end();
383
384 list_of_records.clear();
385
386 while (it != it_end)
387 {
388 easystring val = (**it).getValueOfAttribute(attribute_key, true);
389
390 if ( val == attribute_value )
391 {
392 list_of_records.push_back(*it);
393 }
394 ++it;
395 }
396 }
397
398
399 vector<easystring> getVectorOfSeqid()
400 {
401 vector<easystring> v;
402 map<easystring, int>::iterator it, it_end;
403
404 it = sequences.begin();
405 it_end = sequences.end();
406 while (it != it_end)
407 {
408 v.push_back( it->first );
409 ++it;
410 }
411 return v;
412 }
413
414 void get_min_max_number_of_features_for_Seqid(int &mi, int &ma)
415 {
416 mi = ma = 0; // Only used when collection is empty
417 map<easystring, int>::iterator it, it_end;
418
419 it = sequences.begin();
420 it_end = sequences.end();
421
422 if (it != it_end)
423 mi = ma = it->second;
424 while (it != it_end)
425 {
426 if (it->second > ma)
427 ma = it->second;
428 if (it->second < mi)
429 mi = it->second;
430 ++it;
431 }
432 }
433
434
435 void addSeqidsToSet(set<easystring> &s)
436 {
437 map<easystring, int>::iterator it, it_end;
438
439 it = sequences.begin();
440 it_end = sequences.end();
441
442 while (it != it_end)
443 {
444 s.insert( it->first );
445 ++it;
446 }
447 }
448
449
450 void readGFFfile(easystring filename,
451 unsigned &good_lines,
452 unsigned &bad_lines,
453 bool &file_error )
454 {
455 ifstream is(filename.c_str() );
456 easystring line;
457 GFF_record *gffr;
458 bool match_OK;
459
460 file_error = false;
461 good_lines = 0;
462 bad_lines = 0;
463
464 if ( is.fail() )
465 {
466 file_error = true;
467 return;
468 }
469
470 getline(is, line);
471 while (is)
472 {
473 line.removeSpacesBack();
474 if (line[0] == '#' || line.empty() )
475 {
476 getline(is, line);
477 continue;
478 }
479
480 gffr = new GFF_record(line);
481 match_OK = gffr->good();
482
483 if (match_OK)
484 {
485 add(gffr);
486 ++good_lines;
487 }
488 else
489 {
490 ++bad_lines;
491 std::cerr << endl << "Problematic line: " << line << endl;
492 std::cerr << "Reason for error: " << gffr->getErrorReason() << endl;
493 delete gffr;
494 }
495 getline(is, line);
496 }
497 is.close();
498
499 // gff_record_list.sort(lessThan_genomeFeature);
500 }
501
502 void print_sequences_all_features(FILE *of, easystring &format, CFile *seq_file, int char_per_line,
503 bool ref_comp_if_neg_direction,
504 unsigned &count_seq, unsigned &count_printed_features)
505 {
506 // Variables to read sequence file:
507 CSequence_Mol seq(CSequence_Mol::dna);
508
509 count_seq=0;
510 count_printed_features=0;
511
512 //std::ios::sync_with_stdio(false);
513
514 // For each sequence: analyse the features
515 while (!seq_file->eof())
516 {
517 seq.readRawFastaSequence(*seq_file);
518 ++count_seq;
519
520 // cerr << count_seq << " " << seq.getName() << endl;
521
522 count_printed_features += gff_record_list.print_sequences(of, format, seq, char_per_line, ref_comp_if_neg_direction);
523 }
524 }
525
526
527 void getVectorOfFeatures(vector<easystring> &the_features)
528 {
529 map<easystring, int>::iterator it, it_end;
530 it = features.begin();
531 it_end = features.end();
532
533 the_features.clear();
534
535 while (it != it_end)
536 {
537 the_features.push_back(it->first);
538 ++it;
539 }
540 }
541
542
543 void getRangeList(CRangeList &rl, const easystring &seq)
544 {
545 GFF_record_list::iterator it, it_end;
546 it = gff_record_list.begin();
547 it_end = gff_record_list.end();
548
549 rl.clear();
550
551 while (it != it_end)
552 {
553 if ( (**it).getSeqid() == seq )
554 {
555 // Remember: Ranges should be 0 based and the end should be the point after the last element in the range.
556 // Coordinates in the gff file are 1 based.
557 rl.add( (**it).getStart(),
558 (**it).getEnd()+1); // the range class expects
559 }
560 ++it;
561 }
562 }
563
564
565 void getConsolidatedRangeListOfGFFCollection_for_Sequence(CRangeList &crl, easystring &seq)
566 {
567 CRangeList rl;
568 getRangeList(rl, seq);
569 rl.get_consolidated_range_list(crl);
570 }
571
572
573 unsigned get_coverage_loop(std::ostream &os_error)
574 {
575 map<easystring, int>::iterator it, it_end;
576
577 it = sequences.begin();
578 it_end = sequences.end();
579
580 unsigned all_len=0;
581 unsigned n;
582
583 while (it != it_end)
584 {
585 // Do something with it->first:
586
587 os_error << "Analysing sequence coverage:" << it->first << endl;
588 CRangeList rl1;
589
590 getRangeList(rl1, it->first);
591 os_error << "Elements: " << rl1.size() << endl;
592 rl1.sort();
593 n = rl1.coverage_len_loop();
594 os_error << "Contribution: " << n << endl;
595 all_len += n;
596 ++it;
597 }
598 return all_len;
599 }
600
601
602 unsigned get_coverage(std::ostream &os_error)
603 {
604 map<easystring, int>::iterator it, it_end;
605
606 it = sequences.begin();
607 it_end = sequences.end();
608
609 unsigned all_len=0;
610 unsigned n;
611
612 while (it != it_end)
613 {
614 // Do something with it->first:
615
616 // os_error << "Analysing sequence coverage:" << it->first << endl;
617 CRangeList rl1;
618 getRangeList(rl1, it->first);
619 // os_error << "Elements: " << rl1.size() << endl;
620 n = rl1.coverage_len();
621 // os_error << "Contribution: " << n << endl;
622 all_len += n;
623 ++it;
624 }
625 return all_len;
626 }
627
628
629
630 friend void get_coverage_info_loop(GFF_collection &gff1, GFF_collection &gff2,
631 unsigned &len_gff1, unsigned &len_gff2,
632 unsigned &len_union, unsigned &len_intersection,
633 unsigned &only_gff1, unsigned &only_gff2)
634 {
635 set<easystring> s;
636
637 gff1.addSeqidsToSet(s);
638 gff2.addSeqidsToSet(s);
639
640 Crange limit(0, -1u);
641 bool err1, err2;
642 CRangeList rl1;
643 CRangeList rl2;
644
645 unsigned len_gff1_this_seq;
646 unsigned len_gff2_this_seq;
647 unsigned len_union_this_seq;
648 unsigned len_intersection_this_seq;
649 unsigned limit_len_dummy;
650
651 len_gff1 = 0;
652 len_gff2 = 0;
653 len_union = 0;
654 len_intersection = 0;
655 only_gff1 = 0;
656 only_gff2 = 0;
657
658 set<easystring>::iterator it, it_end;
659 map<easystring, int>::iterator it_find;
660
661 it = s.begin();
662 it_end = s.end();
663
664 while (it != it_end)
665 {
666 err1 = false;
667 err2 = false;
668
669 it_find = gff1.sequences.find(*it);
670 if (it_find == gff1.sequences.end() )
671 {
672 if (____verbosity > 1)
673 {
674 cerr << "Note: Sequence " << *it
675 << " is present in gff file 2 but not in file 1." << endl;
676 }
677 err1 = true;
678 }
679 it_find = gff2.sequences.find(*it);
680 if (it_find == gff2.sequences.end() )
681 {
682 if (____verbosity > 1)
683 {
684 cerr << "Note: Sequence " << *it
685 << " is present in gff file 1 but not in file 2." << endl;
686 }
687 err2 = true;
688 }
689
690 if (!(err1 || err2))
691 {
692 if (____verbosity > 0)
693 {
694 cerr << "Analysing sequence " << *it << endl;
695 }
696
697 gff1.getRangeList(rl1, *it);
698 gff2.getRangeList(rl2, *it);
699
700 Crange b1 = rl1.bounds();
701 Crange b2 = rl2.bounds();
702
703 Crange limit = range_span(b1, b2);
704
705 rl1.sort();
706 rl2.sort();
707 coverage_values_loop(limit, rl1, rl2, limit_len_dummy,
708 len_gff1_this_seq,
709 len_gff2_this_seq,
710 len_union_this_seq,
711 len_intersection_this_seq);
712
713 // cerr << len_gff1_this_seq << endl;
714 // cerr << len_gff2_this_seq << endl;
715 // cerr << len_union_this_seq << endl;
716 // cerr << len_intersection_this_seq << endl;
717
718 len_gff1 += len_gff1_this_seq;
719 len_gff2 += len_gff2_this_seq;
720 len_union += len_union_this_seq;
721 len_intersection += len_intersection_this_seq;
722 }
723 else // we still want to update the individual lengths as well as the union length:
724 {
725 if (!err1)
726 {
727 gff1.getRangeList(rl1, *it);
728 rl1.sort();
729 unsigned n = rl1.coverage_len_loop();
730 len_gff1 += n;
731 len_union += n;
732 ++only_gff1;
733 }
734 if (!err2)
735 {
736 gff2.getRangeList(rl2, *it);
737 rl2.sort();
738 unsigned n = rl2.coverage_len_loop();
739 len_gff2 += n;
740 len_union += n;
741 ++only_gff2;
742 }
743 }
744 ++it;
745 }
746 }
747
748
749 friend void get_coverage_info(GFF_collection &gff1, GFF_collection &gff2,
750 unsigned &len_gff1, unsigned &len_gff2,
751 unsigned &len_union, unsigned &len_intersection,
752 unsigned &only_gff1, unsigned &only_gff2)
753 {
754 set<easystring> s;
755
756 gff1.addSeqidsToSet(s);
757 gff2.addSeqidsToSet(s);
758
759 Crange limit(0, -1u);
760 bool err1, err2;
761 CRangeList rl1;
762 CRangeList rl2;
763
764 unsigned len_gff1_this_seq;
765 unsigned len_gff2_this_seq;
766 unsigned len_union_this_seq;
767 unsigned len_intersection_this_seq;
768 unsigned limit_len_dummy;
769
770 len_gff1 = 0;
771 len_gff2 = 0;
772 len_union = 0;
773 len_intersection = 0;
774 only_gff1 = 0;
775 only_gff2 = 0;
776
777 set<easystring>::iterator it, it_end;
778 map<easystring, int>::iterator it_find;
779
780 it = s.begin();
781 it_end = s.end();
782
783 while (it != it_end)
784 {
785 err1 = false;
786 err2 = false;
787 it_find = gff1.sequences.find(*it);
788 if (it_find == gff1.sequences.end() )
789 {
790 if (____verbosity > 1)
791 {
792 cerr << "Note: Sequence " << *it
793 << " is present in gff file 1 but not in file 2." << endl;
794 }
795 err1 = true;
796 }
797 it_find = gff2.sequences.find(*it);
798 if (it_find == gff2.sequences.end() )
799 {
800 if (____verbosity > 1)
801 {
802 cerr << "Note: Sequence " << *it
803 << " is present in gff file 2 but not in file 1." << endl;
804 }
805 err2 = true;
806 }
807
808 if (!( err1 || err2 ))
809 {
810 if (____verbosity > 0)
811 {
812 cerr << "Analysing sequence " << *it << endl;
813 }
814
815 gff1.getRangeList(rl1, *it);
816 gff2.getRangeList(rl2, *it);
817
818 Crange b1 = rl1.bounds();
819 Crange b2 = rl2.bounds();
820
821 Crange limit = range_span(b1, b2);
822
823 rl1.sort();
824 rl2.sort();
825 coverage_values_loop(limit, rl1, rl2, limit_len_dummy,
826 len_gff1_this_seq,
827 len_gff2_this_seq,
828 len_union_this_seq,
829 len_intersection_this_seq);
830
831 // cerr << len_gff1_this_seq << endl;
832 // cerr << len_gff2_this_seq << endl;
833 // cerr << len_union_this_seq << endl;
834 // cerr << len_intersection_this_seq << endl;
835
836 len_gff1 += len_gff1_this_seq;
837 len_gff2 += len_gff2_this_seq;
838 len_union += len_union_this_seq;
839 len_intersection += len_intersection_this_seq;
840 }
841 else // we still want to update the individual lengths as well as the union length:
842 {
843 if (!err1)
844 {
845 gff1.getRangeList(rl1, *it);
846 unsigned n = rl1.coverage_len();
847 len_gff1 += n;
848 len_union += n;
849 ++only_gff1;
850 }
851 if (!err2)
852 {
853 gff2.getRangeList(rl2, *it);
854 unsigned n = rl2.coverage_len();
855 len_gff2 += n;
856 len_union += n;
857 ++only_gff2;
858 }
859 }
860 ++it;
861 } // while
862 } // END friend void get_coverage_info
863
864
865 // Main purpose: Find in1in2not3not4 = (1^2)\(3 U 4)
866 friend void get_coverage_info_in1in2not3no4_loop
867 (GFF_collection &gff1, GFF_collection &gff2,
868 GFF_collection &gff3, GFF_collection &gff4,
869 unsigned &len_gff1, unsigned &len_gff2,
870 unsigned &len_gff3, unsigned &len_gff4,
871 unsigned &in1not3not4, unsigned &in2not3not4,
872 unsigned &in1in2not3not4, unsigned &in3in4not1not2
873 )
874 {
875 len_gff1 = 0;
876 len_gff2 = 0;
877 len_gff3 = 0;
878 len_gff4 = 0;
879 in1not3not4 = 0;
880 in2not3not4 = 0;
881 in1in2not3not4 = 0;
882 in3in4not1not2 = 0;
883
884 set<easystring> s;
885
886 gff1.addSeqidsToSet(s);
887 gff2.addSeqidsToSet(s);
888 gff3.addSeqidsToSet(s);
889 gff4.addSeqidsToSet(s);
890
891 // bool noseq1, noseq2, noseq3, noseq4;
892 CRangeList rl1;
893 CRangeList rl2;
894 CRangeList rl3;
895 CRangeList rl4;
896
897 unsigned len_gff1_this_seq;
898 unsigned len_gff2_this_seq;
899 unsigned len_gff3_this_seq;
900 unsigned len_gff4_this_seq;
901 unsigned in1not3not4_this_seq;
902 unsigned in2not3not4_this_seq;
903 unsigned in1in2not3not4_this_seq;
904 unsigned in3in4not1not2_this_seq;
905
906 set<easystring>::iterator it, it_end;
907 map<easystring, int>::iterator it_find;
908
909 it = s.begin();
910 it_end = s.end();
911
912 while (it != it_end)
913 {
914 if (____verbosity > 0)
915 {
916 cerr << "Analysing sequence: " << *it << endl;
917 }
918 /* noseq1 = false; */
919 /* noseq2 = false; */
920 /* noseq3 = false; */
921 /* noseq4 = false; */
922
923 it_find = gff1.sequences.find(*it);
924 if (it_find == gff1.sequences.end() )
925 {
926 if (____verbosity > 1)
927 {
928 cerr << "Note: Sequence " << *it
929 << " is not present in gff file 1." << endl;
930 }
931 // noseq1 = true;
932 rl1 = CRangeList();
933 }
934 else
935 {
936 gff1.getRangeList(rl1, *it);
937 }
938
939 it_find = gff2.sequences.find(*it);
940 if (it_find == gff2.sequences.end() )
941 {
942 if (____verbosity > 1)
943 {
944 cerr << "Note: Sequence " << *it
945 << " is not present in gff file 2." << endl;
946 }
947 // noseq2 = true;
948 rl2 = CRangeList();
949 }
950 else
951 {
952 gff2.getRangeList(rl2, *it);
953 }
954
955 it_find = gff3.sequences.find(*it);
956 if (it_find == gff3.sequences.end() )
957 {
958 if (____verbosity > 1)
959 {
960 cerr << "Note: Sequence " << *it
961 << " is not present in gff file 3." << endl;
962 }
963 // noseq3 = true;
964 rl3 = CRangeList();
965 }
966 else
967 {
968 gff3.getRangeList(rl3, *it);
969 }
970
971 it_find = gff4.sequences.find(*it);
972 if (it_find == gff4.sequences.end() )
973 {
974 if (____verbosity > 1)
975 {
976 cerr << "Note: Sequence " << *it
977 << " is not present in gff file 4." << endl;
978 }
979 // noseq4 = true;
980 rl4 = CRangeList();
981 }
982 else
983 {
984 gff4.getRangeList(rl4, *it);
985 }
986
987
988
989 Crange b1 = rl1.bounds();
990 Crange b2 = rl2.bounds();
991 Crange b3 = rl2.bounds();
992 Crange b4 = rl2.bounds();
993
994 Crange limit = range_span(b1, b2);
995 limit = range_span(limit, b3);
996 limit = range_span(limit, b4);
997
998 rl1.sort();
999 rl2.sort();
1000 rl3.sort();
1001 rl4.sort();
1002
1003 coverage_values_loop(limit,
1004 rl1, rl2, rl3, rl4,
1005 len_gff1_this_seq,
1006 len_gff2_this_seq,
1007 len_gff3_this_seq,
1008 len_gff4_this_seq,
1009 in1not3not4_this_seq,
1010 in2not3not4_this_seq,
1011 in1in2not3not4_this_seq,
1012 in3in4not1not2_this_seq
1013 );
1014
1015 /* cerr << "len1: " << len_gff1_this_seq << endl; */
1016 /* cerr << "len2: " << len_gff2_this_seq << endl; */
1017 /* cerr << "len3: " << len_gff3_this_seq << endl; */
1018 /* cerr << "len4: " << len_gff4_this_seq << endl; */
1019
1020 /* cerr << "in1n34: " << in1not3not4_this_seq << endl; */
1021 /* cerr << "in2n34: " << in2not3not4_this_seq << endl; */
1022 /* cerr << "in12n34: " << in1in2not3not4_this_seq << endl; */
1023 /* cerr << "in34n12: " << in3in4not1not2_this_seq << endl; */
1024
1025 len_gff1 += len_gff1_this_seq;
1026 len_gff2 += len_gff2_this_seq;
1027 len_gff3 += len_gff3_this_seq;
1028 len_gff4 += len_gff4_this_seq;
1029
1030 /* cerr << "int-res: len1: " << len_gff1 << endl; */
1031 /* cerr << "int-res: len2: " << len_gff2 << endl; */
1032 /* cerr << "int-res: len3: " << len_gff3 << endl; */
1033 /* cerr << "int-res: len4: " << len_gff4 << endl; */
1034
1035
1036 in1not3not4 += in1not3not4_this_seq;
1037 in2not3not4 += in2not3not4_this_seq;
1038 in1in2not3not4 += in1in2not3not4_this_seq;
1039 in3in4not1not2 += in3in4not1not2_this_seq;
1040
1041 ++it;
1042 }
1043 } // END friend void get_coverage_info_in1in2not3no4_loop
1044
1045
1046 void set_to_complement_in_universe(GFF_collection &universe, GFF_collection &gff,
1047 easystring creator, easystring feature)
1048 {
1049 int mi, ma;
1050 universe.get_min_max_number_of_features_for_Seqid(mi, ma);
1051
1052 if (mi != 1 || ma != 1)
1053 {
1054 std::cerr << "Error in set_to_complement_in_inverse: In the universe collection each sequence id can only occurr once."
1055 << std::endl;
1056 return;
1057 }
1058
1059 vector<easystring> vec_seqIds = universe.getVectorOfSeqid();
1060
1061 unsigned i, N=vec_seqIds.size();
1062 CRangeList rl1, rl2, rl2res;
1063 Crange limit;
1064 GFF_record *p;
1065 easystring name;
1066
1067 for (i=0; i<N; ++i)
1068 {
1069 std::cerr << "Compute complement in " << vec_seqIds[i] << endl;
1070 universe.getRangeList(rl1, vec_seqIds[i]);
1071 gff.getRangeList(rl2, vec_seqIds[i]);
1072 limit = *(rl1.begin());
1073
1074 rl2res.set_to_complement_of(rl2, limit);
1075
1076 // std::cerr << "SIZE: " << rl2res.size() << endl;
1077
1078 CRangeList::CRangeList_iterator_const rl_it, rl_it_end;
1079
1080 rl_it = rl2res.begin();
1081 rl_it_end = rl2res.end();
1082
1083 easystring n100 = ".";
1084
1085 while (rl_it != rl_it_end)
1086 {
1087 // Remember the conventions here:
1088 // The ranges in rl2res are non inclusive for the end of the range.
1089 // But GFF_records store ranges such that the end coordinate is
1090 // the last coordinate in the range. So we have to subract 1.
1091 name = easystring("name=") + vec_seqIds[i] + "_" + easystring(rl_it->begin()) + "_" + easystring(rl_it->end()-1);
1092 p = new GFF_record(vec_seqIds[i], creator, feature,
1093 rl_it->begin(), rl_it->end()-1, n100, '.', '.', name);
1094 add(p);
1095
1096 // p->print(cout);
1097 // std::cout << endl;
1098
1099 // delete p;
1100
1101 ++rl_it;
1102 }
1103
1104 }
1105 }
1106
1107
1108 void set_to_union_of(GFF_collection &gff1, GFF_collection &gff2,
1109 easystring creator, easystring feature)
1110 {
1111 // Determine set of all Seqids:
1112 set<easystring> s;
1113
1114 gff1.addSeqidsToSet(s);
1115 gff2.addSeqidsToSet(s);
1116
1117 set<easystring>::iterator it, it_end;
1118 map<easystring, int>::iterator it_find;
1119
1120 it = s.begin();
1121 it_end = s.end();
1122
1123 // bool present1, present2;
1124
1125 CRangeList rl1;
1126 CRangeList rl2;
1127 CRangeList rlres;
1128
1129 easystring name;
1130
1131 // For all Seqids
1132 while (it != it_end)
1133 {
1134 /* present1 = false; */
1135 /* present2 = false; */
1136 rl1.clear();
1137 rl2.clear();
1138 rlres.clear();
1139
1140 it_find = gff1.sequences.find(*it);
1141 if (it_find == gff1.sequences.end() )
1142 {
1143 if (____verbosity > 1)
1144 {
1145 cerr << "Note: Sequence " << *it
1146 << " is present in gff file 2 but not in file 1." << endl;
1147 }
1148 }
1149 else
1150 {
1151 // present1 = true;
1152 gff1.getRangeList(rl1, *it);
1153 }
1154
1155 it_find = gff2.sequences.find(*it);
1156 if (it_find == gff2.sequences.end() )
1157 {
1158 if (____verbosity > 1)
1159 {
1160 cerr << "Note: Sequence " << *it
1161 << " is present in gff file 1 but not in file 2." << endl;
1162 }
1163 }
1164 else
1165 {
1166 // present2 = true;
1167 gff2.getRangeList(rl2, *it);
1168 }
1169
1170 rlres.set_to_range_list_union(rl1, rl2);
1171
1172 easystring n100 = ".";
1173 easystring seq_name = *it;
1174 GFF_record *p;
1175
1176 CRangeList::CRangeList_iterator_const rl_it, rl_it_end;
1177 rl_it = rlres.begin();
1178 rl_it_end = rlres.end();
1179
1180 while (rl_it != rl_it_end)
1181 {
1182 // Remember the conventions here:
1183 // The ranges in rlres are non inclusive for the end of the range.
1184 // But GFF_records store ranges such that the end coordinate is
1185 // the last coordinate in the range. So we have to subract 1.
1186 name = easystring("name=") + seq_name + "_" + easystring(rl_it->begin()) + "_" + easystring(rl_it->end()-1);
1187 p = new GFF_record(seq_name, creator, feature,
1188 rl_it->begin(), rl_it->end()-1, n100, '.', '.', name);
1189 add(p);
1190 // p->print(cout);
1191 // std::cout << endl;
1192 // delete p;
1193 ++rl_it;
1194 } // while (rl_it != rl_it_end)
1195 ++it;
1196 } // while (it != it_end)
1197
1198 }
1199
1200 void set_to_intersection_of(GFF_collection &gff1, GFF_collection &gff2,
1201 easystring creator, easystring feature)
1202 {
1203 // Determine set of all Seqids:
1204 set<easystring> s;
1205
1206 gff1.addSeqidsToSet(s);
1207 gff2.addSeqidsToSet(s);
1208
1209 set<easystring>::iterator it, it_end;
1210 map<easystring, int>::iterator it_find;
1211
1212 it = s.begin();
1213 it_end = s.end();
1214
1215 // bool present1, present2;
1216
1217 CRangeList rl1;
1218 CRangeList rl2;
1219 CRangeList rlres;
1220
1221 easystring name;
1222
1223 // For all Seqids
1224 while (it != it_end)
1225 {
1226 // present1 = false;
1227 // present2 = false;
1228 rl1.clear();
1229 rl2.clear();
1230 rlres.clear();
1231
1232 it_find = gff1.sequences.find(*it);
1233 if (it_find == gff1.sequences.end() )
1234 {
1235 if (____verbosity > 1)
1236 {
1237 cerr << "Note: Sequence " << *it
1238 << " is present in gff file 2 but not in file 1." << endl;
1239 }
1240 }
1241 else
1242 {
1243 // present1 = true;
1244 gff1.getRangeList(rl1, *it);
1245 }
1246
1247 it_find = gff2.sequences.find(*it);
1248 if (it_find == gff2.sequences.end() )
1249 {
1250 if (____verbosity > 1)
1251 {
1252 cerr << "Note: Sequence " << *it
1253 << " is present in gff file 1 but not in file 2." << endl;
1254 }
1255 }
1256 else
1257 {
1258 // present2 = true;
1259 gff2.getRangeList(rl2, *it);
1260 }
1261
1262 rlres.set_to_range_list_intersection(rl1, rl2);
1263
1264 // std::cerr << "rl1" << endl;
1265 // rl1. print_bracketed_list(std::cerr);
1266 // std::cerr << "rl2" << endl;
1267 // rl2. print_bracketed_list(std::cerr);
1268 // std::cerr << "rlres" << endl;
1269 // rlres. print_bracketed_list(std::cerr);
1270
1271 easystring n100 = ".";
1272 easystring seq_name = *it;
1273 GFF_record *p;
1274
1275 CRangeList::CRangeList_iterator_const rl_it, rl_it_end;
1276 rl_it = rlres.begin();
1277 rl_it_end = rlres.end();
1278
1279 while (rl_it != rl_it_end)
1280 {
1281 // Remember the conventions here:
1282 // The ranges in rlres are non inclusive for the end of the range.
1283 // But GFF_records store ranges such that the end coordinate is
1284 // the last coordinate in the range. So we have to subract 1.
1285 name = easystring("name=") + seq_name + "_" + easystring(rl_it->begin()) + "_" + easystring(rl_it->end()-1);
1286 p = new GFF_record(seq_name, creator, feature,
1287 rl_it->begin(), rl_it->end()-1, n100, '.', '.', name);
1288 add(p);
1289 // p->print(cout);
1290 // std::cout << endl;
1291 // delete p;
1292 ++rl_it;
1293 } // while (rl_it != rl_it_end)
1294 ++it;
1295 } // while (it != it_end)
1296
1297 }
1298
1299
1300
1301
1302
1303 }; // End class GFF_collection
1304
1305
1306
1307 #endif