Skip to content

Commit fb95edb

Browse files
committed
Fasta support
1 parent f72d59c commit fb95edb

5 files changed

Lines changed: 135 additions & 1 deletion

File tree

autoload/sj/fasta.vim

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function! sj#fasta#SplitSequence() abort
2+
" Check we're on a header line:
3+
if getline('.') !~ '^>'
4+
return 0
5+
endif
6+
let header_lineno = line('.')
7+
8+
" Check that the next line is a sequence
9+
if getline(header_lineno + 1) =~ '^>'
10+
return 0
11+
endif
12+
let sequence_lineno = header_lineno + 1
13+
let sequence_line = getline(sequence_lineno)
14+
15+
" Check that the next next line is a header
16+
if getline(header_lineno + 2) !~ '^>'
17+
return 0
18+
endif
19+
20+
let width = sj#settings#Read('fasta_textwidth')
21+
if width <= 0
22+
let width = &textwidth
23+
endif
24+
if width <= 0
25+
let width = 80
26+
endif
27+
28+
let split_sequence_line = substitute(sequence_line, '.\{'..width..'}', '\0\n', 'g')
29+
call sj#ReplaceLines(sequence_lineno, sequence_lineno, split_sequence_line)
30+
return 1
31+
endfunction
32+
33+
function! sj#fasta#JoinSequence() abort
34+
" Check we're on a header line:
35+
if getline('.') !~ '^>'
36+
return 0
37+
endif
38+
let header_lineno = line('.')
39+
let start_lineno = header_lineno + 1
40+
41+
" Find next header line, if any:
42+
let next_header_lineno = search('^>', 'W')
43+
if next_header_lineno > 0
44+
let end_lineno = next_header_lineno - 1
45+
else
46+
let end_lineno = line('$')
47+
endif
48+
49+
if end_lineno - start_lineno <= 0
50+
" No sequence lines to join
51+
return 0
52+
endif
53+
54+
call sj#Keeppatterns(start_lineno..','..(end_lineno - 1)..'s/\n//g')
55+
return 1
56+
endfunction

doc/splitjoin.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ CONTENTS *splitjoin* *splitjoin-content
1414
Elixir.................................: |splitjoin-elixir|
1515
Elm....................................: |splitjoin-elm|
1616
Eruby..................................: |splitjoin-eruby|
17+
FASTA..................................: |splitjoin-fasta|
1718
Go.....................................: |splitjoin-go|
1819
HAML...................................: |splitjoin-haml|
20+
HTML...................................: |splitjoin-html|
1921
Handlebars.............................: |splitjoin-handlebars|
2022
Hare...................................: |splitjoin-hare|
21-
HTML...................................: |splitjoin-html|
2223
Java...................................: |splitjoin-java|
2324
Javascript.............................: |splitjoin-javascript| |splitjoin-json|
2425
JSX/TSX................................: |splitjoin-jsx| |splitjoin-tsx|
@@ -513,6 +514,20 @@ Option hashes ~
513514
} %>
514515
<
515516

517+
==============================================================================
518+
FASTA *splitjoin-fasta*
519+
520+
Sequences ~
521+
>
522+
> E.coli_K12 Cysteine--tRNA ligase
523+
MLKIFNTLTRQKEEFKPIHAGEVGMYVCGITVYDLCHIGHGRTFVAFDVVARYLRFLGYKLKYVRNITDIDDK...
524+
525+
> E.coli_K12 Cysteine--tRNA ligase
526+
MLKIFNTLTRQKEEFKPIHAGEVGMYVCGITVYDLCHIGHGRTFVAFDVVARYLRFLGYK
527+
LKYVRNITDIDDKIIKRANENGESFVAMVDRMIAEMHKDFDALNILRPDMEPRATHHIAE
528+
...
529+
<
530+
516531
==============================================================================
517532
GO *splitjoin-go*
518533

@@ -2133,6 +2148,18 @@ part. However, if it's broken at a `.`, it could cause parsing problems. Not
21332148
leaving a space might be safer, set to 0 to do that.
21342149

21352150

2151+
*splitjoin_fasta_textwidth*
2152+
>
2153+
let g:splitjoin_fasta_textwidth = 100
2154+
<
2155+
Default value: 0
2156+
2157+
Fasta files get split into lines based on textwidth. If this variable is set
2158+
to anything greater than 0, that will be considered the desired textwidth.
2159+
Otherwise, the built-in 'textwidth' will be used. If that's set to 0, the
2160+
plugin will arbitrarily use 100 as the threshold.
2161+
2162+
21362163
==============================================================================
21372164
INTERNALS *splitjoin-internals*
21382165

ftplugin/fasta/splitjoin.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let b:splitjoin_split_callbacks = [
2+
\ 'sj#fasta#SplitSequence',
3+
\ ]
4+
5+
let b:splitjoin_join_callbacks = [
6+
\ 'sj#fasta#JoinSequence',
7+
\ ]

plugin/splitjoin.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ let g:splitjoin_default_settings = {
3838
\ 'java_argument_split_first_newline': 0,
3939
\ 'java_argument_split_last_newline': 0,
4040
\ 'vim_split_whitespace_after_backslash': 1,
41+
\ 'fasta_textwidth': 0,
4142
\ }
4243

4344
if !exists('g:splitjoin_join_mapping')

spec/plugin/fasta_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'spec_helper'
2+
3+
describe "fasta" do
4+
let(:filename) { 'test.fasta' }
5+
6+
specify "sequences" do
7+
set_file_contents <<~EOF
8+
>Something else
9+
ELVIS-LIVES
10+
> E.coli_K12 Cysteine--tRNA ligase
11+
MLKIFNTLTRQKEEFKPIHAGEVGMYVCGITVYDLCHIGHGRTFVAFDVVARYLRFLGYKLKYVRNITDIDDK...
12+
> Another one
13+
LIVES-ELVIS*
14+
EOF
15+
16+
vim.set :filetype, 'fasta'
17+
vim.set :textwidth, 50
18+
19+
vim.search '> E\.coli'
20+
split
21+
22+
assert_file_contents <<~EOF
23+
>Something else
24+
ELVIS-LIVES
25+
> E.coli_K12 Cysteine--tRNA ligase
26+
MLKIFNTLTRQKEEFKPIHAGEVGMYVCGITVYDLCHIGHGRTFVAFDVV
27+
ARYLRFLGYKLKYVRNITDIDDK...
28+
> Another one
29+
LIVES-ELVIS*
30+
EOF
31+
32+
join
33+
34+
assert_file_contents <<~EOF
35+
>Something else
36+
ELVIS-LIVES
37+
> E.coli_K12 Cysteine--tRNA ligase
38+
MLKIFNTLTRQKEEFKPIHAGEVGMYVCGITVYDLCHIGHGRTFVAFDVVARYLRFLGYKLKYVRNITDIDDK...
39+
> Another one
40+
LIVES-ELVIS*
41+
EOF
42+
end
43+
end

0 commit comments

Comments
 (0)