-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserefgene.cpp
More file actions
executable file
·203 lines (187 loc) · 5.15 KB
/
parserefgene.cpp
File metadata and controls
executable file
·203 lines (187 loc) · 5.15 KB
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
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <string.h>
#include "ordpair.h"
using namespace std;
const int MAX_REF_LINE=10000;
const int MAX_X=500;//for mouse 312
struct exon
{
unsigned Start,End;
};
struct Offset_Record
{
char Genome[40];
unsigned Offset;
FILE* Out_File;
};
FILE* File_Open(const char* File_Name,const char* Mode);
int Split(char* String,char Sep, char* Fields[],int Max=0);
void Load_RefGene(char* File_Name,Hash & Ref_Gene);
int Filter_RefGene(char* Genome,char* File_Name);
int main( int argc,char* argv[])
{
if (argc <3)
{
printf("Processes RefGene for Rawbin..\n");
printf("%s <genome> <refgene>\n",argv[0]);
exit(0);
}
Filter_RefGene(argv[1],argv[2]);
//Load_RefGene(argv[1],Ref_Gene);
}
void Load_RefGene(char* File_Name,Hash & Ref_Gene)
{
char* String_Buffer=new char[MAX_REF_LINE+1];
char** Fields=new char*[20];
char** X_Start=new char*[MAX_X];
char** X_End=new char*[MAX_X];
FILE* Ref_Handle=File_Open(File_Name,"r");
char* & Chr=Fields[2];
char* & Exon_Count=Fields[8];
char* & Exon_Start=Fields[9];
char* & Exon_End=Fields[10];
char* & Strand=Fields[3];
int Max_Ex=0;
try
{
while(1)
{
if (fgets(String_Buffer,MAX_REF_LINE,Ref_Handle))
{
int Tab_Count=0;
Split(String_Buffer,'\t',Fields);
int Exon_Num=atoi(Exon_Count);
Split(Exon_Start,',',X_Start,MAX_X);
Split(Exon_End,',',X_End,MAX_X);
if (Exon_Num > MAX_X) {printf("Long splicing ... Some exons skipped..\n");Exon_Num=MAX_X;}
for (int i=0;i<Exon_Num;i++)
{
printf("%s\n",Strand);
//OP.Start=X_Start[i];OP.End=X_End[i];
}
}
else if(!feof(Ref_Handle)) throw("Read Error..\n"); else break;
}
}
catch(char *Error)
{
printf("%s",Error);
exit(0);
}
delete [] String_Buffer;delete [] Fields;delete [] X_End;delete []X_Start;
printf("%d\n",Max_Ex);
}
int Filter_RefGene(char* Genome,char* File_Name)
{
int Genome_Count=0;
Offset_Record Genome_Offsets[80];
map <string,FILE*> GList;
map <string,FILE*> ::iterator GList_IT;
char* String_Buffer=new char[MAX_REF_LINE+1];
char* String_Org=new char[MAX_REF_LINE+1];
char** Fields=new char*[20];
char* & Chr=Fields[2];
FILE* Ref_Handle=File_Open(File_Name,"r");
string LOCATIONFILE=Genome;
LOCATIONFILE+=".ann.location";
FILE* Location_File=File_Open(LOCATIONFILE.c_str(),"r");
while (fgets(Genome_Offsets[Genome_Count].Genome,39,Location_File)!=0 && Genome_Count<80)
{
Genome_Offsets[Genome_Count].Offset=atoi(Genome_Offsets[Genome_Count].Genome);
fgets(Genome_Offsets[Genome_Count].Genome,39,Location_File);
strcpy(Genome_Offsets[Genome_Count].Genome,Genome_Offsets[Genome_Count].Genome);
for(int i=0;i<40;i++)
{
if (Genome_Offsets[Genome_Count].Genome[i] == '\n' ||Genome_Offsets[Genome_Count].Genome[i] == '\r')
{
Genome_Offsets[Genome_Count].Genome[i]=0;
break;
}
}
Genome_Count++;
}
for ( int i=1;i<Genome_Count;i++)
{
string S=Genome_Offsets[i-1].Genome;
S=Genome_Offsets[i-1].Genome;S=Genome+S+".ref";
Genome_Offsets[i-1].Out_File=File_Open(S.c_str(),"w");
GList[Genome_Offsets[i-1].Genome]=Genome_Offsets[i-1].Out_File;
}
try
{
while(1)
{
if (fgets(String_Buffer,MAX_REF_LINE,Ref_Handle))
{
strcpy(String_Org,String_Buffer);
int Tab_Count=0;
Split(String_Buffer,'\t',Fields);
GList_IT=GList.find(Chr);
if(GList_IT != GList.end())
{
fprintf(GList_IT->second,"%s",String_Org);
}
}
else if(!feof(Ref_Handle)) throw("Read Error..\n"); else break;
}
}
catch(char *Error)
{
printf("%s",Error);
exit(0);
}
delete [] String_Buffer;delete [] Fields;
return Genome_Count-1;
}
/*
* === FUNCTION ======================================================================
* Name: Split
* Description: split string into a maximum of Max fields seperated by Sep, and strore starts in Fields..
* if Max=0, all fields are split..
* returns the number of fields..
* =====================================================================================
*/
int Split(char* String,char Sep, char* Fields[],int Max)
{
int i=0,j=0;
char* Last=String;
while(String[i] && String[i]!='\n')
{
if(String[i]==Sep)
{
Fields[j++]=Last;
String[i]=0;Last=String+i+1;
if (Max ==j) break;
}
i++;
}
return j;
}
char* Get_Field(char* String_Buffer,int & Tab_Count,int Skip)
{
return NULL;
}
/*
* === FUNCTION ======================================================================
* Name: File_Open
* Description: Open a file:
* Mode - "w" create/Write text from top - "wb" Write/Binary -"w+" open text read/write -"w+b" same as prev/Binary
* "r" Read text from top - "rb" Read/Binary -"r+" open text read/write/nocreate -"r+b" same as prev/binary
* - "a" text append/write -"a+" text open file read/append -"a+b" open binary read/append
*
* =====================================================================================
*/
FILE* File_Open(const char* File_Name,const char* Mode)
{
FILE* Handle;
Handle=fopen64(File_Name,Mode);
if (Handle==NULL)
{
printf("File %s Cannot be opened ....",File_Name);
exit(1);
}
else return Handle;
}