FASTA format: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 27: Line 27:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V FASTA =
<syntaxhighlight lang="11l">V FASTA =
|‘>Rosetta_Example_1
|‘>Rosetta_Example_1
THERECANBENOSPACE
THERECANBENOSPACE
Line 51: Line 51:
R r
R r


print(fasta_parse(FASTA).map((key, val) -> ‘#.: #.’.format(key, val)).join("\n"))</lang>
print(fasta_parse(FASTA).map((key, val) -> ‘#.: #.’.format(key, val)).join("\n"))</syntaxhighlight>


{{out}}
{{out}}
Line 61: Line 61:
=={{header|Action!}}==
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/fasta.txt fasta.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/fasta.txt fasta.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
<lang Action!>PROC ReadFastaFile(CHAR ARRAY fname)
<syntaxhighlight lang="action!">PROC ReadFastaFile(CHAR ARRAY fname)
CHAR ARRAY line(256)
CHAR ARRAY line(256)
CHAR ARRAY tmp(256)
CHAR ARRAY tmp(256)
Line 90: Line 90:


ReadFastaFile(fname)
ReadFastaFile(fname)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/FASTA_format.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/FASTA_format.png Screenshot from Atari 8-bit computer]
Line 102: Line 102:
The simple solution just reads the file (from standard input) line by line and directly writes it to the standard output.
The simple solution just reads the file (from standard input) line by line and directly writes it to the standard output.


<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;


procedure Simple_FASTA is
procedure Simple_FASTA is
Line 129: Line 129:
end loop;
end loop;


end Simple_FASTA;</lang>
end Simple_FASTA;</syntaxhighlight>


{{out}}
{{out}}
Line 142: Line 142:




<lang Ada>with Ada.Text_IO, Ada.Containers.Indefinite_Ordered_Maps; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Containers.Indefinite_Ordered_Maps; use Ada.Text_IO;


procedure FASTA is
procedure FASTA is
Line 187: Line 187:
Map.Iterate(Process => Print_Pair'Access); -- print Map
Map.Iterate(Process => Print_Pair'Access); -- print Map
end FASTA;</lang>
end FASTA;</syntaxhighlight>


=={{header|Aime}}==
=={{header|Aime}}==


<lang aime>file f;
<syntaxhighlight lang="aime">file f;
text n, s;
text n, s;


Line 205: Line 205:
}
}


o_(n);</lang>
o_(n);</syntaxhighlight>
{{Out}}
{{Out}}
<pre>>Rosetta_Example_1: THERECANBENOSPACE
<pre>>Rosetta_Example_1: THERECANBENOSPACE
Line 211: Line 211:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% reads FASTA format data from standard input and write the results to standard output %
% reads FASTA format data from standard input and write the results to standard output %
% only handles the ">" line start %
% only handles the ">" line start %
Line 236: Line 236:
readcard( line );
readcard( line );
end while_not_eof
end while_not_eof
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 245: Line 245:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>parseFasta: function [data][
<syntaxhighlight lang="rebol">parseFasta: function [data][
result: #[]
result: #[]
current: ø
current: ø
Line 268: Line 268:
}
}


inspect.muted parseFasta text</lang>
inspect.muted parseFasta text</syntaxhighlight>


{{out}}
{{out}}
Line 278: Line 278:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Data =
<syntaxhighlight lang="autohotkey">Data =
(
(
>Rosetta_Example_1
>Rosetta_Example_1
Line 291: Line 291:
Gui, add, Edit, w700, % Data
Gui, add, Edit, w700, % Data
Gui, show
Gui, show
return</lang>
return</syntaxhighlight>
{{out}}
{{out}}
<pre>>Rosetta_Example_1: THERECANBENOSPACE
<pre>>Rosetta_Example_1: THERECANBENOSPACE
Line 297: Line 297:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FASTA_FORMAT.AWK filename
# syntax: GAWK -f FASTA_FORMAT.AWK filename
# stop processing each file when an error is encountered
# stop processing each file when an error is encountered
Line 349: Line 349:
return
return
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 360: Line 360:
{{works with|QBasic|1.1}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<lang qbasic>FUNCTION checkNoSpaces (s$)
<syntaxhighlight lang="qbasic">FUNCTION checkNoSpaces (s$)
FOR i = 1 TO LEN(s$) - 1
FOR i = 1 TO LEN(s$) - 1
IF MID$(s$, i, 1) = CHR$(32) OR MID$(s$, i, 1) = CHR$(9) THEN checkNoSpaces = 0
IF MID$(s$, i, 1) = CHR$(32) OR MID$(s$, i, 1) = CHR$(9) THEN checkNoSpaces = 0
Line 389: Line 389:
END IF
END IF
LOOP
LOOP
CLOSE #1</lang>
CLOSE #1</syntaxhighlight>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
{{trans|QBasic}}
{{trans|QBasic}}
<lang qbasic>DEF EOF(f)
<syntaxhighlight lang="qbasic">DEF EOF(f)
IF END #f THEN LET EOF = -1 ELSE LET EOF = 0
IF END #f THEN LET EOF = -1 ELSE LET EOF = 0
END DEF
END DEF
Line 426: Line 426:
LOOP
LOOP
CLOSE #1
CLOSE #1
END</lang>
END</syntaxhighlight>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<lang BASIC256>open 1, "input.fasta"
<syntaxhighlight lang="basic256">open 1, "input.fasta"


first = True
first = True
Line 461: Line 461:
next i
next i
return True
return True
end function</lang>
end function</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 501: Line 501:
free(line);
free(line);
exit(EXIT_SUCCESS);
exit(EXIT_SUCCESS);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 508: Line 508:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==


<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.IO;
Line 559: Line 559:
Console.ReadLine();
Console.ReadLine();
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <fstream>
#include <fstream>


Line 602: Line 602:
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 610: Line 610:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(defn fasta [pathname]
<syntaxhighlight lang="clojure">(defn fasta [pathname]
(with-open [r (clojure.java.io/reader pathname)]
(with-open [r (clojure.java.io/reader pathname)]
(doseq [line (line-seq r)]
(doseq [line (line-seq r)]
(if (= (first line) \>)
(if (= (first line) \>)
(print (format "%n%s: " (subs line 1)))
(print (format "%n%s: " (subs line 1)))
(print line)))))</lang>
(print line)))))</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>;; * The input file as a parameter
<syntaxhighlight lang="lisp">;; * The input file as a parameter
(defparameter *input* #p"fasta.txt"
(defparameter *input* #p"fasta.txt"
"The input file name.")
"The input file name.")
Line 631: Line 631:
:do (format t "~&~a: " (subseq line 1))
:do (format t "~&~a: " (subseq line 1))
:else
:else
:do (format t "~a" line)))</lang>
:do (format t "~a" line)))</syntaxhighlight>
{{out}}
{{out}}
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 637: Line 637:
=={{header|Crystal}}==
=={{header|Crystal}}==
If you want to run below code online, then paste below code to [https://play.crystal-lang.org/#/cr <b>playground</b>]
If you want to run below code online, then paste below code to [https://play.crystal-lang.org/#/cr <b>playground</b>]
<lang ruby>
<syntaxhighlight lang="ruby">
# create tmp fasta file in /tmp/
# create tmp fasta file in /tmp/
tmpfile = "/tmp/tmp"+Random.rand.to_s+".fasta"
tmpfile = "/tmp/tmp"+Random.rand.to_s+".fasta"
Line 664: Line 664:
# show fasta component
# show fasta component
fasta.each { |k,v| puts "#{k}: #{v}"}
fasta.each { |k,v| puts "#{k}: #{v}"}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 674: Line 674:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: formatting io kernel sequences ;
<syntaxhighlight lang="factor">USING: formatting io kernel sequences ;
IN: rosetta-code.fasta
IN: rosetta-code.fasta


Line 683: Line 683:
readln rest "%s: " printf [ process-fasta-line ] each-line ;
readln rest "%s: " printf [ process-fasta-line ] each-line ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 692: Line 692:
=={{header|Forth}}==
=={{header|Forth}}==
Developed with gforth 0.7.9
Developed with gforth 0.7.9
<lang forth>1024 constant max-Line
<syntaxhighlight lang="forth">1024 constant max-Line
char > constant marker
char > constant marker


Line 708: Line 708:
cr ;
cr ;
Test
Test
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 718: Line 718:
This program sticks to the task as described in the heading and doesn't allow for any of the (apparently) obsolete
This program sticks to the task as described in the heading and doesn't allow for any of the (apparently) obsolete
practices described in the Wikipedia article :
practices described in the Wikipedia article :
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Function checkNoSpaces(s As String) As Boolean
Function checkNoSpaces(s As String) As Boolean
Line 755: Line 755:
Print : Print
Print : Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 764: Line 764:


=={{header|Gambas}}==
=={{header|Gambas}}==
<lang gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()
Dim sList As String = File.Load("../FASTA")
Dim sList As String = File.Load("../FASTA")
Dim sTemp, sOutput As String
Dim sTemp, sOutput As String
Line 779: Line 779:
Print sOutput
Print sOutput


End</lang>
End</syntaxhighlight>
Output:
Output:
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 786: Line 786:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 828: Line 828:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 842: Line 842:
We parse FASTA by hand (generally not a recommended approach). We use the fact that groupBy walks the list from the head and groups the items by a predicate; here we first concatenate all the fasta strings and then pair those with each respective name.
We parse FASTA by hand (generally not a recommended approach). We use the fact that groupBy walks the list from the head and groups the items by a predicate; here we first concatenate all the fasta strings and then pair those with each respective name.


<lang haskell>import Data.List ( groupBy )
<syntaxhighlight lang="haskell">import Data.List ( groupBy )


parseFasta :: FilePath -> IO ()
parseFasta :: FilePath -> IO ()
Line 858: Line 858:
pair :: [String] -> [(String, String)]
pair :: [String] -> [(String, String)]
pair [] = []
pair [] = []
pair (x : y : xs) = (drop 1 x, y) : pair xs</lang>
pair (x : y : xs) = (drop 1 x, y) : pair xs</syntaxhighlight>


{{out}}
{{out}}
Line 868: Line 868:
We parse FASTA using parser combinators. Normally you'd use something like Trifecta or Parsec, but here we use ReadP, because it is simple and also included in ghc by default. With other parsing libraries the code would be almost the same.
We parse FASTA using parser combinators. Normally you'd use something like Trifecta or Parsec, but here we use ReadP, because it is simple and also included in ghc by default. With other parsing libraries the code would be almost the same.


<lang haskell>import Text.ParserCombinators.ReadP
<syntaxhighlight lang="haskell">import Text.ParserCombinators.ReadP
import Control.Applicative ( (<|>) )
import Control.Applicative ( (<|>) )
import Data.Char ( isAlpha, isAlphaNum )
import Data.Char ( isAlpha, isAlphaNum )
Line 885: Line 885:
name = char '>' *> many (satisfy isAlphaNum <|> char '_') <* newline
name = char '>' *> many (satisfy isAlphaNum <|> char '_') <* newline
code = concat <$> many (many (satisfy isAlpha) <* newline)
code = concat <$> many (many (satisfy isAlpha) <* newline)
newline = char '\n'</lang>
newline = char '\n'</syntaxhighlight>


{{out}}
{{out}}
Line 893: Line 893:
=={{header|J}}==
=={{header|J}}==
Needs chunking to handle huge files.
Needs chunking to handle huge files.
<lang j>require 'strings' NB. not needed for J versions greater than 6.
<syntaxhighlight lang="j">require 'strings' NB. not needed for J versions greater than 6.
parseFasta=: ((': ' ,~ LF&taketo) , (LF -.~ LF&takeafter));._1</lang>
parseFasta=: ((': ' ,~ LF&taketo) , (LF -.~ LF&takeafter));._1</syntaxhighlight>
'''Example Usage'''
'''Example Usage'''
<lang j> Fafile=: noun define
<syntaxhighlight lang="j"> Fafile=: noun define
>Rosetta_Example_1
>Rosetta_Example_1
THERECANBENOSPACE
THERECANBENOSPACE
Line 906: Line 906:
parseFasta Fafile
parseFasta Fafile
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</lang>
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</syntaxhighlight>


Nowadays, most machines have gigabytes of memory. However, if it's necessary to process FASTA content on a system with inadequate memory we can use files to hold intermediate results. For example:
Nowadays, most machines have gigabytes of memory. However, if it's necessary to process FASTA content on a system with inadequate memory we can use files to hold intermediate results. For example:


<lang J>bs=: 2
<syntaxhighlight lang="j">bs=: 2
chunkFasta=: {{
chunkFasta=: {{
r=. EMPTY
r=. EMPTY
Line 937: Line 937:
end.
end.
r
r
}}</lang>
}}</syntaxhighlight>


Here, we're using a block size of 2 bytes, to illustrate correctness. If speed matters, we should use something significantly larger.
Here, we're using a block size of 2 bytes, to illustrate correctness. If speed matters, we should use something significantly larger.
Line 945: Line 945:
Thus, if '~/fasta.txt' contains the example file for this task and we want to store intermediate results in the '~temp' directory, we could use:
Thus, if '~/fasta.txt' contains the example file for this task and we want to store intermediate results in the '~temp' directory, we could use:


<lang J> fasta=: '~temp' chunkFasta '~/fasta.txt'</lang>
<syntaxhighlight lang="j"> fasta=: '~temp' chunkFasta '~/fasta.txt'</syntaxhighlight>


And, to complete the task:
And, to complete the task:


<lang J> ;(,': ',,&LF)each/"1 fread each fasta
<syntaxhighlight lang="j"> ;(,': ',,&LF)each/"1 fread each fasta
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</lang>
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{trans|D}}
{{trans|D}}
{{works with|Java|7}}
{{works with|Java|7}}
<lang java>import java.io.*;
<syntaxhighlight lang="java">import java.io.*;
import java.util.Scanner;
import java.util.Scanner;


Line 981: Line 981:
System.out.println();
System.out.println();
}
}
}</lang>
}</syntaxhighlight>


