[Haskell-beginners] text file to data type

Brent Pedersen bpederse at gmail.com
Mon Dec 7 22:43:04 EST 2009


hi, i have files with lots (millions of rows) of data like this:
chr6    chr10  96.96   3392    101 2   79030508    79033899    4160024
4163413 0.0 5894
chr6    chr10  93.19   4098    228 13  117152751   117156826
11355389    11359457    0.0 5886
chr6    chr10  95.82   3445    130 5   112422073   112425513   7785396
7788830 0.0 5666

and i'd like to read it into a type like this:

data Blast = Blast { query :: S.ByteString
                   , subject :: S.ByteString
                   , hitlen :: Int
                   , mismatch :: Int
                   , gaps :: Int
                   , qstart :: Int
                   , qstop :: Int
                   , sstart :: Int
                   , sstop :: Int
                   , pctid :: Double
                   , evalue :: Double
                   , bitscore :: Double
                   } deriving (Show)

where each of those fields corresponds to a column in the file.
in python, i do something like:

>>> line = [fn(col) for fn, col in zip([str, str, int, int, int, int, int, int, int, float, float], sline.split("\t")]

what's a fast, simple way to do this in haskell?

is it something like:

instance Read Blast where
    readsPrec s = ?????


any pointers on where to look for simple examples of this type of parsing would
be much appreciated.
thanks,
-brentp


More information about the Beginners mailing list