File: gnFileSource.cpp

package info (click to toggle)
libgenome 1.3.1-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,156 kB
  • ctags: 1,212
  • sloc: cpp: 10,910; sh: 8,264; makefile: 79
file content (132 lines) | stat: -rw-r--r-- 2,816 bytes parent folder | download | duplicates (8)
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
/////////////////////////////////////////////////////////////////////////////
// File:            gnFileSource.h
// Purpose:         Implements generic gnBaseSource methods
// Description:     Provides a general implementation for accessing DNA
//                  sequence contig data files.
// Changes:        
// Version:         libGenome 0.5.1 
// Author:          Aaron Darling 
// Modified by:     
// Copyright:       (c) Aaron Darling 
// Licenses:        See COPYING file for details
/////////////////////////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "libGenome/gnFileSource.h"
#include <fstream>
#include "libGenome/gnFilter.h"


using namespace std;
namespace genome {

gnFileSource::gnFileSource() :
m_pFilter(gnFilter::fullDNASeqFilter())
{}

//copy constructor
gnFileSource::gnFileSource(const gnFileSource& gnfs){
	m_openString = gnfs.m_openString;
	m_pFilter = gnfs.m_pFilter;
	m_newlineType = gnfs.m_newlineType;
	m_newlineSize = gnfs.m_newlineSize;
#pragma omp critical
{
	m_ifstream.open( m_openString.c_str(), ios::in | ios::binary );
	if( !m_ifstream.is_open() )
		m_ifstream.clear();
}
}

// Open, Close	
void gnFileSource::Open( string openString )
{
	boolean opened = true;
#pragma omp critical
{
	m_ifstream.open(openString.c_str(), ios::in | ios::binary );
	if( m_ifstream.is_open() )
	{
		m_openString = openString;
		if( ParseStream(m_ifstream) )
		{
			;
		}
		else{
			m_ifstream.clear();
			m_ifstream.close();
		}
	}else{
		m_ifstream.clear();
		opened = false;
	}
}
	if(!opened)
		Throw_gnEx(FileNotOpened());
}
void gnFileSource::Open( )
{
	bool opened = true;
#pragma omp critical
{
	m_ifstream.open( m_openString.c_str(), ios::in | ios::binary );
	if( !m_ifstream.is_open() ){
		opened = false;
		m_ifstream.clear();
	}
}
	if(!opened)
		Throw_gnEx(FileNotOpened());
}
void gnFileSource::Close()
{
#pragma omp critical
	m_ifstream.close();
}

boolean gnFileSource::Read( const uint64 pos, char* buf, gnSeqI& bufLen) 
{
	bool failure = false;
#pragma omp critical
{
	m_ifstream.seekg(pos, ios::beg);
	m_ifstream.read(buf, bufLen);
	failure = m_ifstream.fail();
}
	if(failure){
#pragma omp critical
		m_ifstream.clear();
		return false;
	}
	return true;
}

void gnFileSource::DetermineNewlineType()
{
	// set default values
	m_newlineType = gnNewlineUnix;
	m_newlineSize = 1;

	//decide what type of newlines we have
	char buf[ BUFFER_SIZE ];
	m_ifstream.getline( buf, BUFFER_SIZE);
	m_ifstream.seekg(-2, ios::cur);
	m_ifstream.read( buf, 2);
	m_ifstream.seekg(0);
	if(buf[1] == '\n'){
		if(buf[0] == '\r'){
			m_newlineType = gnNewlineWindows;
			m_newlineSize = 2;
		}else{ 
			if(buf[1] == '\r')
				m_newlineType = gnNewlineMac;
			else
				m_newlineType = gnNewlineUnix;
		}
	}
}

}	// end namespace genome