My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package fr.inserm.umr915.core.bio.fasta;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;



import org.lindenb.io.IOUtils;
import org.lindenb.util.StringUtils;
import fr.inserm.umr915.core.bio.SequenceProvider;


/**
* IndexedGenome
*
*/
public class IndexedGenome
implements SequenceProvider
{
private Map<String, FastaIndex> name2index=new HashMap<String, FastaIndex>();
private Map<File, RandomAccessFile> file2stream=new HashMap<File, RandomAccessFile>();

public IndexedGenome()
{

}
public void addAll(Collection<FastaIndex> indexes)
{
for(FastaIndex i:indexes) add(i);
}

public void add(FastaIndex index)
{
for(String name:index.getNames())
{
if(name2index.containsKey(name))
{
throw new IllegalArgumentException("Sequence "+index+" indexed twice.");
}
}
for(String name:index.getNames())
{
this.name2index.put(name, index);
}
}

@Override
public void close()
{
for(File f:file2stream.keySet())
{
RandomAccessFile io= file2stream.get(f);
if(io!=null)
{
try { io.close(); } catch(IOException err) {}
}
}
file2stream.clear();
}

@Override
public byte[] getSequence(String chromosome,int start,int length) throws IOException
{
FastaIndex index=name2index.get(chromosome);
if(index==null) throw new IOException("Sequence \""+chromosome+"\" was not indexed");
RandomAccessFile io=file2stream.get(index.getFile());
if(io==null)
{
io=new RandomAccessFile(index.getFile(), "r");
file2stream.put(index.getFile(), io);
}
return index.getSequence(io, start, length);
}


public static IndexedGenome readIndex(File file) throws IOException
{
IndexedGenome genome=new IndexedGenome();
genome.addAll(FastaIndexer.readIndex(file));
return genome;
}

private static void _echo(String name,int start,int length,byte seq[],int lineLength)
{
System.out.print(">");
System.out.print(name);
System.out.print('|');
System.out.print(""+start+"-"+(start+length));

for(int i=0;i< seq.length;++i)
{
if(i%lineLength==0) System.out.println();
System.out.print((char)seq[i]);
}
System.out.println();
}
private static void _run(IndexedGenome genome,BufferedReader in,int lineLength) throws IOException
{
Pattern TAB=Pattern.compile("[\t]");
String line;
while((line=in.readLine())!=null)
{
if(StringUtils.isBlank(line) || line.startsWith("#")) continue;
String tokens[]=TAB.split(line);
FastaIndex index=genome.name2index.get(tokens[0]);
if(index==null)
{
System.err.println("Not indexed:\""+tokens[0]+"\"");
continue;
}
if(tokens.length<3)
{
_echo(tokens[0], 0, index.getLength(),genome.getSequence(tokens[0], 0, index.getLength()),lineLength);
}
else
{
int start=Integer.parseInt(tokens[1]);
int end=Integer.parseInt(tokens[2]);
if(end<start)
{
System.err.println("Bad start/end in "+line);
continue;
}

_echo(tokens[0],start,end-start,genome.getSequence(tokens[0], start,end-start),lineLength);
}
}
}

public static void main(String[] args)
{
try {
int fastaLineLength=50;
File indexFile=null;
int optind=0;
while(optind<args.length)
{
if(args[optind].equals("-h"))
{
System.err.println("-f <xml-index-file> REQUIRED");
System.err.println("-n <int> fasta line length:"+fastaLineLength);
System.err.println("(stdin|file) with ");
System.err.println(" seqname\n OR");
System.err.println(" seqname(tab)start(end)end");
return;
}
else if(args[optind].equals("-f"))
{
indexFile=new File(args[++optind]);
}
else if(args[optind].equals("-n"))
{
fastaLineLength=Integer.parseInt(args[++optind]);
if(fastaLineLength<=0)
{
System.err.println("Bad fasta line size "+fastaLineLength);
return;
}
}
else if(args[optind].equals("--"))
{
optind++;
break;
}
else if(args[optind].startsWith("-"))
{
System.err.println("Unnown option: "+args[optind]);
return;
}
else
{
break;
}
++optind;
}

if(indexFile==null)
{
System.err.println("XML Index file missing");
return;
}
IndexedGenome index= IndexedGenome.readIndex(indexFile);
if(optind==args.length)
{
_run(index,new BufferedReader(new InputStreamReader(System.in)),fastaLineLength);
}
else
{
while(optind< args.length)
{
String inputName=args[optind++];
BufferedReader in=IOUtils.openReader(inputName);
_run(index,new BufferedReader(new InputStreamReader(System.in)),fastaLineLength);
in.close();
}
}
index.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Change log

r74 by plindenbaum.u915 on May 18, 2010   Diff
cont
Go to: 
Project members, sign in to write a code review

Older revisions

r71 by plindenbaum.u915 on May 17, 2010   Diff
continue
r70 by plindenbaum.u915 on May 17, 2010   Diff
sequence indexing
r68 by plindenbaum.u915 on May 14, 2010   Diff
cont
All revisions of this file

File info

Size: 4975 bytes, 209 lines
Powered by Google Project Hosting