ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/LayoutParser.h
Revision: 268
Committed: Thu Oct 4 14:04:46 2012 UTC (7 years, 1 month ago) by gpertea
File size: 6062 byte(s)
Log Message:
Line User Rev File contents
1 gpertea 16 #ifndef LayoutParser_H
2     #define LayoutParser_H
3    
4     #include "GBase.h"
5     #include "GList.hh"
6     #include "GHash.hh"
7     #include <stdio.h>
8     //hash data associated with a contig/sequence name
9     //a contig name key is always stored as its name plus .<length>
10    
11     class LytCtgData;
12    
13     struct LytSeqInterSeg {
14     int segEnd, segRClip;
15     int nextStart, nextLClip;
16     char segRSplice, nextLSplice;
17     int nextSegSeq;
18     LytSeqInterSeg(int end, int nextstart, int rclip=0, int nextlclip=0,
19     char segrsplice=0, char nextlsplice=0, int seqpos=0) {
20     segEnd=end; segRClip=rclip;
21     nextStart=nextstart; nextLClip=nextlclip;
22     segRSplice=segrsplice;
23     nextLSplice=nextlsplice;
24     nextSegSeq=seqpos;
25     }
26     int length() {
27     return (nextStart-segEnd-1);
28     }
29     };
30    
31     class LytSeqInfo { //info for a sequence within the file
32     int xlen; //total sequence length (with all the added * within contig)
33     int interseglen;
34     public:
35     char *name;
36     LytCtgData* contig; //contig data containing this sequence, as above
37     bool segmented;
38     int numisegs; // number of intersegs[]
39     LytSeqInterSeg* intersegs;
40     off_t fpos; //file position for the sequence data
41     unsigned char reversed;
42     int offs; //offset in contig (of the very left end)
43     int left,right; //clear range (relative to sequence itself, max 1..xlen)
44     LytSeqInfo(char* seqid, LytCtgData* ctg, int pos=0, unsigned char minus=0,
45     int slen=0, int clpL=0, int clpR=0) {
46     contig=ctg;
47     offs=pos;
48     reversed=minus;
49     fpos=0;
50     interseglen=0;
51     xlen=slen;
52     left=clpL+1; //1 if no clpL given
53     right=xlen-clpR; //0 if no len given
54     segmented=false;
55     numisegs=0;
56     name=Gstrdup(seqid);
57     intersegs=NULL;
58     }
59     ~LytSeqInfo() {
60     GFREE(name);
61     GFREE(intersegs);
62     }
63     bool hasIntrons() { return (numisegs>0); }
64     void addInterSeg(int end, int nextstart, int rclip=0, int nextlclip=0,
65     char splice=0, char nextsplice=0, int seqofs=0) {
66     GREALLOC(intersegs,(numisegs+1)*sizeof(LytSeqInterSeg));
67     interseglen+=nextstart-end-1;
68     intersegs[numisegs].segEnd=end; intersegs[numisegs].segRClip=rclip;
69     intersegs[numisegs].nextStart=nextstart; intersegs[numisegs].nextLClip=nextlclip;
70     intersegs[numisegs].segRSplice=splice;
71     intersegs[numisegs].nextLSplice=nextsplice;
72     intersegs[numisegs].nextSegSeq=seqofs;
73     numisegs++;
74     }
75    
76     void setLength(int len) {
77     //should only be called BEFORE setting the real clipping coordinates
78     //(left,right)
79     xlen=len;
80     left=1;
81     right=xlen;
82     }
83     int length() { return xlen; } //xtended span, including introns
84     int seglen() { return xlen-interseglen; } //segments only, no introns
85     bool operator==(const LytSeqInfo& s) {
86     return (offs+left-1==s.offs+s.left-1);
87     }
88     bool operator<(const LytSeqInfo& s) {
89     return (offs+left-1<s.offs+s.left-1);
90     }
91     char* expandGaps(char* s);
92     };
93    
94    
95     class LytCtgData {
96     public:
97     char* name; //contig name, as stored in file
98     unsigned int len; //contig length (lsequence, from ACE file)
99 gpertea 264 int lpos;
100     int rpos;
101 gpertea 16 int numseqs;
102     int offs; //some other type of user data that might be of use
103     off_t fpos; //position in file for this contig's entry
104     GList<LytSeqInfo> seqs;
105 gpertea 268 LytCtgData(off_t pos=0):name(NULL), len(0),
106     lpos(0), rpos(0), numseqs(0), offs(0), fpos(pos),
107     seqs(false,false,false) {
108 gpertea 264 }
109 gpertea 16 ~LytCtgData() {
110     GFREE(name);
111     seqs.Clear();
112 gpertea 264 }
113 gpertea 16
114     char* readName(char* s, GHash<int>& names);
115    
116     bool operator==(const LytCtgData& s) {
117     return (strcmp(name,s.name)==0);
118     }
119     bool operator<(const LytCtgData& s) {
120     return (strcmp(name,s.name)<0);
121     }
122     };
123     //callback -- called after a read or contig sequence is loaded
124     typedef bool fnLytSeq(int ctgno, LytCtgData* d, LytSeqInfo* s, char* seq);
125    
126     class LayoutParser {
127     protected:
128     FILE* f; //file stream
129     off_t f_pos;
130     char* fname;
131     LytCtgData* currentContig; // currently loaded contig -- for browsing/loading
132     int numContigs; //total number of contigs found in this file
133     //int numSeqs; //total number of (distinct) sequences found in this file
134     GHash<LytSeqInfo> seqinfo; //sequence locations in the file
135     GHash<int> ctgIDs; //contig IDs, to make them unique!
136    
137     GList<LytCtgData> contigs; //list of contig names with their size,
138     //number of sequences and filepos
139     protected:
140     GLineReader* linebuf; //the line buffer
141     off_t fskipTo(const char* linestart, const char* butnot=NULL);
142     bool startsWith(const char* s, const char* start, int tlen);
143     virtual LytSeqInfo* addSeq(char* s, LytCtgData* ctg);
144     int seek(off_t offset) {
145     int r=fseeko(f, offset, SEEK_SET);
146     if (r==0) f_pos=offset;
147     return r;
148     }
149    
150     public:
151     LayoutParser(const char* filename):contigs(false,true) {
152     f=NULL;
153     f_pos=0;
154     numContigs=0;
155     currentContig=NULL;
156     if (filename==NULL) {
157     f=stdin;
158     fname=Gstrdup("stdin");
159     }
160     else
161     fname=Gstrdup(filename);
162     linebuf=new GLineReader();
163     }
164     virtual ~LayoutParser() {
165     ctgIDs.Clear();
166     GFREE(fname);
167     delete linebuf;
168     close();
169     numContigs=0;
170     seqinfo.Clear();
171     contigs.Clear();
172     }
173     virtual bool open();
174     void close();
175     virtual bool parse(fnLytSeq* seqfn=NULL); //load all the file offsets
176     virtual bool parseContigs(); //load all the file offsets for contigs
177     virtual bool loadContig(int ctgidx, fnLytSeq* seqfn=NULL, bool re_pos=true); //for loading by browsing
178     //if parsefn is not NULL, it is executed, passing the sequence data(first time, with the contig sequence)
179     //if parserfn returns true, the data is freed after it is processed
180     virtual char getFileType() { return 'L'; }
181     //sequence loading - only by request
182     LytCtgData* getContig(int idx) { return contigs[idx]; }
183     virtual char* getSeq(LytSeqInfo* sqinfo) { return NULL; }
184     virtual char* getContigSeq(LytCtgData* ctgdata) { return NULL; }
185     int getNumContigs() { return numContigs; }
186     //int getNumSeqs() { return numSeqs; }
187     off_t getFilePos() { return f_pos; }
188     //sorting the list of contigs:
189     void contigsByName();
190     void contigsByLen();
191     void contigsByNumSeqs();
192     };
193    
194     #endif