<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 989: Line 989:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
The code below uses Nodejs to read the file.
The code below uses Nodejs to read the file.
<syntaxhighlight lang="javascript">
<lang JavaScript>
const fs = require("fs");
const fs = require("fs");
const readline = require("readline");
const readline = require("readline");
Line 1,018: Line 1,018:


readInterface.on("close", () => process.stdout.write("\n"));
readInterface.on("close", () => process.stdout.write("\n"));
</syntaxhighlight>
</lang>


<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 1,029: Line 1,029:
in each cycle, only as many lines are read as are required to compose an output line. <br>
in each cycle, only as many lines are read as are required to compose an output line. <br>
Notice that an additional ">" must be provided to "foreach" to ensure the final block of lines of the input file are properly assembled.
Notice that an additional ">" must be provided to "foreach" to ensure the final block of lines of the input file are properly assembled.
<syntaxhighlight lang="jq">
<lang jq>
def fasta:
def fasta:
foreach (inputs, ">") as $line
foreach (inputs, ">") as $line
Line 1,040: Line 1,040:
;
;


fasta</lang>
fasta</syntaxhighlight>
{{out}}
{{out}}
<lang sh>$ jq -n -R -r -f FASTA_format.jq < FASTA_format.fasta
<syntaxhighlight lang="sh">$ jq -n -R -r -f FASTA_format.jq < FASTA_format.fasta
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</lang>
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>for line in eachline("data/fasta.txt")
<syntaxhighlight lang="julia">for line in eachline("data/fasta.txt")
if startswith(line, '>')
if startswith(line, '>')
print(STDOUT, "\n$(line[2:end]): ")
print(STDOUT, "\n$(line[2:end]): ")
Line 1,055: Line 1,055:
print(STDOUT, "$line")
print(STDOUT, "$line")
end
end
end</lang>
end</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


