-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fasta parser and writer + spec (#15)
* wrike-385288488: fasta parser and writer + spec
- Loading branch information
Showing
11 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: cobot-io | ||
version: 0.1.1.1 | ||
version: 0.1.2.0 | ||
github: "less-wrong/cobot-io" | ||
license: BSD3 | ||
category: Bio | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Bio.FASTA | ||
( module T | ||
, fromFile | ||
, toFile | ||
, fastaP | ||
) where | ||
|
||
import Bio.FASTA.Parser | ||
import Bio.FASTA.Type as T | ||
import Bio.FASTA.Writer (fastaToText) | ||
import Control.Monad.IO.Class (MonadIO, liftIO) | ||
import Data.Attoparsec.Text (parseOnly) | ||
import Data.Text.IO (readFile, writeFile) | ||
import Prelude hiding (writeFile, readFile) | ||
|
||
-- | Reads 'FastaSequence' from given file. | ||
-- | ||
fromFile :: MonadIO m => FilePath -> m (Fasta Char) | ||
fromFile f = liftIO (readFile f) >>= either fail pure . parseOnly fastaP | ||
|
||
-- | Writes 'FastaSequence' to file. | ||
-- | ||
toFile :: MonadIO m => Fasta Char -> FilePath -> m () | ||
toFile s f = liftIO $ writeFile f $ fastaToText s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
>3HMX:A|PDBID|CHAIN|SEQUENCE | ||
IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSE | ||
VLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL | ||
|
||
>7HMX:A|PDBID|CHAIN|SEQUENCE | ||
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE | ||
VLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module FASTASpec where | ||
|
||
import Bio.FASTA (fromFile, toFile) | ||
import Bio.FASTA.Type (FastaItem(..), Fasta) | ||
import Bio.Sequence (bareSequence) | ||
import Prelude hiding (writeFile, readFile) | ||
import Test.Hspec | ||
|
||
correctFasta :: Fasta Char | ||
correctFasta = [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL"), FastaItem "7HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL")] | ||
|
||
fastaSpec :: Spec | ||
fastaSpec = describe "Fasta file parser." $ do | ||
parseFile "test/FASTA/correct.fasta" | ||
writeFile "test/FASTA/test.fasta" | ||
|
||
parseFile :: FilePath -> Spec | ||
parseFile path = describe "fromFile" $ do | ||
it "correctly parses fasta from file" $ do | ||
fasta <- fromFile path | ||
fasta `shouldBe` correctFasta | ||
|
||
writeFile :: FilePath -> Spec | ||
writeFile path = describe "writeFile" $ do | ||
it "correctly write fasta into file" $ do | ||
toFile correctFasta path | ||
fasta <- fromFile path | ||
fasta `shouldBe` correctFasta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module FastaParserSpec where | ||
|
||
import Bio.FASTA.Parser (fastaP) | ||
import Bio.FASTA.Type (FastaItem(..)) | ||
import Bio.Sequence (bareSequence) | ||
import Data.Attoparsec.Text (parseOnly) | ||
import Test.Hspec | ||
|
||
fastaParserSpec :: Spec | ||
fastaParserSpec = describe "Fasta format parser." $ do | ||
emptyFasta | ||
onlyName | ||
oneSequence | ||
twoSequences | ||
sequenceWithDigit | ||
sequenceWithWrongName | ||
sequenceWithSpacesInName | ||
sequenceWithSeveralEndOfLine | ||
sequenceWithSeveralEndOfLineInSequence | ||
sequenceWithTabsInName | ||
sequenceWithTabsInSequence | ||
|
||
emptyFasta :: Spec | ||
emptyFasta = describe "emptyFasta" $ do | ||
it "correctly parses empty fasta" $ do | ||
let res = parseOnly fastaP "" | ||
res `shouldBe` Right [] | ||
|
||
onlyName :: Spec | ||
onlyName = describe "onlyName" $ do | ||
it "correctly parses fasta without sequence" $ do | ||
let res = parseOnly fastaP ">3HMX:A|PDBID|CHAIN|SEQUENCE" | ||
res `shouldBe` Right [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "")] | ||
|
||
oneSequence :: Spec | ||
oneSequence = describe "oneSequence" $ do | ||
it "correctly parses one correct sequence" $ do | ||
let res = parseOnly fastaP ">3HMX:A|PDBID|CHAIN|SEQUENCE\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSE\nVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL\n" | ||
res `shouldBe` Right [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL")] | ||
|
||
twoSequences :: Spec | ||
twoSequences = describe "twoSequences" $ do | ||
it "correctly parses two correct sequences" $ do | ||
let res = parseOnly fastaP ">3HMX:A|PDBID|CHAIN|SEQUENCE\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSE\nVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL\n>7HMX:A|PDBID|CHAIN|SEQUENCE\nEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\nVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL" | ||
res `shouldBe` Right [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL"), FastaItem "7HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL")] | ||
|
||
sequenceWithDigit :: Spec | ||
sequenceWithDigit = describe "sequenceWithDigit" $ do | ||
it "correctly parses incorrect sequence with digit" $ do | ||
let res = parseOnly fastaP ">123\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEE4GITWTLDQSSE" | ||
res `shouldBe` Right [FastaItem "123" (bareSequence "")] | ||
|
||
sequenceWithWrongName :: Spec | ||
sequenceWithWrongName = describe "sequenceWithWrongName" $ do | ||
it "correctly parses incorrect sequence with wrong name" $ do | ||
let res = parseOnly fastaP "123\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE" | ||
res `shouldBe` Right [] | ||
|
||
sequenceWithSpacesInName :: Spec | ||
sequenceWithSpacesInName = describe "sequenceWithSpacesInName" $ do | ||
it "correctly parses sequence with spaces in name" $ do | ||
let res = parseOnly fastaP "> this is my sequence \nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE" | ||
res `shouldBe` Right [FastaItem "this is my sequence" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE")] | ||
|
||
sequenceWithSeveralEndOfLine :: Spec | ||
sequenceWithSeveralEndOfLine = describe "sequenceWithSeveralEndOfLine" $ do | ||
it "correctly parses sequence with several \n after name" $ do | ||
let res = parseOnly fastaP ">this is my sequence\n\n\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE" | ||
res `shouldBe` Right [FastaItem "this is my sequence" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE")] | ||
|
||
sequenceWithSeveralEndOfLineInSequence :: Spec | ||
sequenceWithSeveralEndOfLineInSequence = describe "sequenceWithSeveralEndOfLineInSequence" $ do | ||
it "correctly parses sequence with several \n between sequence parts" $ do | ||
let res = parseOnly fastaP ">this is my sequence\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE\n\n\nYYYYYYYYYYYYYYYYYYYYYYYY" | ||
res `shouldBe` Right [FastaItem "this is my sequence" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSEYYYYYYYYYYYYYYYYYYYYYYYY")] | ||
|
||
sequenceWithTabsInName :: Spec | ||
sequenceWithTabsInName = describe "sequenceWithTabsInName" $ do | ||
it "correctly parses sequence with tabs in name" $ do | ||
let res = parseOnly fastaP ">\tthis\tis\tmy\tsequence\t\t\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE" | ||
res `shouldBe` Right [FastaItem "this\tis\tmy\tsequence" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE")] | ||
|
||
sequenceWithTabsInSequence :: Spec | ||
sequenceWithTabsInSequence = describe "sequenceWithTabsInSequence" $ do | ||
it "correctly parses sequence with tabs between sequence parts" $ do | ||
let res = parseOnly fastaP ">this is my sequence\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSE\t\t\nYYYYYYYYYYYYYYYYYYYYYYYY\t\n" | ||
res `shouldBe` Right [FastaItem "this is my sequence" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEGITWTLDQSSEYYYYYYYYYYYYYYYYYYYYYYYY")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module FastaWriterSpec where | ||
|
||
import Bio.FASTA.Writer (fastaToText) | ||
import Bio.FASTA.Type (FastaItem(..)) | ||
import Bio.Sequence (bareSequence) | ||
import Test.Hspec | ||
|
||
fastaWriterSpec :: Spec | ||
fastaWriterSpec = describe "Fasta format parser." $ do | ||
emptyFasta | ||
oneShortSequence | ||
oneLongSequence | ||
twoSequences | ||
|
||
emptyFasta :: Spec | ||
emptyFasta = describe "emptyFasta" $ do | ||
it "correctly write empty fasta" $ do | ||
let res = fastaToText [] | ||
res `shouldBe` "" | ||
|
||
oneShortSequence :: Spec | ||
oneShortSequence = describe "oneShortSequence" $ do | ||
it "correctly write one correct short (less than 80 chars) sequence" $ do | ||
let res = fastaToText [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL")] | ||
res `shouldBe` ">3HMX:A|PDBID|CHAIN|SEQUENCE\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL\n" | ||
|
||
oneLongSequence :: Spec | ||
oneLongSequence = describe "oneLongSequence" $ do | ||
it "correctly write one correct long (more than 80 chars) sequence" $ do | ||
let res = fastaToText [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLLLLHKKEDGIWSTDILKDQKEPKNKTFLRCEAKNYSGRFTCWWLTTISTDLTFSVKSSRGSSDPQGVTCGAATLSAERVRGDNKEYEYSVECQEDSACPAAEESLPIEVMVDAVHKLKYENYTSSFFIRDIIKPDPPKNLQLKPLKNSRQVEVSWEYPDTWSTPHSYFSLTFCVQVQGKSKREKKDRVFTDKTSATVICRKNASISVRAQDRYYSSSWSEWASVPCS")] | ||
res `shouldBe` ">3HMX:A|PDBID|CHAIN|SEQUENCE\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL\nLLHKKEDGIWSTDILKDQKEPKNKTFLRCEAKNYSGRFTCWWLTTISTDLTFSVKSSRGSSDPQGVTCGAATLSAERVRG\nDNKEYEYSVECQEDSACPAAEESLPIEVMVDAVHKLKYENYTSSFFIRDIIKPDPPKNLQLKPLKNSRQVEVSWEYPDTW\nSTPHSYFSLTFCVQVQGKSKREKKDRVFTDKTSATVICRKNASISVRAQDRYYSSSWSEWASVPCS\n" | ||
|
||
twoSequences :: Spec | ||
twoSequences = describe "twoSequences" $ do | ||
it "correctly write two correct sequences" $ do | ||
let res = fastaToText [FastaItem "3HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL"), FastaItem "7HMX:A|PDBID|CHAIN|SEQUENCE" (bareSequence "IWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLLLLHKKEDGIWSTDILKDQKEPKNKTFLRCEAKNYSGRFTCWWLTTISTDLTFSVKSSRGSSDPQGVTCGAATLSAERVRGDNKEYEYSVECQEDSACPAAEESLPIEVMVDAVHKLKYENYTSSFFIRDIIKPDPPKNLQLKPLKNSRQVEVSWEYPDTWSTPHSYFSLTFCVQVQGKSKREKKDRVFTDKTSATVICRKNASISVRAQDRYYSSSWSEWASVPCS")] | ||
res `shouldBe` ">3HMX:A|PDBID|CHAIN|SEQUENCE\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL\n>7HMX:A|PDBID|CHAIN|SEQUENCE\nIWELKKDVYVVELDWYPDAPGEMVVLTCDTPEEDGITWTLDQSSEVLGSGKTLTIQVKEFGDAGQYTCHKGGEVLSHSLL\nLLHKKEDGIWSTDILKDQKEPKNKTFLRCEAKNYSGRFTCWWLTTISTDLTFSVKSSRGSSDPQGVTCGAATLSAERVRG\nDNKEYEYSVECQEDSACPAAEESLPIEVMVDAVHKLKYENYTSSFFIRDIIKPDPPKNLQLKPLKNSRQVEVSWEYPDTW\nSTPHSYFSLTFCVQVQGKSKREKKDRVFTDKTSATVICRKNASISVRAQDRYYSSSWSEWASVPCS\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters