FASTA format: Difference between revisions

→‎version 2: rewritten
(→‎version 2: rewritten)
 
(11 intermediate revisions by 7 users not shown)
Line 209:
<pre>>Rosetta_Example_1: THERECANBENOSPACE
>Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED</pre>
 
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<syntaxhighlight lang="algol68">
BEGIN # read FASTA format data from standard input and write the results to #
# standard output - only the ">" line start is handled #
 
BOOL at eof := FALSE;
on logical file end( stand in, ( REF FILE f )BOOL: at eof := TRUE );
 
WHILE STRING line;
read( ( line, newline ) );
NOT at eof
DO
IF line /= "" THEN # non-empty line #
INT start := LWB line;
BOOL is heading = line[ start ] = ">"; # check for heading line #
IF is heading THEN
print( ( newline ) );
start +:= 1
FI;
print( ( line[ start : ] ) );
IF is heading THEN print( ( ": " ) ) FI
FI
OD
END
</syntaxhighlight>
{{out}}
<pre>
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED
</pre>
 
=={{header|ALGOL W}}==
Line 672 ⟶ 704:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/FASTA_format#Pascal Pascal].
 
=={{header|EasyLang}}==
<syntaxhighlight>
repeat
s$ = input
until s$ = ""
if substr s$ 1 1 = ">"
if stat = 1
print ""
.
stat = 1
print s$
else
write s$
.
.
input_data
>Rosetta_Example_1
THERECANBENOSPACE
>Rosetta_Example_2
THERECANBESEVERAL
LINESBUTTHEYALLMUST
BECONCATENATED
 
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 687 ⟶ 744:
THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting io kernel sequences ;
Line 1,349 ⟶ 1,407:
 
fasta(input)
</syntaxhighlight>
{{out}}
<pre>
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED
</pre>
=={{header|Oberon}}==
Works with A2 Oberon.
 
<syntaxhighlight lang="Oberon">
MODULE Fasta;
 
IMPORT Files, Streams, Strings, Commands;
 
PROCEDURE PrintOn*(filename: ARRAY OF CHAR; wr: Streams.Writer);
VAR
rd: Files.Reader;
f: Files.File;
line: ARRAY 1024 OF CHAR;
res: BOOLEAN;
BEGIN
f := Files.Old(filename);
ASSERT(f # NIL);
NEW(rd,f,0);
res := rd.GetString(line);
WHILE rd.res # Streams.EOF DO
IF line[0] = '>' THEN
wr.Ln;
wr.String(Strings.Substring2(1,line)^);
wr.String(": ")
ELSE
wr.String(line)
END;
res := rd.GetString(line)
END
END PrintOn;
 
PROCEDURE Do*;
VAR
ctx: Commands.Context;
filename: ARRAY 256 OF CHAR;
res: BOOLEAN
BEGIN
ctx := Commands.GetContext();
res := ctx.arg.GetString(filename);
PrintOn(filename,ctx.out)
END Do;
 
END Fasta.
</syntaxhighlight>
{{out}}
Line 1,557 ⟶ 1,664:
(prinl) ) ) )
(fasta "fasta.dat")</syntaxhighlight>
{{out}}
<pre>
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED
</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
Reads the data from the file named on the command line, e.g., if the program is stored in D:FASTA.COM and the data in D:FSTAIN.TXT, the following could be used: <code>D:FASTA D:FASTAIN.TXT</code>.<br>
Restarts CP/M when the program finishes.
<syntaxhighlight lang="plm">
100H: /* DISPLAY THE CONTENTS OF A FASTA FORMT FILE */
 
DECLARE FALSE LITERALLY '0', TRUE LITERALLY '0FFH';
DECLARE NL$CHAR LITERALLY '0AH'; /* NEWLINE: CHAR 10 */
DECLARE CR$CHAR LITERALLY '0DH'; /* CARRIAGE RETURN, CHAR 13 */
DECLARE EOF$CHAR LITERALLY '26'; /* EOF: CTRL-Z */
/* CP/M BDOS SYSTEM CALL, RETURNS A VALUE */
BDOS: PROCEDURE( FN, ARG )BYTE; DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
/* CP/M BDOS SYSTEM CALL, NO RETURN VALUE */
BDOS$P: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
EXIT: PROCEDURE; CALL BDOS$P( 0, 0 ); END; /* CP/M SYSTEM RESET */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS$P( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS$P( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, NL$CHAR, '$' ) ); END;
FL$EXISTS: PROCEDURE( FCB )BYTE; /* RETURNS TRUE IF THE FILE NAMED IN THE */
DECLARE FCB ADDRESS; /* FCB EXISTS */
RETURN ( BDOS( 17, FCB ) < 4 );
END FL$EXISTS ;
FL$OPEN: PROCEDURE( FCB )BYTE; /* OPEN THE FILE WITH THE SPECIFIED FCB */
DECLARE FCB ADDRESS;
RETURN ( BDOS( 15, FCB ) < 4 );
END FL$OPEN;
FL$READ: PROCEDURE( FCB )BYTE; /* READ THE NEXT RECORD FROM FCB */
DECLARE FCB ADDRESS;
RETURN ( BDOS( 20, FCB ) = 0 );
END FL$READ;
FL$CLOSE: PROCEDURE( FCB )BYTE; /* CLOSE THE FILE WITH THE SPECIFIED FCB */
DECLARE FCB ADDRESS;
RETURN ( BDOS( 16, FCB ) < 4 );
END FL$CLOSE;
 
