File size: 1,534 Bytes
27124e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// One parser used across all tool pages. Handles:
//   - FASTA (headers starting with '>')
//   - plain newline-separated sequences
//   - comma-separated sequence lists
// Returns a normalized list of { id, seq }.

export interface ParsedSequence {
  id: string
  seq: string
}

const AA_RE = /[^A-Za-z]/g

function cleanSeq(raw: string): string {
  return raw.replace(AA_RE, '').toUpperCase()
}

export function parseSequences(input: string): ParsedSequence[] {
  const text = input.trim()
  if (!text) return []

  // FASTA path: contains at least one '>' header.
  if (text.includes('>')) {
    const out: ParsedSequence[] = []
    let current: ParsedSequence | null = null
    for (const line of text.split(/\r?\n/)) {
      const trimmed = line.trim()
      if (!trimmed) continue
      if (trimmed.startsWith('>')) {
        if (current) out.push(current)
        const id = trimmed.slice(1).trim().split(/\s+/)[0] || `seq_${out.length + 1}`
        current = { id, seq: '' }
      } else if (current) {
        current.seq += cleanSeq(trimmed)
      } else {
        // sequence data before any header
        current = { id: `seq_1`, seq: cleanSeq(trimmed) }
      }
    }
    if (current) out.push(current)
    return out.filter((s) => s.seq.length > 0)
  }

  // Non-FASTA: split on commas and/or newlines.
  const tokens = text
    .split(/[,\n\r]+/)
    .map((t) => t.trim())
    .filter(Boolean)

  return tokens
    .map((tok, i) => ({ id: `seq_${i + 1}`, seq: cleanSeq(tok) }))
    .filter((s) => s.seq.length > 0)
}