Skip to content

Commit

Permalink
working CSV parsing string example
Browse files Browse the repository at this point in the history
  • Loading branch information
averagehat committed May 1, 2016
1 parent 3770683 commit 8a957c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/Form.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Data.List.Lazy as List
import Data.Maybe.Unsafe (fromJust)
import Control.Monad.Eff.Exception.Unsafe (unsafeThrow)
import Control.Monad.Eff.Class (liftEff)
import Data.Either.Unsafe (fromRight)

--import Data.Unfoldable (replicateA)
import Control.Monad.Eff.Random as Rand
import App.Routes (Route)
Expand Down Expand Up @@ -88,7 +90,7 @@ init = { name: Nothing, country: Nothing
, format : Fasta, genotype : Nothing
, minDate : Nothing, maxDate : Nothing
, errors : M.empty
, db : seqs
, db : fromRightOrError $ Seq.readCSV "," csv
, hostString : Nothing }
-- In order to give Seq.State an Eq instance, it must be wrapped in NewType

Expand Down Expand Up @@ -211,11 +213,11 @@ toCSV :: Array Seq.State -> String
toCSV xs = header <> "\n" <> (intercalate "\n" $ map toRow xs)
where
header = "name,acc,year,segment"
toRow x = intercalate "," [x.name, x.acc, (show x.year), (show x.segment)]
toRow x = intercalate "," [x.name, x.acc, (show x.year), (fromMaybe "" $ show <$> x.segment)]


query :: State -> Array Seq.State
query q = filter match seqs
query q = filter match q.db
where
match x = (q.acc ==? x.acc) &&
(q.name ==? x.name) &&
Expand Down Expand Up @@ -281,3 +283,10 @@ example3 = {
}

-- [ option [label x, value x] []]
fromRightOrError (Right s) = s
fromRightOrError (Left s) = unsafeThrow s
-- TODO: non-normal Host is not okay apparently
csv = "genotype,segment,name,acc,year,month,day,country,host,serotype,sequence\n" ++
",,11/1666,KR922405,2011,,,Thailand,Human,DENV4,ATGAACCAACGAAAGAAGGTGG\n" ++
",,Br246RR/10,JN983813,2010,9,8,Brazil,Human,DENV4,ATGAACCAACGAAAAAAGGT\n" ++
",,D4/Pakistan/150/2009,KF041260,2009,,,Pakistan,Human,DENV4,ATGAACCAACG"
17 changes: 11 additions & 6 deletions src/Seq.purs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ readCSV sep s = do

type Error = String
process :: Array String -> Array (Array String) -> Either Error (Array State)
process header rows = sequence $ map process' rows
process header rows = sequence $ map process' $ A.filter (not <<< A.null) rows
where
--process' row | (A.length row) < (A.length columns) = Left ("Row not have expected length " ++ (show $ A.length columns) ++ " found length " ++ (show $ A.length row) )
process' :: Array String -> Either Error State
process' row = do
name <- Right $ row `at` "name"
acc <- Right $ row `at` "acc"
country <- Right $ row `at` "country"
year <- toEither "bad year" $ Int.fromString (row `at` "year")
host <- toEither "Bad host" $ readHost (row `at` "host")
serotype <- toEither "Bad serotype" $ readSerotype (row `at` "serotype")
year <- toField Int.fromString "year" row
host <- toField readHost "host" row
serotype <- toField readSerotype "serotype" row
let hostString = (row `at` "host")
let segment = readSegment $ (row `at` "segement")
let genotype = readGenotype $ (row `at` "genotype")
Expand All @@ -156,9 +156,14 @@ process header rows = sequence $ map process' rows
, hostString : hostString
}
where
at xs col = fromMaybe ("Bad column " ++ col ) $ do
at xs col = fromMaybe ("Bad column " <> col <> " in header " <> (intercalate "," header ) <> "\n" <> "row: " <> (intercalate ","xs)) $ do
i <- A.elemIndex col header
xs A.!! i
xs A.!! i
toField :: forall a. (String -> Maybe a) -> String -> Array String -> Either Error a
toField f col row = toEither msg $ f x
where
msg = x ++ " is not a valid " ++ col
x = row `at` col
order row = A.sortBy headerOrder row
headerOrder x y = compare (fromMaybe 9999 (A.elemIndex x header)) (fromMaybe 9999 (A.elemIndex y header))

Expand Down

0 comments on commit 8a957c7

Please sign in to comment.