ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/tophat_cpp/insertions.cpp
Revision: 29
Committed: Tue Aug 2 21:24:54 2011 UTC (8 years, 9 months ago) by gpertea
File size: 4148 byte(s)
Log Message:
adding tophat source work

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     assert(insertion.refid != 0xFFFFFFFF);
65     insertions[insertion] = 1;
66     }
67     }
68     return;
69     }
70    
71    
72     /**
73     * Print insertions in BED format.
74     * Note, as per the BED-standard (http://genome.ucsc.edu/FAQ/FAQformat)
75     * -The coordinates should be 0-based
76     * -The chromEnd field should not include the actual feature
77     * -The name will be the inserted sequence
78     * -The score will be the number of supporing counts, which is capped at 1,000
79     * By (my) convention, the chromStart will be the last genome postion
80     * before hte insertio.
81     *
82     * <chrom>\t<left>\t<left>\t<inserted sequence>\t<read count>\n
83     * @param insertions_out The output file
84     * @param insertions Maps from insertions to number of supporting reads
85     * @param ref_sequences The table of reference sequences
86     */
87     void print_insertions(FILE* insertions_out, const InsertionSet& insertions, RefSequenceTable& ref_sequences){
88     fprintf(insertions_out, "track name=insertions description=\"TopHat insertions\"\n");
89     for(InsertionSet::const_iterator i = insertions.begin(); i != insertions.end(); ++i){
90     int counts = i->second;
91     if(counts > 1000){
92     counts = 1000;
93     }
94     fprintf(insertions_out, "%s\t%d\t%d\t%s\t%d\n",
95     ref_sequences.get_name(i->first.refid),
96     i->first.left,
97     i->first.left,
98     (i->first.sequence).c_str(),
99     counts);
100     }
101     }
102    
103     /**
104     * Extract a list of insertions from a bowtie hit.
105     * Given a bowtie hit, extract a vector of insertions.
106     * @param bh The bowtie hit to use for alignment information.
107     * @param insertions Used to store the resultant vector of insertions.
108     */
109     void insertions_from_spliced_hit(const BowtieHit& bh, vector<Insertion>& insertions){
110     const vector<CigarOp>& cigar = bh.cigar();
111     unsigned int positionInGenome = bh.left();
112     unsigned int positionInRead = 0;
113    
114     for(size_t c = 0; c < cigar.size(); ++c){
115     Insertion insertion;
116     switch(cigar[c].opcode){
117     case REF_SKIP:
118     positionInGenome += cigar[c].length;
119     break;
120     case MATCH:
121     positionInGenome += cigar[c].length;
122     positionInRead += cigar[c].length;
123     break;
124     case DEL:
125     positionInGenome += cigar[c].length;
126     break;
127     case INS:
128     /*
129     * Note that the reported position in the genome from the SAM
130     * alignment is 1-based, since the insertion object is expecting
131     * a 0-based co-ordinate, we need to subtract 1
132     */
133     insertion.refid = bh.ref_id();
134     insertion.left = positionInGenome-1;
135     insertion.sequence = bh.seq().substr(positionInRead, cigar[c].length);
136    
137     insertions.push_back(insertion);
138     positionInRead += cigar[c].length;
139     break;
140     default:
141     break;
142     }
143     }
144     return;
145     }