Guest User

Untitled

a guest
Oct 18th, 2017
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. """search data information by lib list
  4.  
  5. Usage:
  6. searchLib <libfile>
  7.  
  8. Options:
  9. <libfile> The lib list file.
  10. -h --help Show the help informations.
  11. -v --version Show the version.
  12.  
  13. Contact:
  14. suqingdong <suqingdong@novogene.com>
  15.  
  16. """
  17. from collections import defaultdict
  18. import pymongo
  19. import docopt
  20.  
  21.  
  22. def connectDB(host='192.168.20.19', port=8085, db='project', username='normal', password='novogene2017'):
  23.  
  24. client = pymongo.MongoClient(host=host, port=port)
  25. db = client.get_database(db)
  26. db.authenticate(username, password)
  27.  
  28. return db
  29.  
  30.  
  31. def getLibList(libfile):
  32.  
  33. liblist = []
  34. with open(libfile) as f:
  35. for line in f:
  36. liblist.append(line.strip())
  37.  
  38. return liblist
  39.  
  40.  
  41. def main(libfile):
  42.  
  43. db = connectDB()
  44. samplelist = db.get_collection('samplelist')
  45.  
  46. liblist = getLibList(libfile)
  47. filters = defaultdict(list)
  48. for lib in liblist:
  49. filters['$or'].append({'libid': lib})
  50. options = {'_id': 0}
  51. result = samplelist.find(filters, options)
  52.  
  53. columns = samplelist.find_one().keys()
  54. print '\t'.join(columns)
  55.  
  56. for row in result:
  57. values = [row.get(column) for column in columns]
  58. linelist = [','.join(value) if type(value) == list else str(value) for value in values]
  59. print '\t'.join(linelist)
  60.  
  61. if __name__ == "__main__":
  62.  
  63. arguments = docopt.docopt(__doc__, version='v1.0', help=True)
  64. # print arguments
  65.  
  66. libfile = arguments.get('<libfile>')
  67.  
  68. main(libfile)
Add Comment
Please, Sign In to add comment