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.FileParse
7 import jalview.io.AlignFile
10 * Example script that registers a new alignment file format
11 * consisting of lines like
13 * !S=<sequence string>
17 * A parser class to read or write the format
19 class MyParser extends AlignFile
22 * Constructor for reading a file; the superclass
23 * constructor will call the parse() method
25 MyParser(FileParse src)
31 * Constructor for writing out an alignment
33 MyParser(AlignmentI al)
38 * Parse a formatted data file (with no error checking!)
44 while ((line = nextLine()) != null)
46 if (line.startsWith('!I='))
48 int pos = line.indexOf('/')
49 id = line.substring(3, pos == -1 ? line.length() : pos)
50 } else if (line.startsWith('!S='))
52 String seq = line.substring(3)
53 addSequence(new Sequence(id, seq))
59 * Print the formatted sequences
60 * (addSuffix always defaults to true as no user preference for it)
62 String print(SequenceI[] seqs, boolean addSuffix)
64 StringBuilder sb = new StringBuilder()
65 for (SequenceI seq : seqs)
67 sb.append('!I=').append(seq.getDisplayId(addSuffix)).append('\n')
68 sb.append('!S=').append(seq.getSequenceAsString()).append('\n')
75 * A closure that defines the 'Groovy example' file format,
76 * delegating to MyParser for reading and writing
80 getName: { -> 'Groovy example' },
82 toString: { -> getName() },
84 getExtensions: { -> 'grv' },
86 getReader: { FileParse source -> new MyParser(source) },
88 getWriter: { AlignmentI al -> new MyParser(al) },
90 isReadable: { -> true },
92 isWritable: { -> true },
94 isTextFormat: { -> true },
96 isStructureFile: { -> false },
98 isComplexAlignFile: { -> false },
104 * Register the file format. After running this script in Jalview's
105 * Groovy console, the new format should be shown in open file,
106 * save file, and output to textbox menu options.
108 FileFormats.instance.registerFileFormat(myFormat())