forked from wardwheeler/PhyloComGraph-Prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFiles.hs
More file actions
623 lines (561 loc) · 22.4 KB
/
ReadFiles.hs
File metadata and controls
623 lines (561 loc) · 22.4 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
{- |
Module : Functions for reading file formats (fasta, nexus, TNT,csv)
Description : Reads input fasta file and returns lsit of pairs with taxon name and sequence
Copyright : (c) 2014 Ward C. Wheeler, Division of Invertebrate Zoology, AMNH. All rights reserved.
License :
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
Maintainer : Ward Wheeler <wheeler@amnh.org>
Stability : unstable
Portability : portable (I hope)
-}
--TO DO--read file control words and lower case with conversions.
module ReadFiles
( processFastaInput
, processCustomAlphabet
--, processCustomAlphabetMatrix
, processTNTInput
, processNexusInput
, processCsvInput
, printInputData
, printInputDataByTerminal
, printInputDataByCharacter
, CharType(..)
, CharInfo(..)
, RawData
, TermData
, modifyRootCost
) where
import Data.List
import Data.List.Split
import Data.Maybe
import qualified Data.Text as T
import Debug.Trace
-- | CharType data type for input characters
data CharType = Add | NonAdd | Matrix | NucSeq | AminoSeq | GenSeq | Genome
deriving (Read, Show, Eq)
-- | CharInfo information about characters
-- need to add rootCost for forest optimization
data CharInfo = CharInfo { charType :: CharType
, activity :: Bool
, weight :: Float
, costMatrix :: [Int]
, name :: String
, numStates :: Int
, alphabet :: [String]
, rootCost :: Float
} deriving (Show, Eq)
-- | RawData type processed from input to be passed to characterData
--to recode into usable form
--the format is tuple of a list of taxon-data list tuples and charinfo list.
--the data list and charinfo list must have the same length
type RawData = ([TermData], [CharInfo])
-- | type TermData type contians termnal name and list of characters
type TermData = (String, [String])
--Funtions to modify char info values
modifyCharType :: CharInfo -> CharType -> CharInfo
modifyCharType charState x =
charState { charType = x }
modifyActivity :: CharInfo -> Bool -> CharInfo
modifyActivity charState x =
charState { activity = x }
modifyWeight :: CharInfo -> Float -> CharInfo
modifyWeight charState x =
charState { weight = x }
modifyCostMatrix :: CharInfo -> [Int] -> CharInfo
modifyCostMatrix charState x =
charState { costMatrix = x }
modifyName :: CharInfo -> String -> CharInfo
modifyName charState x =
charState { name = x }
modifyNumStates :: CharInfo -> Int -> CharInfo
modifyNumStates charState x =
charState { numStates = x }
modifyRootCost :: CharInfo -> Float -> CharInfo
modifyRootCost charState x =
charState { rootCost = x }
-- | getTaxonNames identify taxon names (with leading '>'--no space after '>') from input lines (to allow for
--non-name info in taxon line ala fasta
getTaxonNames :: [String] -> [String]
getTaxonNames y
| null y = []
| null (head y) = getTaxonNames (tail y)
| otherwise =
let x = head y
xs = tail y
in
if head x /= '>' then getTaxonNames xs else
let wx = words x
hwx = head wx
thwx = tail hwx
in thwx : getTaxonNames xs
-- | getSequences get list for each taxon (names and sequence) by checking if word is name
--and if not appending to that list
getSequences :: [String] -> [String]
getSequences [] = []
getSequences (x:xs)
| null (x:xs) = []
| null x = getSequences xs
| head x == '>' = ">" : getSequences xs
| otherwise = concat (words x) : getSequences xs
-- | getCustomSequences
--get list for each taxon (names and sequence) by checking if word is name
--and if not appending to that list--but leave spaces between symbols
getCustomSequences :: [String] -> [String]
getCustomSequences [] = []
getCustomSequences (x:xs)
| null (x:xs) = []
| null x = getCustomSequences xs
| head x == '>' = ">" : getCustomSequences xs
| otherwise = x : getCustomSequences xs
-- | mergeSeqs
--merge list of String into String
mergeSeqs :: [[String]] -> [String]
mergeSeqs [] = []
mergeSeqs x = map concat x
-- | convertSeqToList makes sequence into list of String to move towards rawData
--type
convertSeqToList :: [(String, String)] -> [(String, [String])]
convertSeqToList x =
if null x then []
else
let (y, z) = head x
in
(y, [z]) : convertSeqToList (tail x)
-- | processFastaInput
--Process input fasta data and return RAwData--but only for asingle input file
--Taxon names must begin with '>' with no spaces, charcaters after spaces on
-- taxon line are ignores (e.g. genbank #s)
--does not filter out numbers fomr sequences to allow generality, spaces in sequences
--shouldn't matter
--now does 2 passes + zip, but should be made into a single pass
--breaking on names with '>' only
processFastaInput :: String -> RawData
processFastaInput x =
if null x then ([],[])
else
let inLines = lines x
taxNames = getTaxonNames inLines
taxSeqs = getSequences inLines
groupSeqs = tail (splitOn [">"] taxSeqs)
contSeqs = mergeSeqs groupSeqs
pairedData = zip taxNames contSeqs
pairedListData = convertSeqToList pairedData
in
let defaultFastaCharInfo = CharInfo {
charType = NucSeq
, activity = True
, weight = 1.0
, costMatrix = []
, name = "FastaSeqChar"
, numStates = 0
, alphabet = ["A", "C", "G", "T", "-"]
, rootCost = 0.5
}
in
(pairedListData, [defaultFastaCharInfo])
-- | getInts takes String andretuns [Int]
getInts :: [String] -> [Int]
getInts inString =
if null inString then []
else
let a = head inString
b = read a :: Int
in
b : getInts (tail inString)
-- | processTCM takes tcmfile contents and returns alphabet and
--costmatrix
processTCM :: String -> ([String], [Int])
processTCM tcmStuff =
if null tcmStuff then error "tcm file empty"
else
let firstLine = head (lines tcmStuff)
alphabet = words firstLine
costMatrix = getInts $ tail (lines tcmStuff) --not really processed now
in
(alphabet, costMatrix)
-- | processCustomAlphabet takes input custom_alphabet sequences
--and returns processed data
processCustomAlphabet :: String -> String -> RawData
processCustomAlphabet x tcmStuff =
if null x then ([],[])
else
let inLines = lines x
taxNames = getTaxonNames inLines
taxSeqs = getCustomSequences inLines
groupSeqs = tail (splitOn [">"] taxSeqs)
contSeqs = mergeSeqs groupSeqs
pairedData = zip taxNames contSeqs
pairedListData = convertSeqToList pairedData
(alphabetList , matrixList) = processTCM tcmStuff
in
let defaultGenSeqCharInfo = CharInfo {
charType = GenSeq
, activity = True
, weight = 1.0
, costMatrix = matrixList
, name = "CustomAlphabetChar"
, numStates = 0
, alphabet = alphabetList
, rootCost = 0.5
}
in
if (length alphabetList) > 63 then error "Alphabet > 63"
else
trace ("\nCA alphabet " ++ show (alphabetList) )-- ++ " " ++ show matrixList)
(pairedListData, [defaultGenSeqCharInfo])
-- | reformatCharString
--Convert String of chars into list of String so a charcater can be a sequence,
--or ambiguous if Hennig86/TNTR/Nexus
reformatCharString :: String -> [String]
reformatCharString x
| null x = []
| head x /= '[' = [head x] : reformatCharString (tail x)
| otherwise =
let y = splitOn "]" (tail x) in
trace ("y = " ++ show y)
(head y : reformatCharString (concat (tail y)))
-- | extractLines Extract pairs form lines of data file body
extractLines :: [String] -> [(String, [String])]
extractLines [] = []
extractLines (x:xs) = (x, reformatCharString (head xs)) : extractLines (tail xs)
-- | getTaxCharPairs
--get Taxon, Characters Pairs assuming on same line separated by space
getTaxCharPairs :: String -> [(String, [String])]
getTaxCharPairs x =
if null x
then []
else
let guts = words x
resPairs = extractLines guts
in
--trace ("in " ++ (show x) ++ "to " ++ (show guts) ++ " to " ++ (show resPairs))
resPairs
-- | getAction returns charcater type info from Hennig86/TNT +,-,[,],w etc
getAction :: Char -> String
getAction x
| x == '-' = "NonAdditive"
| x == '+' = "Additive"
| x == '[' = "Active"
| x == ']' = "InActive"
| x == '/' = "Weight"
| otherwise = error ("Unrecognized character attribute " ++ show x ++ " in Hennig86/TNT input file.")
-- | stringToInt Converts sString to Int, needs to return or check if not an Int.
stringToInt :: String -> Int
stringToInt x =
if null x then error ("Input error " ++ show x ++ "in stringToInt function")
else
let y = read x :: Int
in
y
-- | getScope reads Hennig86/TNT scope options '.' etc and sets
getScope :: [String] -> Int -> [Int]
getScope (x : xs) nchar
| null (x : xs) =
error "Missing character scope in Hennig86/TNT input file."
| length (x : xs) == 2 =
let y = words x
z = words (head xs)
in
if null y then
if null z then [0 .. (nchar - 1)] else
let second = stringToInt (head z) in [0 .. second]
else
let first = stringToInt (head y) in
if null z then [first, nchar - 1] else
let second = stringToInt (head z) in [first .. second]
| length (x : xs) == 1 =
let individualNums = map stringToInt (words x) in individualNums
| otherwise =
error
("Unrecognized character scope " ++
show (x : xs) ++ " in Hennig86/TNT input file.")
-- | processWeightScope removes and returns weight adnd scope as separate theings
processWeightScope :: String -> String -> (String, Int)
processWeightScope attribute x
| null x = ([], 0)
| attribute /= "Weight" = (x, 1)
| otherwise =
let y = words x
weight = stringToInt (head y)
z = unwords (tail y)
in (z, weight)
-- | modifyCharInfo updates CharInfo record, overwriting previous values
--checks to make sure modifucations are within nchar
modifyCharInfo :: [CharInfo] -> String -> [Int] -> Int -> Int -> [CharInfo]
modifyCharInfo oldCharInfo attribute scope weight nchar =
if null scope || (head scope > (nchar - 1)) || (head scope < 0) then oldCharInfo
else
let (xs, ys) = splitAt (head scope) oldCharInfo
in
if attribute == "NonAdditive" then
let newCharElem = modifyCharType (oldCharInfo !! head scope) NonAdd
newCharInfo = xs ++ [newCharElem] ++ tail ys
in
modifyCharInfo newCharInfo attribute (tail scope) weight nchar
else if attribute == "Additive" then
let newCharElem = modifyCharType (oldCharInfo !! head scope) Add
newCharInfo = xs ++ [newCharElem] ++ tail ys
in
modifyCharInfo newCharInfo attribute (tail scope) weight nchar
else if attribute == "Active" then
let newCharElem = modifyActivity (oldCharInfo !! head scope) True
newCharInfo = xs ++ [newCharElem] ++ tail ys
in
modifyCharInfo newCharInfo attribute (tail scope) weight nchar
else if attribute == "InActive" then
let newCharElem = modifyActivity (oldCharInfo !! head scope) False
newCharInfo = xs ++ [newCharElem] ++ tail ys
in
modifyCharInfo newCharInfo attribute (tail scope) weight nchar
else if attribute == "Weight" then
let newCharElem = modifyWeight (oldCharInfo !! head scope) (fromIntegral weight)
newCharInfo = xs ++ [newCharElem] ++ tail ys
in
modifyCharInfo newCharInfo attribute (tail scope) weight nchar
else error ("Unrecognized character attribute " ++ show attribute ++ " in input file.")
-- | setCodes based on Henig/TNT get and set action and scope
setCodes :: [String] -> Int -> [CharInfo] -> [CharInfo]
setCodes x nchar initialCharInfo =
if null x then []
else
let
y = unwords x
attribute = getAction (head y)
(z, weight) = processWeightScope attribute (tail y)
scope = getScope (splitOn "." z) nchar
newCharInfo = modifyCharInfo initialCharInfo attribute scope weight nchar
in
--trace ("parsing " ++ (show y) ++ "atr " ++ (show attribute) ++ "prescope "
-- ++ (show (tail y)) ++ " sc " ++ (show (splitOn "." (tail y))) ++ " scope " ++ (show scope))
newCharInfo
-- | getCharInfo information on charcater type etc from lines after data body--one set
--option (cc -.; cc[ 0;) per semicolon
getCharInfo :: [String] -> Int -> [CharInfo] -> [CharInfo]
getCharInfo (x : xs) nchar initialCharInfo
| null (x : xs) = []
| head (words x) == "proc" || head (words x) == "proc/" =
initialCharInfo
| head (words x) == "cc" || head (words x) == "ccode" =
let newCharInfo = setCodes (tail (words x)) nchar initialCharInfo
newerCharInfo = getCharInfo xs nchar newCharInfo
in newerCharInfo
| otherwise = error ("Unrecongized character option " ++ show x)
-- | initializeCharInfo sets chrcater info to defaults
initializeCharInfo :: CharInfo -> Int -> Int -> [CharInfo]
initializeCharInfo defaults nchar acc =
if acc == nchar then []
else
let newChar = CharInfo {
charType = charType defaults
, activity = activity defaults
, weight = weight defaults
, costMatrix = costMatrix defaults
, name = name defaults
, numStates = numStates defaults
, alphabet = []
, rootCost = 0.5
}
in
newChar : initializeCharInfo defaults nchar (acc + 1)
-- | parse Xread version of Hennig86/TNT file
processXread :: [String] -> RawData
processXread x =
if null x
then ([], [])
else
let defaultHennigCharInfo = CharInfo {
charType = Add
, activity = True
, weight = 1.0
, costMatrix = []
, name = "Hennig86/TNTChar"
, numStates = 0
, alphabet = []
, rootCost = 0.5
}
thang = splitOn "'" (unwords x)
message = head (tail thang)
body = words (head (tail (tail thang)))
nchar = stringToInt (head body)
initialCharInfo = initializeCharInfo defaultHennigCharInfo nchar 0
ntax = stringToInt (head (tail body))
rest = splitOn ";" (unwords (tail (tail body)))
taxCharPair = getTaxCharPairs (head rest)
charInfo = getCharInfo (tail rest) nchar initialCharInfo
in
trace ("Hennig/TNT file message " ++ show message ++ " ntax = " ++ show ntax ++ " nchar = " ++ show nchar)
(taxCharPair, charInfo)
-- | processTNTInput
--Process basic Hennig86/TNT file
--assumes:
--1) starts with "xread"
--2) follwed by a mandatory comment in single quotes -- 'blah' (Need to make
-- this optional later)
--3) line with nchar and ntax
--4) lines of taxname , space, characters with no breaks and single char states
-- or ambiguous in square brackets == [01]
--5) semi colon at end of character info ';'
--6) cc or ccode commands one per line terminated with semicolon ';'
--7) scopes as '.' 'a.' '.b' a.b' or 'a b'
--8) last line 'proc /;'
--Not yet implemented
-- 1) tread
-- 2) costmatrix
processTNTInput :: String -> RawData
processTNTInput x =
if null x
then ([], [])
else
let header = head (words x) in
if T.unpack (T.toLower (T.pack header)) == "xread"
then processXread (tail (words x))
else error "Only processing 'xread' Hennig/TNT files for now"
-- | processNexusInput
--Process basic Nexus file (eventually complete to specification)
processNexusInput :: String -> RawData
processNexusInput x =
error (if null x then error "NEXUS stream empty."
else "NEXUS file parsing not implemented.") --([],[])
-- | assignCharType
--assignType makes a character info array of itype 'y' for each column
--in 'x' file
assignCharType :: Int -> CharInfo -> [CharInfo]
assignCharType x y
| x == 0 = []
| otherwise = y : assignCharType (x - 1) y
-- | getPairDataCSV
--Process CSV input as lines to extract name and data cells
getPairDataCSV :: [String] -> [(String, [String])]
getPairDataCSV x =
if null x then []
else
let currentLine = splitOn "," (head x)
name = head currentLine
characters = tail currentLine
in
if (name == "\n") || (name == "\r") || null characters then getPairDataCSV (tail x)
else
trace ("CSV in " ++ show name ++ " " ++ show (length x) ++ " " ++ show (length characters)) (name, characters) : getPairDataCSV (tail x)
-- | processCsvInput Process CSV ',' delimited file
--assumes
--One taxon per line first field taxon name
-- comma delimited cells in lines, one line per terminal
processCsvInput :: String -> RawData
processCsvInput x =
if null x
then error "CSV stream empty."
else
let inLines = split (oneOf "\n\r") x
pairedNameData = getPairDataCSV inLines
(_, charData) = head pairedNameData
defaultCsvInfo = CharInfo {
charType = GenSeq
, activity = True
, weight = 1.0
, costMatrix = []
, name = "CsvChar"
, numStates = 0
, alphabet = []
, rootCost = 0.5
}
charTypeValue = assignCharType (length charData) defaultCsvInfo
in
trace ("lines in " ++ show (length inLines))
(pairedNameData, charTypeValue)
-- | printStrinListWithNewLine between elements
printStringListWithNewLine :: [String] -> IO ()
printStringListWithNewLine x =
if null x then putStrLn " \\newpage "
else
do
putStr (head x)
putStrLn " \\\\ "
printStringListWithNewLine (tail x)
-- | printInputCharacters
--Print Characters
printInputCharacters :: [String] -> IO ()
printInputCharacters x =
if not (null x) then
do
putStr (head x)
putStr " "
printInputCharacters (tail x)
else putStrLn ""
-- | printInputData
--Print input data as strings to check validity of the read
--uses the ``pairedNameData'' one line per taxon, taxon
--name followed by characters then \n
printInputData :: [(String, [String])] -> IO ()
printInputData x =
if not (null x) then
do
let (a,b) = head x
putStr a
putStr " "
printInputCharacters b
printInputData (tail x)
else putStrLn ""
-- | printInputDataByTerminal
--Print input data as strings to check validity of IPA read
--uses the ``pairedNameData'' one page per taxon
--characters in column including latex commands for IPA
printInputDataByTerminal :: [(String, [String])] -> IO ()
printInputDataByTerminal x =
if not (null x) then
do
let (a,b) = head x
putStrLn "\\end{IPA}"
putStr a
putStrLn "\\begin{IPA}"
putStr "\\\\"
printStringListWithNewLine b
printInputDataByTerminal (tail x)
else putStrLn ""
-- | removeTerminalNames
--Remove the names from pairedData
removeTerminalNames :: [(String, [String])] -> [[String]]
removeTerminalNames x =
if null x then []
else
let (a, b) = head x
in
b : removeTerminalNames (tail x)
-- | printOneAtATime
--Print one char at a time
printOneAtATime :: [[String]] -> IO ()
printOneAtATime x =
if null x then putStrLn ""
else
do
printStringListWithNewLine (head x)
printOneAtATime (tail x)
-- | printInputDataByCharacter
--Print input data as columns of character states 1 at a time then \n
--so get a single column output separated by new line latex characters
printInputDataByCharacter :: [(String, [String])] -> IO ()
printInputDataByCharacter x =
if not (null x) then
do
let y = removeTerminalNames x
let z = transpose y
printOneAtATime z
else putStrLn ""