/* I/O USES FILE CONTROL BLOCKS CONTAINING THE FILE-NAME, POSITION, ETC. */
/* WHEN THE PROGRAM IS RUN, THE CCP WILL FIRST PARSE THE COMMAND LINE AND */
/* PUT THE FIRST PARAMETER IN FCB1, THE SECOND PARAMETER IN FCB2 */
/* BUT FCB2 OVERLAYS THE END OF FCB1 AND THE DMA BUFFER OVERLAYS THE END */
/* OF FCB2 */
 
DECLARE FCB$SIZE LITERALLY '36'; /* SIZE OF A FCB */
DECLARE FCB1 LITERALLY '5CH'; /* ADDRESS OF FIRST FCB */
DECLARE FCB2 LITERALLY '6CH'; /* ADDRESS OF SECOND FCB */
DECLARE DMA$BUFFER LITERALLY '80H'; /* DEFAULT DMA BUFFER ADDRESS */
DECLARE DMA$SIZE LITERALLY '128'; /* SIZE OF THE DMA BUFFER */
 
DECLARE F$PTR ADDRESS, F$CHAR BASED F$PTR BYTE;
 
/* CLEAR THE PARTS OF FCB1 OVERLAYED BY FCB2 */
DO F$PTR = FCB1 + 12 TO FCB1 + ( FCB$SIZE - 1 );
F$CHAR = 0;
END;
 
/* SHOW THE FASTA DATA, IF THE FILE EXISTS */
IF NOT FL$EXISTS( FCB1 ) THEN DO; /* THE FILE DOES NOT EXIST */
CALL PR$STRING( .'FILE NOT FOUND$' );CALL PR$NL;
END;
ELSE IF NOT FL$OPEN( FCB1 ) THEN DO; /* UNABLE TO OPEN THE FILE */
CALL PR$STRING( .'UNABLE TO OPEN THE FILE$' );CALL PR$NL;
END;
ELSE DO; /* FILE EXISTS AND OPENED OK - ATTEMPT TO SHOW THE DATA */
DECLARE ( BOL, GOT$RCD, IS$HEADING ) BYTE, DMA$END ADDRESS;
DMA$END = DMA$BUFFER + ( DMA$SIZE - 1 );
GOT$RCD = FL$READ( FCB1 ); /* GET THE FIRST RECORD */
F$PTR = DMA$BUFFER;
BOL = TRUE;
IS$HEADING = FALSE;
DO WHILE GOT$RCD;
IF F$PTR > DMA$END THEN DO; /* END OF BUFFER */
GOT$RCD = FL$READ( FCB1 ); /* GET THE NEXT RECORDD */
F$PTR = DMA$BUFFER;
END;
ELSE IF F$CHAR = NL$CHAR THEN DO; /* END OF LINE */
IF IS$HEADING THEN DO;
CALL PR$STRING( .': $' );
IS$HEADING = FALSE;
END;
BOL = TRUE;
END;
ELSE IF F$CHAR = CR$CHAR THEN DO; END; /* IGNORE CARRIAGE RETURN */
ELSE IF F$CHAR = EOF$CHAR THEN GOT$RCD = FALSE; /* END OF FILE */
ELSE DO; /* HAVE ANOTHER CHARACTER */
IF NOT BOL THEN CALL PR$CHAR( F$CHAR ); /* NOT FIRST CHARACTER */
ELSE DO; /* FIRST CHARACTER - CHECK FOR A HEADING LINE */
BOL = FALSE;
IF IS$HEADING := F$CHAR = '>' THEN CALL PR$NL;
ELSE CALL PR$CHAR( F$CHAR );
END;
END;
F$PTR = F$PTR + 1;
END;
/* CLOSE THE FILE */
IF NOT FL$CLOSE( FCB1 ) THEN DO;
CALL PR$STRING( .'UNABLE TO CLOSE THE FILE$' ); CALL PR$NL;
END;
END;
 
