Exemple #1
0
 def testParams(self):
     """
     When a BLAST XML file has been read, its parameters must be present
     in the reader instance. We only test a subset of the parameters.
     """
     mockOpener = mockOpen(read_data=RECORD)
     with patch.object(builtins, 'open', mockOpener):
         reader = XMLRecordsReader('file.xml')
         list(reader.records())
         self.assertEqual('BLASTN', reader.params['application'])
         self.assertEqual('virus-nt-20130719', reader.params['database'])
Exemple #2
0
 def testXMLInput(self):
     """
     Test conversion of a chunk of BLAST XML. This is highly incomplete
     in what it tests.
     """
     mockOpener = mockOpen(read_data=RECORD)
     with patch.object(builtins, 'open', mockOpener):
         reader = XMLRecordsReader('file.xml')
         record1, record2 = list(reader.records())
         self.assertEqual(0, len(record1.alignments))
         self.assertEqual(2, len(record2.alignments))
 def testXMLInput(self):
     """
     Test conversion of a chunk of BLAST XML. This is highly incomplete
     in what it tests.
     """
     mockOpener = mockOpen(read_data=RECORD)
     with patch.object(builtins, 'open', mockOpener):
         reader = XMLRecordsReader('file.xml')
         record1, record2 = list(reader.records())
         self.assertEqual(0, len(record1.alignments))
         self.assertEqual(2, len(record2.alignments))
 def testParams(self):
     """
     When a BLAST XML file has been read, its parameters must be present
     in the reader instance. We only test a subset of the parameters.
     """
     mockOpener = mockOpen(read_data=RECORD)
     with patch.object(builtins, 'open', mockOpener):
         reader = XMLRecordsReader('file.xml')
         list(reader.records())
         self.assertEqual('BLASTN', reader.params['application'])
         self.assertEqual('virus-nt-20130719', reader.params['database'])
    parser = argparse.ArgumentParser(
        description='Convert a BLAST XML file to JSON.',
        epilog=('Give a BLAST XML file and convert it to JSON. Optionally '
                'compress the JSON output.')
    )

    parser.add_argument(
        '--json', metavar='JSON-output-file',
        help=('the JSON filename to write the converted XML to. If omitted, '
              'standard output will be used.'))

    parser.add_argument(
        '--xml', metavar='BLAST-XML-file', default=sys.stdin,
        help=('the BLAST XML output file to convert. If omitted, standard '
              'input will be read.'))

    parser.add_argument(
        '--bzip2', default=False, action='store_true',
        help='If True, compress the JSON output using bzip2.')

    args = parser.parse_args()

    if args.bzip2:
        fp = bz2file.BZ2File(args.json or sys.stdout, 'w')
    else:
        fp = open(args.json, 'w') if args.json else sys.stdout

    reader = XMLRecordsReader(args.xml)
    reader.saveAsJSON(fp)
    fp.close()
                'compress the JSON output.'))

    parser.add_argument(
        '--json',
        metavar='JSON-output-file',
        help=('the JSON filename to write the converted XML to. If omitted, '
              'standard output will be used.'))

    parser.add_argument(
        '--xml',
        metavar='BLAST-XML-file',
        default=sys.stdin,
        help=('the BLAST XML output file to convert. If omitted, standard '
              'input will be read.'))

    parser.add_argument('--bzip2',
                        default=False,
                        action='store_true',
                        help='If True, compress the JSON output using bzip2.')

    args = parser.parse_args()

    if args.bzip2:
        fp = bz2file.BZ2File(args.json or sys.stdout, 'w')
    else:
        fp = open(args.json, 'w') if args.json else sys.stdout

    reader = XMLRecordsReader(args.xml)
    reader.saveAsJSON(fp)
    fp.close()
#!/usr/bin/env python

if __name__ == '__main__':
    from dark.blast.conversion import XMLRecordsReader
    import sys
    if len(sys.argv) == 2:
        reader = XMLRecordsReader(sys.argv[1])
        reader.saveAsJSON(sys.stdout)
    elif len(sys.argv) == 3:
        reader = XMLRecordsReader(sys.argv[1])
        with open(sys.argv[2], 'w') as fp:
            reader.saveAsJSON(fp)
    else:
        print >>sys.stderr, 'Usage: %s infile.xml [outfile.json]' % sys.argv[0]
        sys.exit(1)