import java.util.Scanner
import java.util.Scanner
Line 1,088: Line 1,088:
}
}
sc.close()
sc.close()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,097: Line 1,097:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>local file = io.open("input.txt","r")
<syntaxhighlight lang="lua">local file = io.open("input.txt","r")
local data = file:read("*a")
local data = file:read("*a")
file:close()
file:close()
Line 1,120: Line 1,120:
for k,v in pairs(output) do
for k,v in pairs(output) do
print(k..": "..v)
print(k..": "..v)
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,134: Line 1,134:




<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
Class FASTA_MACHINE {
Class FASTA_MACHINE {
Line 1,238: Line 1,238:
}
}
checkit
checkit
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica has built-in support for FASTA files and strings
Mathematica has built-in support for FASTA files and strings
<lang Mathematica>ImportString[">Rosetta_Example_1
<syntaxhighlight lang="mathematica">ImportString[">Rosetta_Example_1
THERECANBENOSPACE
THERECANBENOSPACE
>Rosetta_Example_2
>Rosetta_Example_2
Line 1,248: Line 1,248:
LINESBUTTHEYALLMUST
LINESBUTTHEYALLMUST
BECONCATENATED
BECONCATENATED
", "FASTA"]</lang>
", "FASTA"]</syntaxhighlight>
{{out}}
{{out}}
<pre>{"THERECANBENOSPACE", "THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED"}</pre>
<pre>{"THERECANBENOSPACE", "THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED"}</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<lang Nim>
import strutils
import strutils


Line 1,274: Line 1,274:


fasta(input)
fasta(input)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,282: Line 1,282:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>class Fasta {
<syntaxhighlight lang="objeck">class Fasta {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
if(args->Size() = 1) {
if(args->Size() = 1) {
Line 1,307: Line 1,307:
}
}
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,320: Line 1,320:
The program reads and processes the input one line at a time, and directly prints out the chunk of data available. The long strings are not concatenated in memory but just examined and processed as necessary: either printed out as is in the case of part of a sequence, or formatted in the case of the name (what I call the label), and managing the new lines where needed.
The program reads and processes the input one line at a time, and directly prints out the chunk of data available. The long strings are not concatenated in memory but just examined and processed as necessary: either printed out as is in the case of part of a sequence, or formatted in the case of the name (what I call the label), and managing the new lines where needed.
{{works with|OCaml|4.03+}}
{{works with|OCaml|4.03+}}
<lang ocaml>
<syntaxhighlight lang="ocaml">
(* This program reads from the standard input and writes to standard output.
(* This program reads from the standard input and writes to standard output.
* Examples of use:
* Examples of use:
Line 1,361: Line 1,361:
let () =
let () =
print_fasta stdin
print_fasta stdin
</syntaxhighlight>
</lang>
{{out}}
{{out}}
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_1: THERECANBENOSPACE
Line 1,367: Line 1,367:


=={{header|Pascal}}==
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">
<lang Pascal>
program FASTA_Format;
program FASTA_Format;
// FPC 3.0.2
// FPC 3.0.2
Line 1,407: Line 1,407:
Close(InF);
Close(InF);
end.
end.
</syntaxhighlight>
</lang>


FASTA_Format < test.fst
FASTA_Format < test.fst
Line 1,416: Line 1,416:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>my $fasta_example = <<'END_FASTA_EXAMPLE';
<syntaxhighlight lang="perl">my $fasta_example = <<'END_FASTA_EXAMPLE';
>Rosetta_Example_1
>Rosetta_Example_1
THERECANBENOSPACE
THERECANBENOSPACE
Line 1,434: Line 1,434:
print;
print;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,442: Line 1,442:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #004080;">bool</span> <span style="color: #000000;">first</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">first</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"fasta.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"fasta.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">)</span>
Line 1,466: Line 1,466:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,474: Line 1,474:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de fasta (F)
<syntaxhighlight lang="picolisp">(de fasta (F)
(in F
(in F
(while (from ">")
(while (from ">")
Line 1,481: Line 1,481:
(prin (line T)) )
(prin (line T)) )
(prinl) ) ) )
(prinl) ) ) )
(fasta "fasta.dat")</lang>
(fasta "fasta.dat")</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,491: Line 1,491:
When working with a real file, the content of the <code>$file</code> variable would be: <code>Get-Content -Path .\FASTA_file.txt -ReadCount 1000</code>. The <code>-ReadCount</code> parameter value for large files is unknown, yet sure to be a value between 1,000 and 10,000 depending upon the length of file and length of the records in the file. Experimentation is the only way to know the optimum value.
When working with a real file, the content of the <code>$file</code> variable would be: <code>Get-Content -Path .\FASTA_file.txt -ReadCount 1000</code>. The <code>-ReadCount</code> parameter value for large files is unknown, yet sure to be a value between 1,000 and 10,000 depending upon the length of file and length of the records in the file. Experimentation is the only way to know the optimum value.
{{works with|PowerShell|4.0+}}
{{works with|PowerShell|4.0+}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
$file = @'
$file = @'
>Rosetta_Example_1
>Rosetta_Example_1
Line 1,513: Line 1,513:


$output | Format-List
$output | Format-List
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,521: Line 1,521:


===Version 3.0 Or Less===
===Version 3.0 Or Less===
<syntaxhighlight lang="powershell">
<lang PowerShell>
$file = @'
$file = @'
>Rosetta_Example_1
>Rosetta_Example_1
Line 1,543: Line 1,543:


$output | Format-List
$output | Format-List
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,551: Line 1,551:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>EnableExplicit
<syntaxhighlight lang="purebasic">EnableExplicit
Define Hdl_File.i,
Define Hdl_File.i,
Frm_File.i,
Frm_File.i,
Line 1,579: Line 1,579:
CloseFile(Hdl_File)
CloseFile(Hdl_File)
Input()
Input()
EndIf</lang>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 1,589: Line 1,589:
and I use a generator expression yielding key, value pairs
and I use a generator expression yielding key, value pairs
as soon as they are read, keeping the minimum in memory.
as soon as they are read, keeping the minimum in memory.
<lang python>import io
<syntaxhighlight lang="python">import io


FASTA='''\
FASTA='''\
Line 1,613: Line 1,613:
yield key, val
yield key, val


print('\n'.join('%s: %s' % keyval for keyval in fasta_parse(infile)))</lang>
print('\n'.join('%s: %s' % keyval for keyval in fasta_parse(infile)))</syntaxhighlight>


{{out}}
{{out}}
Line 1,620: Line 1,620:


=={{header|R}}==
=={{header|R}}==
<lang rsplus>
<syntaxhighlight lang="rsplus">
library("seqinr")
library("seqinr")


Line 1,633: Line 1,633:
cat(attr(aline, 'Annot'), ":", aline, "\n")
cat(attr(aline, 'Annot'), ":", aline, "\n")
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,641: Line 1,641:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(let loop ([m #t])
(let loop ([m #t])
Line 1,651: Line 1,651:
(current-output-port)))))
(current-output-port)))))
(newline)
(newline)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>grammar FASTA {
<syntaxhighlight lang="raku" line>grammar FASTA {


rule TOP { <entry>+ }
rule TOP { <entry>+ }
Line 1,675: Line 1,675:
for $/<entry>[] {
for $/<entry>[] {
say ~.<title>, " : ", .<sequence>.made;
say ~.<title>, " : ", .<sequence>.made;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Rosetta_Example_1 : THERECANBENOSPACE
<pre>Rosetta_Example_1 : THERECANBENOSPACE
Line 1,684: Line 1,684:
===version 1===
===version 1===
This REXX version correctly processes the examples shown.
This REXX version correctly processes the examples shown.
<lang rexx>/*REXX program reads a (bio-informational) FASTA file and displays the contents. */
<syntaxhighlight lang="rexx">/*REXX program reads a (bio-informational) FASTA file and displays the contents. */
parse arg iFID . /*iFID: the input file to be read. */
parse arg iFID . /*iFID: the input file to be read. */
if iFID=='' then iFID='FASTA.IN' /*Not specified? Then use the default.*/
if iFID=='' then iFID='FASTA.IN' /*Not specified? Then use the default.*/
Line 1,699: Line 1,699:
else $=$ || x
else $=$ || x
end /*j*/ /* [↓] show output of last file used. */
end /*j*/ /* [↓] show output of last file used. */
if $\=='' then say name':' $ /*stick a fork in it, we're all done. */</lang>
if $\=='' then say name':' $ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input filename:}}
{{out|output|text=&nbsp; when using the default input filename:}}
<pre>
<pre>
Line 1,712: Line 1,712:
::* &nbsp; sequences that contain blanks, tabs, and other whitespace
::* &nbsp; sequences that contain blanks, tabs, and other whitespace
::* &nbsp; sequence names that are identified with a semicolon &nbsp; [''';''']
::* &nbsp; sequence names that are identified with a semicolon &nbsp; [''';''']
<lang rexx>/*REXX program reads a (bio-informational) FASTA file and displays the contents. */
<syntaxhighlight lang="rexx">/*REXX program reads a (bio-informational) FASTA file and displays the contents. */
parse arg iFID . /*iFID: the input file to be read. */
parse arg iFID . /*iFID: the input file to be read. */
if iFID=='' then iFID='FASTA2.IN' /*Not specified? Then use the default.*/
if iFID=='' then iFID='FASTA2.IN' /*Not specified? Then use the default.*/
Line 1,733: Line 1,733:
else $=space($ || translate(x, , '*'), 0)
else $=space($ || translate(x, , '*'), 0)
end /*j*/ /* [↓] show output of last file used. */
end /*j*/ /* [↓] show output of last file used. */
if $\=='' then say name':' $ /*stick a fork in it, we're all done. */</lang>
if $\=='' then say name':' $ /*stick a fork in it, we're all done. */</syntaxhighlight>
<pre>
<pre>
'''input:''' &nbsp; The &nbsp; '''FASTA2.IN''' &nbsp; file is shown below:
'''input:''' &nbsp; The &nbsp; '''FASTA2.IN''' &nbsp; file is shown below:
Line 1,766: Line 1,766:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : FAST format
# Project : FAST format


Line 1,789: Line 1,789:
i = i + 1
i = i + 1
end
end
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,797: Line 1,797:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def fasta_format(strings)
<syntaxhighlight lang="ruby">def fasta_format(strings)
out, text = [], ""
out, text = [], ""
strings.split("\n").each do |line|
strings.split("\n").each do |line|
Line 1,819: Line 1,819:
EOS
EOS


puts fasta_format(data)</lang>
puts fasta_format(data)</syntaxhighlight>


{{out}}
{{out}}
Line 1,828: Line 1,828:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>a$ = ">Rosetta_Example_1
<syntaxhighlight lang="runbasic">a$ = ">Rosetta_Example_1
THERECANBENOSPACE
THERECANBENOSPACE
>Rosetta_Example_2
>Rosetta_Example_2
Line 1,845: Line 1,845:
end if
end if
i = i + 1
i = i + 1
wend</lang>
wend</syntaxhighlight>
{{out}}
{{out}}
<pre>>Rosetta_Example_1: THERECANBENOSPACE
<pre>>Rosetta_Example_1: THERECANBENOSPACE
Line 1,854: Line 1,854:
This example is implemented using an [https://doc.rust-lang.org/book/iterators.html iterator] to reduce memory requirements and encourage code reuse.
This example is implemented using an [https://doc.rust-lang.org/book/iterators.html iterator] to reduce memory requirements and encourage code reuse.


<lang rust>
<syntaxhighlight lang="rust">
use std::env;
use std::env;
use std::io::{BufReader, Lines};
use std::io::{BufReader, Lines};
Line 1,918: Line 1,918:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 1,924: Line 1,924:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>import java.io.File
<syntaxhighlight lang="scala">import java.io.File
import java.util.Scanner
import java.util.Scanner


Line 1,942: Line 1,942:


println("~~~+~~~")
println("~~~+~~~")
}</lang>
}</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
<lang scheme>(import (scheme base)
<syntaxhighlight lang="scheme">(import (scheme base)
(scheme file)
(scheme file)
(scheme write))
(scheme write))
Line 1,960: Line 1,960:
(display (string-copy line 1)) (display ": "))
(display (string-copy line 1)) (display ": "))
(else ; display the string directly
(else ; display the string directly
(display line))))))</lang>
(display line))))))</syntaxhighlight>
{{out}}
{{out}}
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 1,966: Line 1,966:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: main is func
const proc: main is func
Line 1,992: Line 1,992:
end if;
end if;
writeln;
writeln;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 2,002: Line 2,002:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>func fasta_format(strings) {
<syntaxhighlight lang="ruby">func fasta_format(strings) {
var out = []
var out = []
var text = ''
var text = ''
Line 2,026: Line 2,026:
THERECANBESEVERAL
THERECANBESEVERAL
LINESBUTTHEYALLMUST
LINESBUTTHEYALLMUST
BECONCATENATED</lang>
BECONCATENATED</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,034: Line 2,034:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc fastaReader {filename} {
<syntaxhighlight lang="tcl">proc fastaReader {filename} {
set f [open $filename]
set f [open $filename]
set sep ""
set sep ""
Line 2,049: Line 2,049:
}
}


fastaReader ./rosettacode.fas</lang>
fastaReader ./rosettacode.fas</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,058: Line 2,058:
=={{header|TMG}}==
=={{header|TMG}}==
Unix TMG: <!-- C port of TMG processes 1.04 GB FASTA file in 38 seconds on a generic laptop -->
Unix TMG: <!-- C port of TMG processes 1.04 GB FASTA file in 38 seconds on a generic laptop -->
<lang UnixTMG>prog: ignore(spaces)
<syntaxhighlight lang="unixtmg">prog: ignore(spaces)
loop: parse(line)\loop parse(( = {*} ));
loop: parse(line)\loop parse(( = {*} ));
line: ( name | * = {} | seqns );
line: ( name | * = {} | seqns );
Line 2,071: Line 2,071:
spaces: << >>;
spaces: << >>;


f: 1;</lang>
f: 1;</syntaxhighlight>


=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
<lang>If Cmd (0) < 2 Then Print "Usage: fasta <fasta file>" : End
<syntaxhighlight lang="text">If Cmd (0) < 2 Then Print "Usage: fasta <fasta file>" : End
If Set(a, Open (Cmd(2), "r")) < 0 Then Print "Cannot open \q";Cmd(2);"\q" : End
If Set(a, Open (Cmd(2), "r")) < 0 Then Print "Cannot open \q";Cmd(2);"\q" : End


Line 2,105: Line 2,105:
Loop ' if not add the line to current string
Loop ' if not add the line to current string


Return (b@) ' return the string</lang>
Return (b@) ' return the string</syntaxhighlight>
{{out}}
{{out}}
<pre>Rosetta_Example_1: THERECANBENOSPACE
<pre>Rosetta_Example_1: THERECANBENOSPACE
Line 2,114: Line 2,114:
{{trans|Kotlin}}
{{trans|Kotlin}}
More or less.
More or less.
<lang ecmascript>import "io" for File
<syntaxhighlight lang="ecmascript">import "io" for File


var checkNoSpaces = Fn.new { |s| !s.contains(" ") && !s.contains("\t") }
var checkNoSpaces = Fn.new { |s| !s.contains(" ") && !s.contains("\t") }
Line 2,153: Line 2,153:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,162: Line 2,162:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>proc Echo; \Echo line of characters from file to screen
<syntaxhighlight lang="xpl0">proc Echo; \Echo line of characters from file to screen
int Ch;
int Ch;
def LF=$0A, EOF=$1A;
def LF=$0A, EOF=$1A;
Line 2,184: Line 2,184:
Echo;
Echo;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,194: Line 2,194:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn fasta(data){ // a lazy cruise through a FASTA file
<syntaxhighlight lang="zkl">fcn fasta(data){ // a lazy cruise through a FASTA file
fcn(w){ // one string at a time, -->False garbage at front of file
fcn(w){ // one string at a time, -->False garbage at front of file
line:=w.next().strip();
line:=w.next().strip();
Line 2,201: Line 2,201:
})
})
}.fp(data.walker()) : Utils.Helpers.wap(_);
}.fp(data.walker()) : Utils.Helpers.wap(_);
}</lang>
}</syntaxhighlight>
*This assumes that white space at front or end of string is extraneous (excepting ">" lines).
*This assumes that white space at front or end of string is extraneous (excepting ">" lines).
*Lazy, works for objects that support iterating over lines (ie most).
*Lazy, works for objects that support iterating over lines (ie most).
*The fasta function returns an iterator that wraps a function taking an iterator. Uh, yeah. An initial iterator (Walker) is used to get lines, hold state and do push back when read the start of the next string. The function sucks up one string (using the iterator). The wrapping iterator (wap) traps the exception when the function waltzes off the end of the data and provides API for foreach (etc).
*The fasta function returns an iterator that wraps a function taking an iterator. Uh, yeah. An initial iterator (Walker) is used to get lines, hold state and do push back when read the start of the next string. The function sucks up one string (using the iterator). The wrapping iterator (wap) traps the exception when the function waltzes off the end of the data and provides API for foreach (etc).
FASTA file:
FASTA file:
<lang zkl>foreach l in (fasta(File("fasta.txt"))) { println(l) }</lang>
<syntaxhighlight lang="zkl">foreach l in (fasta(File("fasta.txt"))) { println(l) }</syntaxhighlight>
FASTA data blob:
FASTA data blob:
<lang zkl>data:=Data(0,String,
<syntaxhighlight lang="zkl">data:=Data(0,String,
">Rosetta_Example_1\nTHERECANBENOSPACE\n"
">Rosetta_Example_1\nTHERECANBENOSPACE\n"
">Rosetta_Example_2\nTHERECANBESEVERAL\nLINESBUTTHEYALLMUST\n"
">Rosetta_Example_2\nTHERECANBESEVERAL\nLINESBUTTHEYALLMUST\n"
"BECONCATENATED");
"BECONCATENATED");
foreach l in (fasta(data)) { println(l) }</lang>
foreach l in (fasta(data)) { println(l) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>