1 import jalview.datamodel.AlignmentI
2 import jalview.datamodel.SequenceI
3 import jalview.datamodel.Sequence
4 import jalview.io.FileFormatI
5 import jalview.io.FileFormats
6 import jalview.io.AlignmentFileI
7 import jalview.io.FileParse
8 import jalview.io.AlignFile
11 * Example script that registers a new alignment file format
12 * consisting of lines like
14 * !S=<sequence string>
18 * A parser class to read or write the format
20 class MyParser extends AlignFile
23 * Constructor for reading a file; the superclass
24 * constructor will call the parse() method
26 MyParser(FileParse src)
32 * Constructor for writing out an alignment
34 MyParser(AlignmentI al)
39 * Parse a formatted data file (with no error checking!)
45 while ((line = nextLine()) != null)
47 if (line.startsWith('!I='))
49 int pos = line.indexOf('/')
50 id = line.substring(3, pos == -1 ? line.length() : pos)
51 } else if (line.startsWith('!S='))
53 String seq = line.substring(3)
54 addSequence(new Sequence(id, seq))
60 * Print the formatted sequences
61 * (addSuffix always defaults to true as no user preference for it)
63 String print(SequenceI[] seqs, boolean addSuffix)
65 StringBuilder sb = new StringBuilder()
66 for (SequenceI seq : seqs)
68 sb.append('!I=').append(seq.getDisplayId(addSuffix)).append('\n')
69 sb.append('!S=').append(seq.getSequenceAsString()).append('\n')
76 * A closure that defines the 'Groovy example' file format,
77 * delegating to MyParser for reading and writing
81 getName: { -> 'Groovy example' },
83 toString: { -> getName() },
85 getExtensions: { -> 'grv' },
87 getReader: { FileParse source -> new MyParser(source) },
89 getWriter: { AlignmentI al -> new MyParser(al) },
91 isReadable: { -> true },
93 isWritable: { -> true },
95 isTextFormat: { -> true },
97 isStructureFile: { -> false },
99 isComplexAlignFile: { -> false },
105 * Register the file format. After running this script in Jalview's
106 * Groovy console, the new format should be shown in open file,
107 * save file, and output to textbox menu options.
109 FileFormats.instance.registerFileFormat(myFormat())