Skip to content

Commit fe7fc0c

Browse files
committed
Add avg, improve fasta scripts
1 parent 56a79c5 commit fe7fc0c

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

Diff for: avg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
# Return the total of a file or stream with one number per line
3+
awk '{t+=$1} END {print t/NR}'

Diff for: fasta_extract_min_length.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def fasta_iterator(input_file):
4949
yield Fasta(name, "".join(sequence))
5050

5151
name = line[1:]
52-
sequence = ""
52+
sequence = []
5353
begun = True
5454

5555
else:
56-
sequence += line
56+
sequence.append(line)
5757

5858
if name != "":
5959
yield Fasta(name, "".join(sequence))
@@ -69,8 +69,7 @@ def fasta_iterator(input_file):
6969

7070
fasta_sequences = fasta_iterator(input_file)
7171

72-
with open(output_file, "wt") as outfile:
72+
with myopen(output_file, "wt") as outfile:
7373
for seq in fasta_sequences:
7474
if len(seq.sequence) >= min_length:
7575
seq.write_to_file(outfile)
76-

Diff for: fasta_n50.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,30 @@ def myopen(_file, mode="rt"):
3030
else:
3131
return open(_file, mode=mode)
3232

33-
def fasta_iterator(genome_file):
34-
"""Takes a fasta file genome_file and returns a fasta iterator
33+
def fasta_iterator(input_file):
34+
"""Takes a fasta file input_file and returns a fasta iterator
3535
"""
36-
with myopen(genome_file) as f:
37-
sequence = ""
36+
with myopen(input_file) as f:
37+
sequence = []
3838
name = ""
3939
begun = False
40+
4041
for line in f:
4142
line = line.strip()
43+
4244
if line.startswith(">"):
4345
if begun:
44-
yield Fasta(name, sequence)
45-
name = line.replace(">", "")
46+
yield Fasta(name, "".join(sequence))
47+
48+
name = line[1:]
4649
sequence = ""
4750
begun = True
51+
4852
else:
4953
sequence += line
5054

5155
if name != "":
52-
yield Fasta(name, sequence)
56+
yield Fasta(name, "".join(sequence))
5357

5458
if __name__ == '__main__':
5559
# Prevent broken pipe error

0 commit comments

Comments
 (0)