CALL EXIT;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
Line 1,759 ⟶ 1,975:
===version 1===
This REXX version correctly processes the examples shown.
<syntaxhighlight lang="rexx">/*REXX program reads a (bio-informational) FASTA file and displays the contents. */
parseParse argArg iFIDifid . /* iFID: the input file to be read. */
If ifid=='' Then
if iFID=='' then iFID='FASTA.IN' /*Not specified? Then use the default.*/
name= ifid='FASTA.IN' /* Not specified? Then use /*the namedefault of an output file (so far). */
$name='' /* the name of an output file (so far) /*the value of the output file's stuff.*/
d='' do while lines(iFID)\==0 /*process the value FASTAof the output file's contents. */
Do While lines(ifid)\==0 x=strip( linein(iFID), 'T') /* process the FASTA file /*readcontents a line (a record) from the file,*/
x=strip(linein(ifid),'T') /* read a line (a record) from the input */
/*───────── and strip trailing blanks. */
/* and strip trailing blanks */
if left(x, 1)=='>' then do
If left(x,1)=='>' Then Do /* a new file id if $\=='' then say name':' $*/
Call out /* show output name=substr(x, and data 2)*/
name=substr(x,2) /* and get the new (or first) $=output name */
d='' end /* start with empty contents */
End
else $=$ || x
Else end /*j*/ /* a line with data /* [↓] show output of last file used. */
if $\= d=''d||x then say name':' $ /*stick aappend it to output fork in it, we're all done. */</syntaxhighlight>
End
Call out /* show output of last file used. */
Exit
 
out:
If d\=='' Then /* if there ara data */
Say name':' d /* show output name and data */
Return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input filename:}}
<pre>
Line 1,787 ⟶ 2,011:
::* &nbsp; sequences that contain blanks, tabs, and other whitespace
::* &nbsp; sequence names that are identified with a semicolon &nbsp; [''';''']
<syntaxhighlight lang="rexx">/*REXX program reads a (bio-informational) FASTA file and displays the contents. */
parseParse argArg iFID . /*iFID: the input file to be read. */
ifIf iFID=='' thenThen iFID='FASTA2.IN' /*Not specified? Then use the default.*/
name= '' /*the name of an output file (so far). */
data=''
$= /*the value of the output file's stuff.*/
do while lines(iFID)\==0 /*process the value FASTAof the output file's contentsstuff. */
Do While x=strip( lineinlines(iFID),\==0 'T') /*readprocess athe line (aFASTA record) fromfile contents. the file,*/
x=strip(linein(iFID),'T') /*read a line (a record) from the file,*/
/*───────── and strip trailing blanks. */
if x=='' then iterate /*If the line is all blank, ignore it /*--------- and strip trailing blanks. */
Select
if left(x, 1)==';' then do
When x=='' Then /* ifIf name==''the thenline name=substr(xis all blank,2) */
Nop say x /* ignore it. */
When left(x,1)==';' Then Do
iterate
If name=='' Then name=substr(x,2)
end
if left(Say x, 1)=='>' then do
End
if $\=='' then say name':' $
When left(x,1)=='>' Then Do
name=substr(x, 2)
If data\=='' Then
$=
Say name':' enddata
name=substr(x,2)
else $=space($ || translate(x, , '*'), 0)
data=''
end /*j*/ /* [↓] show output of last file used. */
End
if $\=='' then say name':' $ /*stick a fork in it, we're all done. */</syntaxhighlight>
Otherwise
data=space(data||translate(x, ,'*'),0)
End
End
If data\=='' Then
Say name':' data /* [?] show output of last file used. */
</syntaxhighlight>
<pre>
'''input:''' &nbsp; The &nbsp; '''FASTA2.IN''' &nbsp; file is shown below:
Line 2,102 ⟶ 2,333:
LINESBUTTHEYALLMUST
BECONCATENATED</syntaxhighlight>
{{out}}
<pre>
Rosetta_Example_1: THERECANBENOSPACE
Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED
</pre>
 
=={{header|Smalltalk}}==
Works with Pharo Smalltalk
<syntaxhighlight lang="smalltalk">
FileLocator home / aFilename readStreamDo: [ :stream |
[ stream atEnd ] whileFalse: [
| line |
((line := stream nextLine) beginsWith: '>')
ifTrue: [
Transcript
cr;
show: (line copyFrom: 2 to: line size);
show: ': ' ]
ifFalse: [ Transcript show: line ] ] ]
</syntaxhighlight>
{{out}}
<pre>
Line 2,170 ⟶ 2,421:
Local (4)
 
b@ := Dup("") ' start with an empty string
 
Do
Line 2,186 ⟶ 2,437:
 
0 OK, 0:431 </pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Vlang">
const data = (
">Rosetta_Example_1
THERECANBENOSPACE
>Rosetta_Example_2
THERECANBESEVERAL
LINESBUTTHEYALLMUST
BECONCATENATED"
)
 
fn main() {
mut i := 0
for i <= data.len {
if data.substr_ni(i, i + 17) == ">Rosetta_Example_" {
print("\n" + data.substr_ni(i, i + 18) + ": ")
i = i + 17
}
else {
if data.substr_ni(i, i + 1) > "\x20" {print(data[i].ascii_str())}
}
i++
}
}
</syntaxhighlight>
 
{{out}}
<pre>
>Rosetta_Example_1: THERECANBENOSPACE
>Rosetta_Example_2: THERECANBESEVERALLINESBUTTHEYALLMUSTBECONCATENATED
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
More or less.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
var checkNoSpaces = Fn.new { |s| !s.contains(" ") && !s.contains("\t") }
2,289

edits