public
Created

  • Download Gist
gistfile1.d
D
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/**
* The module csv is a set of function to parse many format using a delimitter as csv file
* Supported format:
* - .mat matrix file
* - .bed UCSC file
* For parse a .csv file use std.csv
*/
module bed;
 
import std.conv;
import std.stdio;
import std.csv;
import std.traits;
import std.file;
import std.array;
import std.algorithm;
import std.range;
import std.string;
import std.exception;
 
/**
* loadMatrixFile
* load a matrix from a file.
* Params:
* filePath = path to file who contain matrix
* separator = set delimiter used into the file for separate each column default it is tab
* Returns:
* A 2D array
*/
T[][] matrixReader( T )( string filePath, string separator = "\t" ){
File matrixFile = File( filePath, "r");
T[][] matrix;
size_t length = 10;
size_t counter = 0;
matrix.length = 10;
foreach( line; matrixFile.byLine() ){
if( length == counter ){
length += 10;
matrix.length = length;
}
matrix[counter] = array( map!(to!T)( filter!"!a.empty"(line.split( separator ) ) ) );// Use filter like split bug and do not merge consecutive delimiter
counter++;
}
matrix.length = counter;
return matrix;
}
 
struct BedData3{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
}
 
struct BedData4{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
}
 
struct BedData5{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
}
 
struct BedData6{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
}
 
struct BedData7{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
size_t thickStart; // 6
}
 
struct BedData8{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
size_t thickStart; // 6
size_t thickEnd; // 7
}
 
struct BedData9{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
size_t thickStart; // 6
size_t thickEnd; // 7
size_t[3] itemRgb; // 8
}
 
struct BedData10{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
size_t thickStart; // 6
size_t thickEnd; // 7
size_t[3] itemRgb; // 8
size_t blockCount; // 9
}
 
struct BedData11{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
size_t thickStart; // 6
size_t thickEnd; // 7
size_t[3] itemRgb; // 8
size_t blockCount; // 9
size_t blockSizes; // 10
}
 
struct BedData12{
string chrom; // 0
size_t chromStart; // 1
size_t chromEnd; // 2
string name; // 3
size_t score; // 4
char strand; // 5
size_t thickStart; // 6
size_t thickEnd; // 7
size_t[3] itemRgb; // 8
size_t blockCount; // 9
size_t blockSizes; // 10
size_t blockStarts; // 11
}
 
 
struct BedMetadata{
string name;
string description;
size_t visibility;
string itemRgb;
size_t browserStart;
size_t browserEnd;
string chromosome;
string hide;
 
string toString(){
string result = "";
if( chromosome != "" && browserStart != 0 && browserEnd != 0 )
result ~= "browser position %s:%d-%d\n".format( chromosome, browserStart, browserEnd );
if( hide != "" )
result ~= "browser hide %s\n".format( hide );
if( name != "" && description != "" && visibility != 0 )
result ~= "track name=%s description=%s visibility=%d\n".format( name, description, visibility );
if( itemRgb != "" )
result ~= "itemRgb=\"%s\"".format( itemRgb );
return result;
}
}
 
 
struct TrackLine{
string name;
string description;
string type;
size_t visibility;
size_t[3] color;
string itemRgb;
size_t[3] colorByStrand;
size_t useScore;
string group;
string db;
size_t offset;
size_t maxItems;
string url;
string htmlUrl;
string bigDataUrl;
}
 
 
struct Bed( T ){
BedMetadata metadata;
TrackLine trackLine;
T[] bedDataList;
}
 
