forked from josiahseaman/skittle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGtfReader.cpp
266 lines (229 loc) · 7.85 KB
/
GtfReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <QtGui>
#include "GtfReader.h"
#include "NucleotideDisplay.h"
#include "BasicTypes.h"
#include "UiVariables.h"
#include "ui_BookmarkDialog.h"
#include "SkittleUtil.h"
#include <QDebug>
#include <QThread>
#include <QString>
#include <qtconcurrentrun.h>
#include <QApplication>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <ctime>
using namespace QtConcurrent;
using namespace std;
/** **********************************
GtfReader is the file reader for Annotation files. Primarily the GTF
and GFF file format. This file format is tab delimited plain text file.
Each line int the file represent one annotation, one element.
The two main columns of interest are the start and stop position. This
is connected with a text string description that takes up the end of the line.
GtfReader also has the ability to output files that are generated by the user.
These files contain the bookmarks from views that the user has created. They are
outputted in the same GFF format so that they can be read in as any other annotation.
The bookmark functionality references the ui layout Ui_BookmarkDialog.h. GtfReader
provides the information for AnnotationDisplay to display. Both use the track_entry class.
**************************************/
GtfReader::GtfReader(UiVariables* gui)
{
ui = gui;
inputFilename = string("blank.fa");
outputFilename = string("user.gff");
bytesInFile = 0;
blockSize = 1000000;
chrName = string("");
}
bool GtfReader::initFile(string fileName)
{
file.clear();//reset the fail state to normal
file.open (fileName.c_str(), ifstream::in | ifstream::binary);
if(file.fail())
{
ErrorBox msg("Could not read the file.");
return false;
}
int begin = file.tellg();
file.seekg (0, ios::end);
int end = file.tellg();
bytesInFile = end - begin;
file.seekg(0, ios::beg);//move pointer to the beginning of the file
return true;
}
/**SLOTS**/
void GtfReader::addBookmark(int start, int end)
{
QDialog parent;
Ui_BookmarkDialog dialog;
dialog.setupUi(&parent);
std::stringstream startText;
startText << start;
dialog.start->setText( QString( startText.str().c_str() ) );
std::stringstream endText;
endText << end;
dialog.end->setText( QString(endText.str().c_str() ) );
dialog.sequence->setText( QString(chrName.c_str()) );
parent.show();
int result = parent.exec();
if(result == QDialog::Accepted)
{
ofstream outFile;
outFile.open(outputFilename.c_str(), ios::app);
if(!outFile.fail())
{
outFile << dialog.sequence->text().toStdString()<<"\t";
outFile << dialog.source->text().toStdString()<<"\t";
outFile << dialog.feature->text().toStdString()<<"\t";
outFile << dialog.start->text().toStdString() <<"\t";
outFile << dialog.end->text().toStdString() << "\t";
outFile << dialog.score->text().toStdString() << "\t";
outFile << (dialog.strand->isChecked() ? "+" : "-") << "\t";//QCheckBox
outFile << dialog.frame->currentText().toStdString() << "\t";//QComboBox
outFile << dialog.note->toPlainText().toStdString() << "\t";//QPlainTextEdit
outFile << "\n";
outFile.close();
track_entry entry = track_entry(dialog.start->text().toInt(), dialog.end->text().toInt(), color_entry(), dialog.note->toPlainText().toStdString());
emit BookmarkAdded(entry, outputFilename);
}
else
{
ErrorBox msg("Could not read the file.");
outFile.close();
}
}
}
void GtfReader::determineOutputFile(QString file)
{
string filename = file.toStdString();
outputFilename = filename;
outputFilename.append("-skittle_notes.gff");
chrName = trimPathFromFilename(filename);
}
vector<track_entry> GtfReader::readFile(QString filename)
{
vector<track_entry> annotation_track;
inputFilename = filename.toStdString();
if( inputFilename.empty() || !initFile(inputFilename) )
{
return vector<track_entry>();
}
annotation_track.clear();
bool skittleNotes = false;
string chromosomeRead;
if(filename.contains("skittle_notes"))
{
skittleNotes = true;
}
//Get name of chromosome from the beginning of the line and make sure that it matches the current viewed chromosome file
else
{
if(!chrName.empty()){
string chrDelim = "chr";
int chrStart = chrName.find(chrDelim);
int chrEnd = chrName.find(".fa");
chromosomeRead = chrName.substr((chrStart + chrDelim.length()), (chrEnd - (chrStart + chrDelim.length())));
}
else
{
chromosomeRead = "NONE";
}
//Ask user if our parsed chromosome name is correct
QStringList items;
items = getChromosomes();
bool ok;
int index = items.indexOf(QString::fromStdString(chromosomeRead));
if (index == -1)
{
index = 0;
QString temp = QInputDialog::getItem(0, tr("Current Chromosome Name"), tr("Please pick the name of the current chromosome from the Annotation File."), items, index, false, &ok);
if(ok && !temp.isEmpty())
{
chromosomeRead = temp.toStdString();
}
}
file.close();
initFile(filename.toStdString());
}
srand(time(0));
string line;
while( getline(file, line) )
{
line.erase(line.size()-1);//erase last character, should be a newline character
stringstream lineStr( line );
int start = 0;
int stop = 0;
//string repClass;
string chromosomeAnnotation;
lineStr >> chromosomeAnnotation;
if(chromosomeRead.compare(chromosomeAnnotation) == 0 || skittleNotes)
{
lineStr.ignore(10000, '\t');//1
lineStr.ignore(10000, '\t');//2
lineStr.ignore(10000, '\t');//3
/*
lineStr.ignore(10000, '\t');//4
lineStr.ignore(10000, '\t');//5
lineStr.ignore(10000, '\t');//6*/
//getline(lineStr, repClass, '\t');//repClass - type
lineStr >> start >> stop;//genoStart //genoEnd
color c = color_entry();//repClass);
if(!lineStr.fail())
{
annotation_track.push_back( track_entry(start, stop, c, line) );
int last_entry = annotation_track.size() -1;
annotation_track[last_entry].index = last_entry;
}
}
}
file.close();
return annotation_track;
}
QStringList GtfReader::getChromosomes()
{
QStringList list;
srand(time(0));
string line;
while(getline(file, line))
{
stringstream lineStr(line);
string chromosomeName;
lineStr >> chromosomeName;
QString cn = QString::fromStdString(chromosomeName);
if(!list.contains(cn))
list.append(cn);
}
list.removeDuplicates();
return list;
}
/*PRIVATE FUNCTIONS*/
/***********OUTPUT ANNOTATED SEQUENCE************** /
void GtfReader::snipAnnotatedSequence()
{
const string* seq = glWidget->nuc->sequence;
ofstream fout("clipped.fa");
for(int i = 0; i < annotation_track.size(); i++)
{
fout << seq->substr(annotation_track[i].start, annotation_track[i].stop - annotation_track[i].start);
}
fout.close();
}*/
color GtfReader::color_entry()
{
volatile int r = (int)(((float)rand() / RAND_MAX)* 255);
volatile int g = (int)(((float)rand() / RAND_MAX)* 255);
volatile int b = (int)(((float)rand() / RAND_MAX)* 255);
color c = color(r, g, b);
return c;
}
bool GtfReader::eq(string& str1, const char* str2)
{
return str1[0] == str2[0];//strcomp(str1.c_str(), str2) == 0;
}
string GtfReader::outputFile()
{
return outputFilename;
}