ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/tophat_cpp/insertions.cpp
Revision: 135
Committed: Mon Dec 12 22:28:38 2011 UTC (8 years, 5 months ago) by gpertea
File size: 4146 byte(s)
Log Message:
wip - SplicedSAMHitFactory() still not implemented

Line User Rev File contents
1 gpertea 29 /*
2     * insertions.cpp
3     * TopHat
4     *
5     * Created by Ryan Kelley on 11/04/2010.
6     *
7     */
8    
9     #ifdef HAVE_CONFIG_H
10     #include <config.h>
11     #else
12     #define PACKAGE_VERSION "INTERNAL"
13     #define SVN_REVISION "XXX"
14     #endif
15    
16    
17     #include <cassert>
18     #include <cstdio>
19     #include <cstring>
20     #include <vector>
21     #include <string>
22     #include <map>
23     #include <algorithm>
24     #include <set>
25     #include <stdexcept>
26     #include <iostream>
27     #include <fstream>
28     #include <seqan/sequence.h>
29     #include <seqan/find.h>
30     #include <seqan/file.h>
31     #include <getopt.h>
32    
33     #include "common.h"
34     #include "bwt_map.h"
35     #include "junctions.h"
36     #include "insertions.h"
37     #include "fragments.h"
38     #include "wiggles.h"
39     #include "tokenize.h"
40     #include "reads.h"
41    
42     #include "inserts.h"
43    
44     /**
45     * Add insertions from an alignment to an InsertionSet.
46     * This will look for insertion in the alignment specified by bh. If the
47     * insertion is already in insertions, it will updated the count. Otherwise,
48     * it will add the insertion to the set and initialize the count to 1.
49     * @param bh The bowtie hit to be used to specify alignment infromation.
50     * @param insertions The InsertionSet that will be updated with the insertion information from teh alignment.
51     */
52     void insertions_from_alignment(const BowtieHit& bh, InsertionSet& insertions){
53    
54     vector<Insertion> new_insertions;
55     insertions_from_spliced_hit(bh, new_insertions);
56    
57     for(size_t i = 0; i < new_insertions.size(); ++i){
58     Insertion insertion = new_insertions[i];
59     InsertionSet::iterator itr = insertions.find(insertion);
60     if (itr != insertions.end()){
61     itr->second += 1;
62     }
63     else{
64 gpertea 135 assert(insertion.refid != VMAXINT32);
65 gpertea 29 insertions[insertion] = 1;
66     }
67     }
68     return;
69     }
70    
71     /**
72     * Print insertions in BED format.
73     * Note, as per the BED-standard (http://genome.ucsc.edu/FAQ/FAQformat)
74     * -The coordinates should be 0-based
75     * -The chromEnd field should not include the actual feature
76     * -The name will be the inserted sequence
77     * -The score will be the number of supporing counts, which is capped at 1,000
78     * By (my) convention, the chromStart will be the last genome postion
79     * before hte insertio.
80     *
81     * <chrom>\t<left>\t<left>\t<inserted sequence>\t<read count>\n
82     * @param insertions_out The output file
83     * @param insertions Maps from insertions to number of supporting reads
84     * @param ref_sequences The table of reference sequences
85     */
86     void print_insertions(FILE* insertions_out, const InsertionSet& insertions, RefSequenceTable& ref_sequences){
87     fprintf(insertions_out, "track name=insertions description=\"TopHat insertions\"\n");
88     for(InsertionSet::const_iterator i = insertions.begin(); i != insertions.end(); ++i){
89     int counts = i->second;
90     if(counts > 1000){
91     counts = 1000;
92     }
93     fprintf(insertions_out, "%s\t%d\t%d\t%s\t%d\n",
94     ref_sequences.get_name(i->first.refid),
95     i->first.left,
96     i->first.left,
97     (i->first.sequence).c_str(),
98     counts);
99     }
100     }
101    
102     /**
103     * Extract a list of insertions from a bowtie hit.
104     * Given a bowtie hit, extract a vector of insertions.
105     * @param bh The bowtie hit to use for alignment information.
106     * @param insertions Used to store the resultant vector of insertions.
107     */
108     void insertions_from_spliced_hit(const BowtieHit& bh, vector<Insertion>& insertions){
109     const vector<CigarOp>& cigar = bh.cigar();
110     unsigned int positionInGenome = bh.left();
111     unsigned int positionInRead = 0;
112    
113     for(size_t c = 0; c < cigar.size(); ++c){
114     Insertion insertion;
115     switch(cigar[c].opcode){
116     case REF_SKIP:
117     positionInGenome += cigar[c].length;
118     break;
119     case MATCH:
120     positionInGenome += cigar[c].length;
121     positionInRead += cigar[c].length;
122     break;
123     case DEL:
124     positionInGenome += cigar[c].length;
125     break;
126     case INS:
127     /*
128     * Note that the reported position in the genome from the SAM
129     * alignment is 1-based, since the insertion object is expecting
130     * a 0-based co-ordinate, we need to subtract 1
131     */
132     insertion.refid = bh.ref_id();
133     insertion.left = positionInGenome-1;
134     insertion.sequence = bh.seq().substr(positionInRead, cigar[c].length);
135    
136     insertions.push_back(insertion);
137     positionInRead += cigar[c].length;
138     break;
139     default:
140     break;
141     }
142     }
143     return;
144     }