TrackLine trackLineReader( in char[] trackLine ){
TrackLine result;
 
size_t nameStart = trackLine.countUntil("name=\"");
if( nameStart != -1 ){
size_t nameEnd = nameStart + trackLine[nameStart .. $ ].countUntil('"');
result.name = trackLine[nameStart .. nameEnd].idup;
}
 
size_t descriptionStart = trackLine.countUntil("description=\"");
if( descriptionStart != -1 ){
size_t descriptionEnd = descriptionStart + trackLine[descriptionStart .. $ ].countUntil('"');
result.description = trackLine[descriptionStart .. descriptionEnd].idup;
}
 
size_t typeStart = trackLine.countUntil("type=\"");
if( typeStart != -1 ){
size_t typeEnd = typeStart + trackLine[typeStart .. $ ].countUntil('"');
result.type = trackLine[typeStart .. typeEnd].idup;
}
 
size_t visibilityStart = trackLine.countUntil("visibility=");
if( typeStart != -1 ){
size_t visibilityEnd = visibilityStart + trackLine[visibilityStart .. $].countUntil(' ');
result.visibility = to!size_t(trackLine[typeStart .. visibilityEnd]);
}
 
size_t colorStart = trackLine.countUntil("color=\"");
if( typeStart != -1 ){
size_t colorEnd = colorStart + trackLine[colorStart .. $ ].countUntil('"');
result.color = array( map!(to!size_t)(trackLine[typeStart .. colorEnd].idup.split(",")) );
}
 
size_t itemRgbStart = trackLine.countUntil("itemRgb=\"");
if( typeStart != -1 ){
size_t itemRgbEnd = itemRgbStart + trackLine[itemRgbStart .. $ ].countUntil('"');
result.itemRgb = trackLine[typeStart .. itemRgbEnd].idup;
}
 
size_t useScoreStart = trackLine.countUntil("useScore=");
if( typeStart != -1 ){
size_t useScoreEnd = nameStart + trackLine[nameStart .. $ ].countUntil(' ');
result.useScore = to!size_t(trackLine[typeStart .. useScoreEnd]);
}
 
size_t groupStart = trackLine.countUntil("group=\"");
if( typeStart != -1 ){
size_t groupEnd = groupStart + trackLine[groupStart .. $ ].countUntil('"');
result.group = trackLine[typeStart .. groupEnd].idup;
}
 
size_t dbStart = trackLine.countUntil("db=\"");
if( typeStart != -1 ){
size_t dbEnd = dbStart + trackLine[dbStart .. $ ].countUntil('"');
result.db = trackLine[typeStart .. dbEnd].idup;
}
 
size_t offsetStart = trackLine.countUntil("offset=");
if( typeStart != -1 ){
size_t offsetEnd = offsetStart + trackLine[offsetStart .. $ ].countUntil(' ');
result.offset = to!size_t(trackLine[typeStart .. offsetEnd]);
}
 
size_t maxItemsStart = trackLine.countUntil("maxItems=");
if( typeStart != -1 ){
size_t maxItemsEnd = maxItemsStart + trackLine[maxItemsStart .. $ ].countUntil(' ');
result.maxItems = to!size_t(trackLine[typeStart .. maxItemsEnd]);
}
 
size_t urlStart = trackLine.countUntil("url=\"");
if( typeStart != -1 ){
size_t urlEnd = urlStart + trackLine[urlStart .. $ ].countUntil('"');
result.url = trackLine[typeStart .. urlEnd].idup;
}
 
size_t htmlUrlStart = trackLine.countUntil("htmlUrl=\"");
if( typeStart != -1 ){
size_t htmlUrlEnd = htmlUrlStart + trackLine[htmlUrlStart .. $ ].countUntil('"');
result.htmlUrl = trackLine[typeStart .. htmlUrlEnd].idup;
}
 
size_t bigDataUrlStart = trackLine.countUntil("bigDataUrl=\"");
if( typeStart != -1 ){
size_t bigDataUrlEnd = nameStart + trackLine[bigDataUrlStart .. $ ].countUntil('"');
result.bigDataUrl = trackLine[typeStart .. bigDataUrlEnd].idup;
}
 
return result;
}
 
auto bedReader( T = BedData3 )( in char[] filePath, char delimiter='\t' ){
if( !filePath.exists )
throw new FileException( "File %s is do not exist".format(filePath) );
else if( !filePath.isFile )
throw new FileException( "File %s is not a file".format(filePath) );
 
File bedFile = File( to!string(filePath), "r" );
BedMetadata metadata;
TrackLine trackLine;
Bed!(T) bedInstance;
const string browserToken1 = "browser position";
const string browserToken2 = "browser hide";
const string trackToken = "track ";
 
foreach( char[] line; bedFile.byLine() ){
if( line.startsWith( '#' ) ) // comment
continue;
else if( line.empty ) // empty line
continue;
else if( line.startsWith(browserToken1) ){
size_t colonIndex = line.countUntil(':');
size_t minusIndex = line[colonIndex .. $].countUntil('-');
string reversed = to!string( retro( line[browserToken1.length .. colonIndex] ) );
size_t spaceIndexBeforeChrom = reversed.countUntil(' ');
size_t spaceIndexAfterPosition = line[minusIndex..$].countUntil(' ');
size_t endPositionIndex = 0;
if(spaceIndexAfterPosition == -1)
endPositionIndex = line.length;
else
endPositionIndex = colonIndex + minusIndex + spaceIndexAfterPosition;
metadata.chromosome = to!string( retro(reversed[0 .. spaceIndexBeforeChrom]) );
metadata.browserStart = to!size_t(line[colonIndex + 1 .. colonIndex + minusIndex]);
metadata.browserEnd = to!size_t(line[colonIndex + minusIndex + 1 .. endPositionIndex]);
}
else if( line.startsWith(browserToken2) ){
if(line.length > browserToken2.length + 1)
metadata.hide = line[browserToken2.length + 1 .. $].idup;
else
throw new Exception("Malformed metadata line");
}
else if( line.startsWith(trackToken) ){
trackLine = trackLineReader( line );
}
else{ // data in csv format
auto records = csvReader!T(line, delimiter);
bedInstance.bedDataList ~= records.front;
}
}
 
bedInstance.metadata = metadata;
bedInstance.trackLine = trackLine;
 
return bedInstance;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.