Commit d868daf3 authored by Kai Zhang's avatar Kai Zhang

fix a bug in Read.hs

parent b3220726
......@@ -123,6 +123,7 @@ listToStrVector xs = do
{#fun igraph_matrix_ncol as ^ { `MatrixPtr' } -> `Int' #}
-- row lists to matrix
listsToMatrixPtr :: [[Double]] -> IO MatrixPtr
listsToMatrixPtr xs = do
mptr <- igraphMatrixNew r c
......@@ -134,11 +135,15 @@ listsToMatrixPtr xs = do
r = length xs
c = maximum $ map length xs
-- to row lists
matrixPtrToLists :: MatrixPtr -> IO [[Double]]
matrixPtrToLists mptr = do
matrixPtrToLists = liftM transpose . matrixPtrToColumnLists
matrixPtrToColumnLists :: MatrixPtr -> IO [[Double]]
matrixPtrToColumnLists mptr = do
r <- igraphMatrixNrow mptr
c <- igraphMatrixNcol mptr
xs <- allocaArray (r*c) $ \ptr -> do
igraphMatrixCopyTo mptr ptr
peekArray (r*c) ptr
return $ transpose $ chunksOf r $ map realToFrac xs
return $ chunksOf r $ map realToFrac xs
......@@ -27,7 +27,7 @@ kamadaKawai :: Graph d => LGraph d v e -> Double -> Double -> Double -> Double -
kamadaKawai gr sigma initemp coolexp kkconst opt = unsafePerformIO $ do
mptr <- mat
igraphLayoutKamadaKawai (_graph gr) mptr (_nIter opt) sigma initemp coolexp kkconst useSeed nullPtr nullPtr nullPtr nullPtr
[x, y] <- matrixPtrToLists mptr
[x, y] <- matrixPtrToColumnLists mptr
return $ zip x y
where
(useSeed, mat) = case _seed opt of
......
......@@ -11,25 +11,25 @@ readAdjMatrix fl = do
c <- B.readFile fl
let (header:xs) = B.lines c
mat = map (map (fst . fromJust . readDouble) . B.words) xs
es = fst $ unzip $ filter f $ zip [ (i,j) | i <- [0..nrow-1], j <- [i..nrow-1] ] $ concat mat
es = fst $ unzip $ filter f $ zip [ (i,j) | i <- [0..nrow-1], j <- [0..nrow-1] ] $ concat mat
nrow = length mat
ncol = length $ head mat
if nrow /= ncol
then error "nrow != ncol"
else return $ mkGraph (nrow, Just $ B.words header) (es, Nothing)
where
f ((i,j),v) = i /= j && v /= 0
f ((i,j),v) = i < j && v /= 0
readAdjMatrixWeighted :: Graph d => FilePath -> IO (LGraph d B.ByteString Double)
readAdjMatrixWeighted fl = do
c <- B.readFile fl
let (header:xs) = B.lines c
mat = map (map (fst . fromJust . readDouble) . B.words) xs
(es, ws) = unzip $ filter f $ zip [ (i,j) | i <- [0..nrow-1], j <- [i..nrow-1] ] $ concat mat
(es, ws) = unzip $ filter f $ zip [ (i,j) | i <- [0..nrow-1], j <- [0..nrow-1] ] $ concat mat
nrow = length mat
ncol = length $ head mat
if nrow /= ncol
then error "nrow != ncol"
else return $ mkGraph (nrow, Just $ B.words header) (es, Just ws)
where
f ((i,j),v) = i /= j && v /= 0
f ((i,j),v) = i < j && v /= 0
......@@ -18,15 +18,11 @@ import System.Environment
main = do
[fl] <- getArgs
g <- readAdjMatrix fl :: IO (LGraph U B.ByteString ())
let n = nNodes g
r = map (f g) [0..n-1]
mapM_ h r
where
f g i = let name = nodeLab g i
xs = map (nodeLab g) $ neighbors g i
in (name, B.intercalate "," xs)
h (a,b) = do
B.putStr a
B.putStr "\t"
B.putStrLn b
g <- readAdjMatrixWeighted fl :: IO (LGraph U B.ByteString Double)
let m = _labelToNode g
[a] = M.lookupDefault [] "H3K27ac" m
[b] = M.lookupDefault [] "H3K4me1" m
ws = Just $ map (abs . edgeLabByEid g) [0 .. nEdges g - 1]
cs = (map.map) (nodeLab g) $ communityLeadingEigenvector g ws 10000
print $ edgeLab g (b,a)
mapM_ print cs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment